ObservableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-21 04:45:43 +01:00
parent 4020f99d18
commit 1b9fa09186
2 changed files with 34 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
import { Implements } from "@thilawyn/traitify-ts"
import { z } from "zod"
import { ZodSchemaClass } from "./ZodSchemaClass"
import { ObservableZodSchemaObject } from "./traits/ObservableZodSchemaObject"
const TestExp = ZodSchemaClass({
@@ -11,12 +12,14 @@ const TestExp = ZodSchemaClass({
}),
defaultValues: { id: -1n },
}).build()
})
.expresses(ObservableZodSchemaObject)
.buildAnyway()
@TestExp.implementsStatic
class Test extends TestExp.extends implements Implements<typeof TestExp> {}
Test.defaultValues
Test.schema
const inst = Test.create({ id: 1n, name: "" })
console.log(inst)
@@ -37,3 +40,15 @@ console.log(subInst)
// class ChildTest extends Test {}
// ChildTest.instantiate({ name: "" })
const testSchema = { schema: Test.schema }
declare const testGenericSchema: {
schema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, object, object>
}
type U<T> = T
interface ExtendTest extends U<typeof testSchema> {
schema: z.ZodObject<any, any, any, any, any>
}

View File

@@ -1,14 +1,20 @@
import { expression } from "@thilawyn/traitify-ts"
import { InstantiableZodSchemaObject } from "./InstantiableZodSchemaObject"
import { trait } from "@thilawyn/traitify-ts"
import { mapValues } from "lodash-es"
import { makeObservable, observable } from "mobx"
import { z } from "zod"
export const ObservableZodSchemaObject = expression
.expresses(InstantiableZodSchemaObject)
.build()
.subtrait()
export const ObservableZodSchemaObject = trait
.staticAbstract(Super => class extends Super {
declare readonly schema: z.ZodObject<z.ZodRawShape, z.UnknownKeysParam, z.ZodTypeAny, object, object>
})
.implement(Super => class ObservableZodSchemaObject extends Super {
constructor(...args: any[]) {
super(...args)
makeObservable(this,
mapValues(ObservableZodSchemaObject.schema.shape, () => observable)
)
}
})
.build()