Compare commits

...

21 Commits

Author SHA1 Message Date
Julien Valverdé
b98a7cf3fc Build work
All checks were successful
continuous-integration/drone/push Build is passing
2024-03-09 03:52:54 +01:00
Julien Valverdé
663db5fab2 Build system work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:45:13 +01:00
Julien Valverdé
2cfe4fc54e ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:31:51 +01:00
Julien Valverdé
34f57f017f Tests
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:22:15 +01:00
Julien Valverdé
b7e224d89b ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 03:17:12 +01:00
Julien Valverdé
f57360645b ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 02:07:56 +01:00
Julien Valverdé
732780342d ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 01:52:17 +01:00
Julien Valverdé
12b73e39e3 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 01:29:38 +01:00
Julien Valverdé
81efdaab01 ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:43:53 +01:00
Julien Valverdé
24b549ffaa JsonifiableZodSchemaObject done
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:31:10 +01:00
Julien Valverdé
dd0610264d CreateArgs
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-09 00:18:05 +01:00
Julien Valverdé
ce66ffd0c5 JsonifiableZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-08 23:59:07 +01:00
Julien Valverdé
b680295b6c Dependencies upgrade
Some checks failed
continuous-integration/drone/push Build is failing
2024-03-08 20:11:14 +01:00
Julien Valverdé
4392d43211 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 05:02:50 +01:00
Julien Valverdé
aefca6657b ZodSchemaObject fix
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:53:33 +01:00
Julien Valverdé
69d47ba3cf instantiationTransform
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:50:55 +01:00
Julien Valverdé
98ae430249 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:30:33 +01:00
Julien Valverdé
0edbe3f45c ZodSchemaClassBuilder work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 04:02:14 +01:00
Julien Valverdé
c3c454eb15 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 02:57:30 +01:00
Julien Valverdé
d204b71a05 ZodSchemaObject work
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 02:44:02 +01:00
Julien Valverdé
1956aae555 ZodSchemaObject
Some checks failed
continuous-integration/drone/push Build is failing
2024-02-28 01:59:33 +01:00
15 changed files with 472 additions and 659 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -11,32 +11,22 @@
"exports": { "exports": {
".": { ".": {
"import": { "import": {
"types": "./dist/schemable.d.mts", "types": "./dist/lib.d.mts",
"default": "./dist/schemable.mjs" "default": "./dist/lib.mjs"
}, },
"require": { "require": {
"types": "./dist/schemable.d.cts", "types": "./dist/lib.d.cts",
"default": "./dist/schemable.cjs" "default": "./dist/lib.cjs"
} }
}, },
"./jsonifiable": { "./schema": {
"import": { "import": {
"types": "./dist/jsonifiable.d.mts", "types": "./dist/schema.d.mts",
"default": "./dist/jsonifiable.mjs" "default": "./dist/schema.mjs"
}, },
"require": { "require": {
"types": "./dist/jsonifiable.d.cts", "types": "./dist/schema.d.cts",
"default": "./dist/jsonifiable.cjs" "default": "./dist/schema.cjs"
}
},
"./observable": {
"import": {
"types": "./dist/observable.d.mts",
"default": "./dist/observable.mjs"
},
"require": {
"types": "./dist/observable.d.cts",
"default": "./dist/observable.cjs"
} }
} }
}, },
@@ -48,12 +38,12 @@
"clean:node": "rm -rf node_modules" "clean:node": "rm -rf node_modules"
}, },
"dependencies": { "dependencies": {
"@thilawyn/traitify-ts": "^0.1.13", "@thilawyn/traitify-ts": "^0.1.14",
"decimal.js": "^10.4.3", "decimal.js": "^10.4.3",
"effect": "^2.4.0", "effect": "^2.4.1",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"mobx": "^6.12.0", "mobx": "^6.12.0",
"type-fest": "^4.10.3", "type-fest": "^4.12.0",
"zod": "^3.22.4" "zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
@@ -62,11 +52,11 @@
"bun-types": "latest", "bun-types": "latest",
"npm-check-updates": "^16.14.15", "npm-check-updates": "^16.14.15",
"npm-sort": "^0.0.4", "npm-sort": "^0.0.4",
"rollup": "^4.12.0", "rollup": "^4.12.1",
"rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-ts": "^3.4.5", "rollup-plugin-ts": "^3.4.5",
"ts-functional-pipe": "^3.1.2", "ts-functional-pipe": "^3.1.2",
"tsx": "^4.7.1", "tsx": "^4.7.1",
"typescript": "^5.3.3" "typescript": "^5.4.2"
} }
} }

View File

@@ -38,7 +38,6 @@ export const createBundleConfig = (
export default [ export default [
createBundleConfig("src/index.ts", "."), createBundleConfig("src/lib.ts", "."),
createBundleConfig("src/jsonifiable/index.ts", "./jsonifiable"), createBundleConfig("src/schema/lib.ts", "./schema"),
createBundleConfig("src/observable/index.ts", "./observable"),
] ]

View File

@@ -1,26 +1,26 @@
import { Trait, TraitExpression, TraitExpressionBuilder } from "@thilawyn/traitify-ts" import { Trait, TraitExpressionBuilder, expression } from "@thilawyn/traitify-ts"
import { AbstractClass } from "type-fest" import { AbstractClass } from "type-fest"
import { EmptyObject } from "type-fest/source/empty-object"
import { JsonifiableObject } from "type-fest/source/jsonifiable" import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaObject"
import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject"
import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject"
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas" import { ZodSchemaObject } from "../traits/ZodSchemaObject"
import { StaticMembers } from "../util"
export class ZodSchemaClassBuilder< export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>, Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[], const Traits extends readonly Trait<any, any, any, any>[],
> Schemas extends object,
extends TraitExpressionBuilder<Superclass, Traits> { > {
declare ["constructor"]: typeof ZodSchemaClassBuilder declare ["constructor"]: typeof ZodSchemaClassBuilder
schema< constructor(
Super extends AbstractClass<object, []> & { schema?: never, schemaWithDefaultValues?: never }, protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
protected readonly schemas: Schemas,
) {}
schema<
SchemaT extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
@@ -32,7 +32,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
Values extends object, Values extends object,
PartialValues extends Partial<Values>, PartialValues extends Partial<Values>,
>( >(
this: ZodSchemaClassBuilder<Super, Traits>, this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
props: { props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values> schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
@@ -42,39 +42,31 @@ extends TraitExpressionBuilder<Superclass, Traits> {
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> ) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
}, },
) { ) {
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) { const schema = props.schema
static readonly schema = props.schema const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
static readonly schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
abstract class ZodSchemaObjectConstructor {
constructor(values: Values) { constructor(values: Values) {
super()
Object.assign(this, values) Object.assign(this, values)
} }
} }
return new this.constructor( return new this.constructor(
Schemas as unknown as ( this.expression
AbstractClass< .extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
InstanceType<Super> & Values, .expresses(ZodSchemaObject(schema, schemaWithDefaultValues)),
ConstructorParameters<typeof Schemas>
> &
StaticMembers<Super> &
StaticMembers<typeof Schemas>
),
[ { schema, schemaWithDefaultValues } as const,
...this.expressionTraits,
ZodSchemaObjectInstantiationSchemas,
InstantiableZodSchemaObject,
ExtendableZodSchemaObject,
],
) )
} }
jsonifiable< jsonifiable<
Super extends ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> S extends {
& { jsonifySchema?: never, dejsonifySchema?: never }, readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
Instance extends Values, readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema?: never
dejsonifySchema?: never
},
SchemaT extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
@@ -97,10 +89,12 @@ extends TraitExpressionBuilder<Superclass, Traits> {
JsonifiedValues extends JsonifiableObject, JsonifiedValues extends JsonifiableObject,
>( >(
this: ZodSchemaClassBuilder< this: ZodSchemaClassBuilder<Superclass, Traits, S | {
Super | ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>, readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
Traits readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
>, jsonifySchema?: never
dejsonifySchema?: never
}>,
props: { props: {
jsonifySchema: ( jsonifySchema: (
@@ -112,83 +106,28 @@ extends TraitExpressionBuilder<Superclass, Traits> {
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues> ) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
}, },
) { ) {
const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema) const jsonifySchema = props.jsonifySchema(this.schemas.schema)
const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema) const dejsonifySchema = props.dejsonifySchema(this.schemas.schema)
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema
readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
readonly dejsonifySchema = dejsonifySchema
}
return new this.constructor( return new this.constructor(
JsonifiableSchemas as unknown as ( this.expression.expresses(
AbstractClass< JsonifiableZodSchemaObject(
Instance & JsonifiableSchemas, this.schemas.schema,
this.schemas.schemaWithDefaultValues,
// TODO: for some reason, ConstructorParameters<Super> does not work here. Maybe try to find a fix? jsonifySchema,
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>> dejsonifySchema,
> & )
StaticMembers<Super> &
StaticMembers<typeof JsonifiableSchemas>
), ),
[ { ...this.schemas as S, jsonifySchema, dejsonifySchema } as const,
...this.expressionTraits,
JsonifiableZodSchemaObject,
DejsonifiableZodSchemaObject,
],
) )
} }
toExpression() {
return this.expression
} }
export interface ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>,
Traits extends readonly Trait<any, any, any, any>[],
> {
extends<
Super extends AbstractClass<object>
>(
// \/ Ensures `extends` can only be called once at the beginning
this: ZodSchemaClassBuilder<typeof TraitExpression.NullSuperclass, Traits>,
superclass: Super,
): ZodSchemaClassBuilder<Super, Traits>
expresses<
const T extends readonly Trait<
TraitExpression<
typeof TraitExpression.NullSuperclass,
readonly Trait<any, any, any, any>[]
>,
any,
any,
any
>[]
>(
...traits: T
): ZodSchemaClassBuilder<
Superclass,
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
>
expressesFirst<
const T extends readonly Trait<
TraitExpression<
typeof TraitExpression.NullSuperclass,
readonly Trait<any, any, any, any>[]
>,
any,
any,
any
>[]
>(
...traits: T
): ZodSchemaClassBuilder<
Superclass,
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
>
} }
export const zodSchemaClass = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, []) export const zodSchemaClass = new ZodSchemaClassBuilder(expression, {})

View File

@@ -1,196 +1,196 @@
import { expression } from "@thilawyn/traitify-ts" // import { expression } from "@thilawyn/traitify-ts"
import { AbstractClass, Simplify } from "type-fest" // import { AbstractClass, Simplify } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable" // import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" // import { z } from "zod"
import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass" // import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass"
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass" // import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
import { Extend, Override, StaticMembers } from "../util" // import { Extend, Override, StaticMembers } from "../util"
export class ZodSchemaClassExtender< // export class ZodSchemaClassExtender<
Superclass extends AbstractClass<object>, // Superclass extends AbstractClass<object>,
Subclass extends AbstractClass<object>, // Subclass extends AbstractClass<object>,
> { // > {
declare ["constructor"]: typeof ZodSchemaClassExtender // declare ["constructor"]: typeof ZodSchemaClassExtender
constructor( // constructor(
readonly superclass: Superclass, // readonly superclass: Superclass,
readonly subclass: Subclass, // readonly subclass: Subclass,
) {} // ) {}
schema< // schema<
Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>, // Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
SuperSchemaT extends z.ZodRawShape, // SuperSchemaT extends z.ZodRawShape,
SuperSchemaUnknownKeys extends z.UnknownKeysParam, // SuperSchemaUnknownKeys extends z.UnknownKeysParam,
SuperSchemaCatchall extends z.ZodTypeAny, // SuperSchemaCatchall extends z.ZodTypeAny,
SuperValues extends object, // SuperValues extends object,
SuperDefaultValues extends Partial<SuperValues>, // SuperDefaultValues extends Partial<SuperValues>,
SchemaT extends z.ZodRawShape, // SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, // SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, // SchemaCatchall extends z.ZodTypeAny,
Values extends SuperValues, // Values extends SuperValues,
DefaultValues extends Partial<Values>, // DefaultValues extends Partial<Values>,
>( // >(
this: ZodSchemaClassExtender< // this: ZodSchemaClassExtender<
Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>, // Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
any // any
>, // >,
props: { // props: {
schema: (schema: Super["schema"]) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values> // schema: (schema: Super["schema"]) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues // defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
}, // },
) { // ) {
const schema = props.schema(this.superclass.schema) // const schema = props.schema(this.superclass.schema)
const defaultValues = props.defaultValues(this.superclass.defaultValues) // const defaultValues = props.defaultValues(this.superclass.defaultValues)
class Schemas extends (this.superclass as AbstractClass<object, any[]>) { // class Schemas extends (this.superclass as AbstractClass<object, any[]>) {
static readonly schema = schema // static readonly schema = schema
static readonly defaultValues = defaultValues // static readonly defaultValues = defaultValues
} // }
return new this.constructor( // return new this.constructor(
this.superclass as Super, // this.superclass as Super,
Schemas as unknown as AbstractClass< // Schemas as unknown as AbstractClass<
Simplify< // Simplify<
Extend<[InstanceType<Super>, Values]> // Extend<[InstanceType<Super>, Values]>
>, // >,
[values: Values] // [values: Values]
> & // > &
Simplify< // Simplify<
Override<[ // Override<[
StaticMembers<Super>, // StaticMembers<Super>,
StaticMembers<typeof Schemas>, // StaticMembers<typeof Schemas>,
]> // ]>
> // >
) // )
} // }
jsonifiable< // jsonifiable<
/** Superclass jsonifiable schemas */ // /** Superclass jsonifiable schemas */
Super extends JsonifiableZodSchemaAbstractClass< // Super extends JsonifiableZodSchemaAbstractClass<
any, // any,
SuperJsonifySchemaT, // SuperJsonifySchemaT,
SuperJsonifySchemaUnknownKeys, // SuperJsonifySchemaUnknownKeys,
SuperJsonifySchemaCatchall, // SuperJsonifySchemaCatchall,
SuperDejsonifySchemaT, // SuperDejsonifySchemaT,
SuperDejsonifySchemaUnknownKeys, // SuperDejsonifySchemaUnknownKeys,
SuperDejsonifySchemaCatchall, // SuperDejsonifySchemaCatchall,
SuperJsonifiedValues, // SuperJsonifiedValues,
SuperValues // SuperValues
>, // >,
SuperJsonifySchemaT extends z.ZodRawShape, // SuperJsonifySchemaT extends z.ZodRawShape,
SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam, // SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
SuperJsonifySchemaCatchall extends z.ZodTypeAny, // SuperJsonifySchemaCatchall extends z.ZodTypeAny,
SuperDejsonifySchemaT extends z.ZodRawShape, // SuperDejsonifySchemaT extends z.ZodRawShape,
SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam, // SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
SuperDejsonifySchemaCatchall extends z.ZodTypeAny, // SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
SuperJsonifiedValues extends JsonifiableObject, // SuperJsonifiedValues extends JsonifiableObject,
SuperValues extends object, // SuperValues extends object,
/** New schemas */ // /** New schemas */
Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>, // Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
SelfSchemaT extends z.ZodRawShape, // SelfSchemaT extends z.ZodRawShape,
SelfSchemaUnknownKeys extends z.UnknownKeysParam, // SelfSchemaUnknownKeys extends z.UnknownKeysParam,
SelfSchemaCatchall extends z.ZodTypeAny, // SelfSchemaCatchall extends z.ZodTypeAny,
SelfValues extends object, // SelfValues extends object,
SelfDefaultValues extends Partial<SelfValues>, // SelfDefaultValues extends Partial<SelfValues>,
/* New jsonifiable schemas */ // /* New jsonifiable schemas */
JsonifySchemaT extends z.ZodRawShape, // JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam, // JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny, // JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape, // DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, // DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny, // DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends SuperJsonifiedValues, // JsonifiedValues extends SuperJsonifiedValues,
Values extends SelfValues, // Values extends SelfValues,
>( // >(
this: ZodSchemaClassExtender< // this: ZodSchemaClassExtender<
Super | JsonifiableZodSchemaAbstractClass< // Super | JsonifiableZodSchemaAbstractClass<
any, // any,
SuperJsonifySchemaT, // SuperJsonifySchemaT,
SuperJsonifySchemaUnknownKeys, // SuperJsonifySchemaUnknownKeys,
SuperJsonifySchemaCatchall, // SuperJsonifySchemaCatchall,
SuperDejsonifySchemaT, // SuperDejsonifySchemaT,
SuperDejsonifySchemaUnknownKeys, // SuperDejsonifySchemaUnknownKeys,
SuperDejsonifySchemaCatchall, // SuperDejsonifySchemaCatchall,
SuperJsonifiedValues, // SuperJsonifiedValues,
SuperValues // SuperValues
>, // >,
Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues> // Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
>, // >,
props: { // props: {
jsonifySchema: ( // jsonifySchema: (
schema: Self["schema"], // schema: Self["schema"],
jsonifySchema: Super["jsonifySchema"], // jsonifySchema: Super["jsonifySchema"],
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values> // ) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
dejsonifySchema: ( // dejsonifySchema: (
schema: Self["schema"], // schema: Self["schema"],
dejsonifySchema: Super["dejsonifySchema"], // dejsonifySchema: Super["dejsonifySchema"],
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues> // ) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
}, // },
) { // ) {
const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema) // const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema)
const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema) // const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema)
class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) { // class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema // static readonly jsonifySchema = jsonifySchema
readonly jsonifySchema = jsonifySchema // readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema // static readonly dejsonifySchema = dejsonifySchema
readonly dejsonifySchema = dejsonifySchema // readonly dejsonifySchema = dejsonifySchema
} // }
return new this.constructor( // return new this.constructor(
this.superclass as Super, // this.superclass as Super,
JsonifiableSchemas as unknown as AbstractClass< // JsonifiableSchemas as unknown as AbstractClass<
Simplify< // Simplify<
Override<[InstanceType<Self>, JsonifiableSchemas]> // Override<[InstanceType<Self>, JsonifiableSchemas]>
>, // >,
ConstructorParameters< // ConstructorParameters<
ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues> // ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
> // >
> & // > &
Simplify< // Simplify<
Override<[ // Override<[
StaticMembers<Self>, // StaticMembers<Self>,
StaticMembers<typeof JsonifiableSchemas>, // StaticMembers<typeof JsonifiableSchemas>,
]> // ]>
>, // >,
) // )
} // }
toClass() { // toClass() {
return this.subclass // return this.subclass
} // }
toExpressionBuilder() { // toExpressionBuilder() {
return expression.extends(this.subclass) // return expression.extends(this.subclass)
} // }
} // }

View File

@@ -1 +1,5 @@
export { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject" export { ZodSchemaClassBuilder } from "./builders/ZodSchemaClassBuilder"
// export { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject"
export { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject"
export { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
export { ZodSchemaObject, ZodSchemaObjectTrait } from "./traits/ZodSchemaObject"

1
src/schema/lib.ts Normal file
View File

@@ -0,0 +1 @@
export * from "./jsonify"

View File

@@ -28,23 +28,21 @@ const exp = zodSchemaClass
id: dejsonify.bigint(s.shape.id) id: dejsonify.bigint(s.shape.id)
}), }),
}) })
.toExpression()
.expresses(MobXObservableZodSchemaObject) .expresses(MobXObservableZodSchemaObject)
.build() .build()
@exp.staticImplements @exp.staticImplements
class User extends exp.extends implements Implements<typeof exp> {} class User extends exp.extends implements Implements<typeof exp> {}
const test = User.schema.extend({ const inst = User.create({ id: 1n, name: "User" }, )
prout: z.string()
}).merge(
User.schemaWithDefaultValues
)
const inst = User.create({ id: 1n, name: "User" })
// console.log(inst.name) // console.log(inst.name)
const instEffect = User.createEffect({ id: 1n, name: "User" }) const instEffect = User.createEffect({ id: 1n, name: "User" })
// const jsonifiedUser = await inst.jsonifyPromise() const jsonifiedUser = await inst.jsonifyPromise()
const dejsonifiedInst = await User.dejsonifyPromise(jsonifiedUser)
// const AdminUserProto = User.extend() // const AdminUserProto = User.extend()
// .schema({ // .schema({

View File

@@ -1,122 +0,0 @@
import { trait } from "@thilawyn/traitify-ts"
import { Effect } from "effect"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
import { parseZodTypeEffect } from "../util"
export const DejsonifiableZodSchemaObject = trait
.implement(Super => class DejsonifiableZodSchemaObject extends Super {
static dejsonify<
Instance extends Values,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
>(
this: JsonifiableZodSchemaClass<
Instance,
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return new this(
this.dejsonifySchema.parse(values, params)
)
}
static async dejsonifyPromise<
Instance extends Values,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
>(
this: JsonifiableZodSchemaClass<
Instance,
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return new this(
await this.dejsonifySchema.parseAsync(values, params)
)
}
static dejsonifyEffect<
Instance extends Values,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
>(
this: JsonifiableZodSchemaClass<
Instance,
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return parseZodTypeEffect(this.dejsonifySchema, values, params).pipe(
Effect.map(values => new this(values)),
)
}
})
.build()

View File

@@ -1,16 +1,16 @@
import { trait } from "@thilawyn/traitify-ts" // import { trait } from "@thilawyn/traitify-ts"
import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender" // import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass" // import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
export const ExtendableZodSchemaObject = trait // export const ExtendableZodSchemaObject = trait
.implement(Super => class ExtendableZodSchemaObject extends Super { // .implement(Super => class ExtendableZodSchemaObject extends Super {
static extend< // static extend<
Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>, // Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>,
>( // >(
this: Self // this: Self
) { // ) {
return new ZodSchemaClassExtender(this, this) // return new ZodSchemaClassExtender(this, this)
} // }
}) // })
.build() // .build()

View File

@@ -1,90 +0,0 @@
import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
import { HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
import { parseZodTypeEffect } from "../util"
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
type NewZodSchemaInstanceInput<
Values extends object,
DefaultValues extends Partial<Values>,
> = {
[Key in Exclude<keyof Values, keyof DefaultValues>]: Values[Key]
} & {
[Key in keyof DefaultValues]?: Key extends keyof Values
? Values[Key]
: never
}
type ParseParamsArgs = [] | [params: Partial<z.ParseParams>]
type NewZodSchemaInstanceArgs<Input extends object> = (
HasRequiredKeys<Input> extends true
? [values: Input, ...args: ParseParamsArgs]
: [] | [values: Input, ...args: ParseParamsArgs]
)
export const InstantiableZodSchemaObject = trait
.implement(Super => class InstantiableZodSchemaObject extends Super {
static create<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return this.instantiationSchemaWithDefaultValues().parse(values, params)
}
static async createPromise<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
}
static createEffect<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: (
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
ZodSchemaObjectInstantiationSchemasStaticMembers
),
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
) {
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
}
})
.build()

View File

@@ -1,100 +1,112 @@
import { trait } from "@thilawyn/traitify-ts" import { ImplStatic, expression } from "@thilawyn/traitify-ts"
import { Class } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable" import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod" import { z } from "zod"
import { JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass" import { parseZodSchemaEffect } from "../util"
import { parseZodTypeEffect } from "../util" import { ZodSchemaObject } from "./ZodSchemaObject"
export const JsonifiableZodSchemaObject = trait export const JsonifiableZodSchemaObject = <
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
>(
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>,
dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>,
) => expression
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues))
.build()
.subtrait()
.implement(Super => class JsonifiableZodSchemaObject extends Super { .implement(Super => class JsonifiableZodSchemaObject extends Super {
jsonify< static readonly jsonifySchema = jsonifySchema
JsonifySchemaT extends z.ZodRawShape, static readonly dejsonifySchema = dejsonifySchema
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject, jsonify(params?: Partial<z.ParseParams>) {
Values extends object, return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
>( .jsonifySchema
this: Values & JsonifiableZodSchemas< .parse(this, params)
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
params?: Partial<z.ParseParams>,
) {
return this.jsonifySchema.parse(this, params)
} }
async jsonifyPromise< jsonifyPromise(params?: Partial<z.ParseParams>) {
JsonifySchemaT extends z.ZodRawShape, return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
JsonifySchemaUnknownKeys extends z.UnknownKeysParam, .jsonifySchema
JsonifySchemaCatchall extends z.ZodTypeAny, .parseAsync(this, params)
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
>(
this: Values & JsonifiableZodSchemas<
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
params?: Partial<z.ParseParams>,
) {
return this.jsonifySchema.parseAsync(this, params)
} }
jsonifyEffect< jsonifyEffect(params?: Partial<z.ParseParams>) {
JsonifySchemaT extends z.ZodRawShape, return parseZodSchemaEffect(
JsonifySchemaUnknownKeys extends z.UnknownKeysParam, (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>).jsonifySchema,
JsonifySchemaCatchall extends z.ZodTypeAny, this,
params,
)
}
DejsonifySchemaT extends z.ZodRawShape,
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject, static dejsonify<
Values extends object, Instance extends Values
>( >(
this: Values & JsonifiableZodSchemas< this: (
JsonifySchemaT, Class<Instance, [values: Values]> &
JsonifySchemaUnknownKeys, ImplStatic<typeof JsonifiableZodSchemaObject>
JsonifySchemaCatchall, ),
values: JsonifiedValues,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
params?: Partial<z.ParseParams>, params?: Partial<z.ParseParams>,
) { ) {
return parseZodTypeEffect(this.jsonifySchema, this, params) return this
.transform(this.dejsonifySchema)
.parse(values, params)
}
static dejsonifyPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return this
.transform(this.dejsonifySchema)
.parseAsync(values, params)
}
static dejsonifyEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
return parseZodSchemaEffect(
this.transform(this.dejsonifySchema),
values,
params,
)
} }
}) })
.build() .build()

View File

@@ -0,0 +1,122 @@
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
import { Class, HasRequiredKeys } from "type-fest"
import { z } from "zod"
import { parseZodSchemaEffect } from "../util"
type CreateArgs<Input extends object> = (
HasRequiredKeys<Input> extends true
? [values: Input, params?: Partial<z.ParseParams>]
: [] | [values: Input, params?: Partial<z.ParseParams>]
)
export const ZodSchemaObject = <
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) => trait
.implement(Super => class ZodSchemaObject extends Super {
static readonly schema = schema
static readonly schemaWithDefaultValues = schemaWithDefaultValues
static transform<
Instance extends Values,
T extends z.ZodRawShape,
UnknownKeys extends z.UnknownKeysParam,
Catchall extends z.ZodTypeAny,
Output extends Values,
Input,
>(
this: Class<Instance, [values: Values]>,
schema: z.ZodObject<T, UnknownKeys, Catchall, Output, Input>,
) {
return schema.transform(values => new this(values))
}
static create<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parse(values, params)
}
static createPromise<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return this
.transform(this.schemaWithDefaultValues)
.parseAsync(values, params)
}
static createEffect<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof ZodSchemaObject>
),
...[values, params]: CreateArgs<PartialValues>
) {
return parseZodSchemaEffect(
this.transform(this.schemaWithDefaultValues),
values,
params,
)
}
})
.build()
export type ZodSchemaObjectTrait<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
> = (
ReturnType<
typeof ZodSchemaObject<
SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
SchemaWithDefaultValuesT,
SchemaWithDefaultValuesUnknownKeys,
SchemaWithDefaultValuesCatchall,
Values,
PartialValues
>
>
)

View File

@@ -1,37 +0,0 @@
import { trait } from "@thilawyn/traitify-ts"
import { z } from "zod"
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
export const ZodSchemaObjectInstantiationSchemas = trait
.implement(Super => class ZodSchemaObjectInstantiationSchemas extends Super {
static instantiationSchema<
Instance extends Values,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
Values extends object,
>(
this: ZodSchemaClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, any, any, any, Values, any>,
) {
return this.schema.transform(values => new this(values))
}
static instantiationSchemaWithDefaultValues<
Instance extends Values,
SchemaWithDefaultValuesT extends z.ZodRawShape,
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
Values extends object,
PartialValues extends Partial<Values>,
>(
this: ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
) {
return this.schemaWithDefaultValues.transform(values => new this(values))
}
})
.build()

View File

@@ -3,16 +3,13 @@ import { z } from "zod"
/** /**
* Parses a value using a ZodType schema wrapped in an Effect monad. * Parses a value using a Zod schema wrapped in an Effect monad.
* *
* @param schema - The ZodType schema to use for parsing. * @param schema - The Zod schema to use for parsing.
* @param args - The arguments to pass to the `safeParseAsync` method of the schema. * @param args - The arguments to pass to the `safeParseAsync` method of the schema.
* @returns An Effect monad representing the parsing result. * @returns An Effect monad representing the parsing result.
*/ */
export const parseZodTypeEffect = < export const parseZodSchemaEffect = <Output, Input>(
Output,
Input,
>(
schema: z.ZodType<Output, z.ZodTypeDef, Input>, schema: z.ZodType<Output, z.ZodTypeDef, Input>,
...args: Parameters<typeof schema.safeParseAsync> ...args: Parameters<typeof schema.safeParseAsync>
) => pipe( ) => pipe(