0.1.1 #2
@@ -0,0 +1 @@
|
|||||||
|
export * from "./makeSchemableClassObservable"
|
||||||
|
|||||||
29
src/observable/makeSchemableClassObservable.ts
Normal file
29
src/observable/makeSchemableClassObservable.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { mapValues } from "lodash-es"
|
||||||
|
import { makeObservable, observable } from "mobx"
|
||||||
|
import { AbstractConstructor } from "type-fest"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { SchemableClass } from ".."
|
||||||
|
|
||||||
|
|
||||||
|
export function makeSchemableClassObservable<
|
||||||
|
C extends SchemableClass<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues extends Partial<Values>,
|
||||||
|
>(
|
||||||
|
extend: C | SchemableClass<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>
|
||||||
|
) {
|
||||||
|
return class extends (extend as AbstractConstructor<any>) {
|
||||||
|
constructor(...args: any[]) {
|
||||||
|
super(...args)
|
||||||
|
|
||||||
|
makeObservable(this,
|
||||||
|
mapValues(this.schema.shape, () => observable)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} as unknown as C
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ import { pipeInto } from "ts-functional-pipe"
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { defineDefaultValues, extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
import { defineDefaultValues, extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
||||||
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
import { dejsonifyBigIntSchema, dejsonifySchemable, jsonifyBigIntSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||||
|
import { makeSchemableClassObservable } from "./observable"
|
||||||
|
|
||||||
|
|
||||||
const UserLevel = z.enum(["User", "Admin"])
|
const UserLevel = z.enum(["User", "Admin"])
|
||||||
@@ -20,6 +21,8 @@ class User extends pipeInto(
|
|||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
v => makeSchemableClassObservable(v),
|
||||||
|
|
||||||
v => makeJsonifiableSchemableClass(v, {
|
v => makeJsonifiableSchemableClass(v, {
|
||||||
jsonifySchema: ({ schema, shape }) => schema.extend({
|
jsonifySchema: ({ schema, shape }) => schema.extend({
|
||||||
id: jsonifyBigIntSchema(shape.id)
|
id: jsonifyBigIntSchema(shape.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user