ObservableZodSchemaObject
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
19
src/tests.ts
19
src/tests.ts
@@ -1,6 +1,7 @@
|
|||||||
import { Implements } from "@thilawyn/traitify-ts"
|
import { Implements } from "@thilawyn/traitify-ts"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||||
|
import { ObservableZodSchemaObject } from "./traits/ObservableZodSchemaObject"
|
||||||
|
|
||||||
|
|
||||||
const TestExp = ZodSchemaClass({
|
const TestExp = ZodSchemaClass({
|
||||||
@@ -11,12 +12,14 @@ const TestExp = ZodSchemaClass({
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
defaultValues: { id: -1n },
|
defaultValues: { id: -1n },
|
||||||
}).build()
|
})
|
||||||
|
.expresses(ObservableZodSchemaObject)
|
||||||
|
.buildAnyway()
|
||||||
|
|
||||||
@TestExp.implementsStatic
|
@TestExp.implementsStatic
|
||||||
class Test extends TestExp.extends implements Implements<typeof TestExp> {}
|
class Test extends TestExp.extends implements Implements<typeof TestExp> {}
|
||||||
|
|
||||||
Test.defaultValues
|
Test.schema
|
||||||
const inst = Test.create({ id: 1n, name: "" })
|
const inst = Test.create({ id: 1n, name: "" })
|
||||||
console.log(inst)
|
console.log(inst)
|
||||||
|
|
||||||
@@ -37,3 +40,15 @@ console.log(subInst)
|
|||||||
// class ChildTest extends Test {}
|
// class ChildTest extends Test {}
|
||||||
|
|
||||||
// ChildTest.instantiate({ name: "" })
|
// 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>
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
import { expression } from "@thilawyn/traitify-ts"
|
import { trait } from "@thilawyn/traitify-ts"
|
||||||
import { InstantiableZodSchemaObject } from "./InstantiableZodSchemaObject"
|
import { mapValues } from "lodash-es"
|
||||||
|
import { makeObservable, observable } from "mobx"
|
||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
|
||||||
export const ObservableZodSchemaObject = expression
|
export const ObservableZodSchemaObject = trait
|
||||||
.expresses(InstantiableZodSchemaObject)
|
.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()
|
.build()
|
||||||
.subtrait()
|
|
||||||
.implement(Super => class ObservableZodSchemaObject extends Super {
|
|
||||||
constructor(...args: any[]) {
|
|
||||||
super(...args)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user