Compare commits
21 Commits
b1f73a2671
...
b98a7cf3fc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b98a7cf3fc | ||
|
|
663db5fab2 | ||
|
|
2cfe4fc54e | ||
|
|
34f57f017f | ||
|
|
b7e224d89b | ||
|
|
f57360645b | ||
|
|
732780342d | ||
|
|
12b73e39e3 | ||
|
|
81efdaab01 | ||
|
|
24b549ffaa | ||
|
|
dd0610264d | ||
|
|
ce66ffd0c5 | ||
|
|
b680295b6c | ||
|
|
4392d43211 | ||
|
|
aefca6657b | ||
|
|
69d47ba3cf | ||
|
|
98ae430249 | ||
|
|
0edbe3f45c | ||
|
|
c3c454eb15 | ||
|
|
d204b71a05 | ||
|
|
1956aae555 |
38
package.json
38
package.json
@@ -11,32 +11,22 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./dist/schemable.d.mts",
|
||||
"default": "./dist/schemable.mjs"
|
||||
"types": "./dist/lib.d.mts",
|
||||
"default": "./dist/lib.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/schemable.d.cts",
|
||||
"default": "./dist/schemable.cjs"
|
||||
"types": "./dist/lib.d.cts",
|
||||
"default": "./dist/lib.cjs"
|
||||
}
|
||||
},
|
||||
"./jsonifiable": {
|
||||
"./schema": {
|
||||
"import": {
|
||||
"types": "./dist/jsonifiable.d.mts",
|
||||
"default": "./dist/jsonifiable.mjs"
|
||||
"types": "./dist/schema.d.mts",
|
||||
"default": "./dist/schema.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/jsonifiable.d.cts",
|
||||
"default": "./dist/jsonifiable.cjs"
|
||||
}
|
||||
},
|
||||
"./observable": {
|
||||
"import": {
|
||||
"types": "./dist/observable.d.mts",
|
||||
"default": "./dist/observable.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/observable.d.cts",
|
||||
"default": "./dist/observable.cjs"
|
||||
"types": "./dist/schema.d.cts",
|
||||
"default": "./dist/schema.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -48,12 +38,12 @@
|
||||
"clean:node": "rm -rf node_modules"
|
||||
},
|
||||
"dependencies": {
|
||||
"@thilawyn/traitify-ts": "^0.1.13",
|
||||
"@thilawyn/traitify-ts": "^0.1.14",
|
||||
"decimal.js": "^10.4.3",
|
||||
"effect": "^2.4.0",
|
||||
"effect": "^2.4.1",
|
||||
"lodash-es": "^4.17.21",
|
||||
"mobx": "^6.12.0",
|
||||
"type-fest": "^4.10.3",
|
||||
"type-fest": "^4.12.0",
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -62,11 +52,11 @@
|
||||
"bun-types": "latest",
|
||||
"npm-check-updates": "^16.14.15",
|
||||
"npm-sort": "^0.0.4",
|
||||
"rollup": "^4.12.0",
|
||||
"rollup": "^4.12.1",
|
||||
"rollup-plugin-cleanup": "^3.2.1",
|
||||
"rollup-plugin-ts": "^3.4.5",
|
||||
"ts-functional-pipe": "^3.1.2",
|
||||
"tsx": "^4.7.1",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.4.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ export const createBundleConfig = (
|
||||
|
||||
|
||||
export default [
|
||||
createBundleConfig("src/index.ts", "."),
|
||||
createBundleConfig("src/jsonifiable/index.ts", "./jsonifiable"),
|
||||
createBundleConfig("src/observable/index.ts", "./observable"),
|
||||
createBundleConfig("src/lib.ts", "."),
|
||||
createBundleConfig("src/schema/lib.ts", "./schema"),
|
||||
]
|
||||
|
||||
@@ -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 { EmptyObject } from "type-fest/source/empty-object"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
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 { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas"
|
||||
import { StaticMembers } from "../util"
|
||||
import { ZodSchemaObject } from "../traits/ZodSchemaObject"
|
||||
|
||||
|
||||
export class ZodSchemaClassBuilder<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
>
|
||||
extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
Schemas extends object,
|
||||
> {
|
||||
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
||||
|
||||
schema<
|
||||
Super extends AbstractClass<object, []> & { schema?: never, schemaWithDefaultValues?: never },
|
||||
constructor(
|
||||
protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
|
||||
protected readonly schemas: Schemas,
|
||||
) {}
|
||||
|
||||
|
||||
schema<
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
@@ -32,7 +32,7 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<Super, Traits>,
|
||||
this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
|
||||
|
||||
props: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
@@ -42,39 +42,31 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||
},
|
||||
) {
|
||||
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
||||
static readonly schema = props.schema
|
||||
static readonly schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
|
||||
const schema = props.schema
|
||||
const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
|
||||
|
||||
abstract class ZodSchemaObjectConstructor {
|
||||
constructor(values: Values) {
|
||||
super()
|
||||
Object.assign(this, values)
|
||||
}
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
Schemas as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<Super> & Values,
|
||||
ConstructorParameters<typeof Schemas>
|
||||
> &
|
||||
StaticMembers<Super> &
|
||||
StaticMembers<typeof Schemas>
|
||||
),
|
||||
this.expression
|
||||
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
|
||||
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues)),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
ZodSchemaObjectInstantiationSchemas,
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
],
|
||||
{ schema, schemaWithDefaultValues } as const,
|
||||
)
|
||||
}
|
||||
|
||||
jsonifiable<
|
||||
Super extends ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
|
||||
& { jsonifySchema?: never, dejsonifySchema?: never },
|
||||
Instance extends Values,
|
||||
S extends {
|
||||
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
|
||||
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
jsonifySchema?: never
|
||||
dejsonifySchema?: never
|
||||
},
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
@@ -97,10 +89,12 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<
|
||||
Super | ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
Traits
|
||||
>,
|
||||
this: ZodSchemaClassBuilder<Superclass, Traits, S | {
|
||||
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>,
|
||||
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>,
|
||||
jsonifySchema?: never
|
||||
dejsonifySchema?: never
|
||||
}>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (
|
||||
@@ -112,83 +106,28 @@ extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
},
|
||||
) {
|
||||
const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema)
|
||||
|
||||
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
}
|
||||
const jsonifySchema = props.jsonifySchema(this.schemas.schema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.schemas.schema)
|
||||
|
||||
return new this.constructor(
|
||||
JsonifiableSchemas as unknown as (
|
||||
AbstractClass<
|
||||
Instance & JsonifiableSchemas,
|
||||
|
||||
// TODO: for some reason, ConstructorParameters<Super> does not work here. Maybe try to find a fix?
|
||||
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>>
|
||||
> &
|
||||
StaticMembers<Super> &
|
||||
StaticMembers<typeof JsonifiableSchemas>
|
||||
this.expression.expresses(
|
||||
JsonifiableZodSchemaObject(
|
||||
this.schemas.schema,
|
||||
this.schemas.schemaWithDefaultValues,
|
||||
jsonifySchema,
|
||||
dejsonifySchema,
|
||||
)
|
||||
),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
JsonifiableZodSchemaObject,
|
||||
DejsonifiableZodSchemaObject,
|
||||
],
|
||||
{ ...this.schemas as S, jsonifySchema, dejsonifySchema } as const,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
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, {})
|
||||
|
||||
@@ -1,196 +1,196 @@
|
||||
import { expression } from "@thilawyn/traitify-ts"
|
||||
import { AbstractClass, Simplify } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass"
|
||||
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
|
||||
import { Extend, Override, StaticMembers } from "../util"
|
||||
// import { expression } from "@thilawyn/traitify-ts"
|
||||
// import { AbstractClass, Simplify } from "type-fest"
|
||||
// import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
// import { z } from "zod"
|
||||
// import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass"
|
||||
// import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
|
||||
// import { Extend, Override, StaticMembers } from "../util"
|
||||
|
||||
|
||||
export class ZodSchemaClassExtender<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Subclass extends AbstractClass<object>,
|
||||
> {
|
||||
declare ["constructor"]: typeof ZodSchemaClassExtender
|
||||
// export class ZodSchemaClassExtender<
|
||||
// Superclass extends AbstractClass<object>,
|
||||
// Subclass extends AbstractClass<object>,
|
||||
// > {
|
||||
// declare ["constructor"]: typeof ZodSchemaClassExtender
|
||||
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
readonly subclass: Subclass,
|
||||
) {}
|
||||
// constructor(
|
||||
// readonly superclass: Superclass,
|
||||
// readonly subclass: Subclass,
|
||||
// ) {}
|
||||
|
||||
|
||||
schema<
|
||||
Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
// schema<
|
||||
// Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
|
||||
SuperSchemaT extends z.ZodRawShape,
|
||||
SuperSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SuperSchemaCatchall extends z.ZodTypeAny,
|
||||
// SuperSchemaT extends z.ZodRawShape,
|
||||
// SuperSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SuperSchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SuperValues extends object,
|
||||
SuperDefaultValues extends Partial<SuperValues>,
|
||||
// SuperValues extends object,
|
||||
// SuperDefaultValues extends Partial<SuperValues>,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
// SchemaT extends z.ZodRawShape,
|
||||
// SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends SuperValues,
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClassExtender<
|
||||
Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
any
|
||||
>,
|
||||
// Values extends SuperValues,
|
||||
// DefaultValues extends Partial<Values>,
|
||||
// >(
|
||||
// this: ZodSchemaClassExtender<
|
||||
// Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
// any
|
||||
// >,
|
||||
|
||||
props: {
|
||||
schema: (schema: Super["schema"]) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
|
||||
},
|
||||
) {
|
||||
const schema = props.schema(this.superclass.schema)
|
||||
const defaultValues = props.defaultValues(this.superclass.defaultValues)
|
||||
// props: {
|
||||
// schema: (schema: Super["schema"]) => z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
// defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues
|
||||
// },
|
||||
// ) {
|
||||
// const schema = props.schema(this.superclass.schema)
|
||||
// const defaultValues = props.defaultValues(this.superclass.defaultValues)
|
||||
|
||||
class Schemas extends (this.superclass as AbstractClass<object, any[]>) {
|
||||
static readonly schema = schema
|
||||
static readonly defaultValues = defaultValues
|
||||
}
|
||||
// class Schemas extends (this.superclass as AbstractClass<object, any[]>) {
|
||||
// static readonly schema = schema
|
||||
// static readonly defaultValues = defaultValues
|
||||
// }
|
||||
|
||||
return new this.constructor(
|
||||
this.superclass as Super,
|
||||
// return new this.constructor(
|
||||
// this.superclass as Super,
|
||||
|
||||
Schemas as unknown as AbstractClass<
|
||||
Simplify<
|
||||
Extend<[InstanceType<Super>, Values]>
|
||||
>,
|
||||
// Schemas as unknown as AbstractClass<
|
||||
// Simplify<
|
||||
// Extend<[InstanceType<Super>, Values]>
|
||||
// >,
|
||||
|
||||
[values: Values]
|
||||
> &
|
||||
Simplify<
|
||||
Override<[
|
||||
StaticMembers<Super>,
|
||||
StaticMembers<typeof Schemas>,
|
||||
]>
|
||||
>
|
||||
)
|
||||
}
|
||||
// [values: Values]
|
||||
// > &
|
||||
// Simplify<
|
||||
// Override<[
|
||||
// StaticMembers<Super>,
|
||||
// StaticMembers<typeof Schemas>,
|
||||
// ]>
|
||||
// >
|
||||
// )
|
||||
// }
|
||||
|
||||
jsonifiable<
|
||||
/** Superclass jsonifiable schemas */
|
||||
Super extends JsonifiableZodSchemaAbstractClass<
|
||||
any,
|
||||
// jsonifiable<
|
||||
// /** Superclass jsonifiable schemas */
|
||||
// Super extends JsonifiableZodSchemaAbstractClass<
|
||||
// any,
|
||||
|
||||
SuperJsonifySchemaT,
|
||||
SuperJsonifySchemaUnknownKeys,
|
||||
SuperJsonifySchemaCatchall,
|
||||
// SuperJsonifySchemaT,
|
||||
// SuperJsonifySchemaUnknownKeys,
|
||||
// SuperJsonifySchemaCatchall,
|
||||
|
||||
SuperDejsonifySchemaT,
|
||||
SuperDejsonifySchemaUnknownKeys,
|
||||
SuperDejsonifySchemaCatchall,
|
||||
// SuperDejsonifySchemaT,
|
||||
// SuperDejsonifySchemaUnknownKeys,
|
||||
// SuperDejsonifySchemaCatchall,
|
||||
|
||||
SuperJsonifiedValues,
|
||||
SuperValues
|
||||
>,
|
||||
// SuperJsonifiedValues,
|
||||
// SuperValues
|
||||
// >,
|
||||
|
||||
SuperJsonifySchemaT extends z.ZodRawShape,
|
||||
SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SuperJsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
// SuperJsonifySchemaT extends z.ZodRawShape,
|
||||
// SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SuperJsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SuperDejsonifySchemaT extends z.ZodRawShape,
|
||||
SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
// SuperDejsonifySchemaT extends z.ZodRawShape,
|
||||
// SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SuperJsonifiedValues extends JsonifiableObject,
|
||||
SuperValues extends object,
|
||||
// SuperJsonifiedValues extends JsonifiableObject,
|
||||
// SuperValues extends object,
|
||||
|
||||
/** New schemas */
|
||||
Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
|
||||
// /** New schemas */
|
||||
// Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
|
||||
|
||||
SelfSchemaT extends z.ZodRawShape,
|
||||
SelfSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SelfSchemaCatchall extends z.ZodTypeAny,
|
||||
// SelfSchemaT extends z.ZodRawShape,
|
||||
// SelfSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// SelfSchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SelfValues extends object,
|
||||
SelfDefaultValues extends Partial<SelfValues>,
|
||||
// SelfValues extends object,
|
||||
// SelfDefaultValues extends Partial<SelfValues>,
|
||||
|
||||
/* New jsonifiable schemas */
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
// /* New jsonifiable schemas */
|
||||
// JsonifySchemaT extends z.ZodRawShape,
|
||||
// JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
// DejsonifySchemaT extends z.ZodRawShape,
|
||||
// DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
// DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
JsonifiedValues extends SuperJsonifiedValues,
|
||||
Values extends SelfValues,
|
||||
>(
|
||||
this: ZodSchemaClassExtender<
|
||||
Super | JsonifiableZodSchemaAbstractClass<
|
||||
any,
|
||||
// JsonifiedValues extends SuperJsonifiedValues,
|
||||
// Values extends SelfValues,
|
||||
// >(
|
||||
// this: ZodSchemaClassExtender<
|
||||
// Super | JsonifiableZodSchemaAbstractClass<
|
||||
// any,
|
||||
|
||||
SuperJsonifySchemaT,
|
||||
SuperJsonifySchemaUnknownKeys,
|
||||
SuperJsonifySchemaCatchall,
|
||||
// SuperJsonifySchemaT,
|
||||
// SuperJsonifySchemaUnknownKeys,
|
||||
// SuperJsonifySchemaCatchall,
|
||||
|
||||
SuperDejsonifySchemaT,
|
||||
SuperDejsonifySchemaUnknownKeys,
|
||||
SuperDejsonifySchemaCatchall,
|
||||
// SuperDejsonifySchemaT,
|
||||
// SuperDejsonifySchemaUnknownKeys,
|
||||
// SuperDejsonifySchemaCatchall,
|
||||
|
||||
SuperJsonifiedValues,
|
||||
SuperValues
|
||||
>,
|
||||
// SuperJsonifiedValues,
|
||||
// SuperValues
|
||||
// >,
|
||||
|
||||
Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
>,
|
||||
// Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
// >,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (
|
||||
schema: Self["schema"],
|
||||
jsonifySchema: Super["jsonifySchema"],
|
||||
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
// props: {
|
||||
// jsonifySchema: (
|
||||
// schema: Self["schema"],
|
||||
// jsonifySchema: Super["jsonifySchema"],
|
||||
// ) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
|
||||
dejsonifySchema: (
|
||||
schema: Self["schema"],
|
||||
dejsonifySchema: Super["dejsonifySchema"],
|
||||
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
},
|
||||
) {
|
||||
const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema)
|
||||
// dejsonifySchema: (
|
||||
// schema: Self["schema"],
|
||||
// dejsonifySchema: Super["dejsonifySchema"],
|
||||
// ) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
// },
|
||||
// ) {
|
||||
// const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema)
|
||||
// const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema)
|
||||
|
||||
class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
}
|
||||
// class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) {
|
||||
// static readonly jsonifySchema = jsonifySchema
|
||||
// readonly jsonifySchema = jsonifySchema
|
||||
// static readonly dejsonifySchema = dejsonifySchema
|
||||
// readonly dejsonifySchema = dejsonifySchema
|
||||
// }
|
||||
|
||||
return new this.constructor(
|
||||
this.superclass as Super,
|
||||
// return new this.constructor(
|
||||
// this.superclass as Super,
|
||||
|
||||
JsonifiableSchemas as unknown as AbstractClass<
|
||||
Simplify<
|
||||
Override<[InstanceType<Self>, JsonifiableSchemas]>
|
||||
>,
|
||||
// JsonifiableSchemas as unknown as AbstractClass<
|
||||
// Simplify<
|
||||
// Override<[InstanceType<Self>, JsonifiableSchemas]>
|
||||
// >,
|
||||
|
||||
ConstructorParameters<
|
||||
ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
>
|
||||
> &
|
||||
Simplify<
|
||||
Override<[
|
||||
StaticMembers<Self>,
|
||||
StaticMembers<typeof JsonifiableSchemas>,
|
||||
]>
|
||||
>,
|
||||
)
|
||||
}
|
||||
// ConstructorParameters<
|
||||
// ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
// >
|
||||
// > &
|
||||
// Simplify<
|
||||
// Override<[
|
||||
// StaticMembers<Self>,
|
||||
// StaticMembers<typeof JsonifiableSchemas>,
|
||||
// ]>
|
||||
// >,
|
||||
// )
|
||||
// }
|
||||
|
||||
|
||||
toClass() {
|
||||
return this.subclass
|
||||
}
|
||||
// toClass() {
|
||||
// return this.subclass
|
||||
// }
|
||||
|
||||
toExpressionBuilder() {
|
||||
return expression.extends(this.subclass)
|
||||
}
|
||||
}
|
||||
// toExpressionBuilder() {
|
||||
// return expression.extends(this.subclass)
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -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
1
src/schema/lib.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./jsonify"
|
||||
14
src/tests.ts
14
src/tests.ts
@@ -28,23 +28,21 @@ const exp = zodSchemaClass
|
||||
id: dejsonify.bigint(s.shape.id)
|
||||
}),
|
||||
})
|
||||
.toExpression()
|
||||
.expresses(MobXObservableZodSchemaObject)
|
||||
.build()
|
||||
|
||||
|
||||
@exp.staticImplements
|
||||
class User extends exp.extends implements Implements<typeof exp> {}
|
||||
|
||||
const test = User.schema.extend({
|
||||
prout: z.string()
|
||||
}).merge(
|
||||
User.schemaWithDefaultValues
|
||||
)
|
||||
|
||||
const inst = User.create({ id: 1n, name: "User" })
|
||||
const inst = User.create({ id: 1n, name: "User" }, )
|
||||
// console.log(inst.name)
|
||||
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()
|
||||
// .schema({
|
||||
|
||||
@@ -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()
|
||||
@@ -1,16 +1,16 @@
|
||||
import { trait } from "@thilawyn/traitify-ts"
|
||||
import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender"
|
||||
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||
// import { trait } from "@thilawyn/traitify-ts"
|
||||
// import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender"
|
||||
// import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
||||
|
||||
|
||||
export const ExtendableZodSchemaObject = trait
|
||||
.implement(Super => class ExtendableZodSchemaObject extends Super {
|
||||
static extend<
|
||||
Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>,
|
||||
>(
|
||||
this: Self
|
||||
) {
|
||||
return new ZodSchemaClassExtender(this, this)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
// export const ExtendableZodSchemaObject = trait
|
||||
// .implement(Super => class ExtendableZodSchemaObject extends Super {
|
||||
// static extend<
|
||||
// Self extends ZodSchemaClass<any, any, any, any, any, any, any, any, any>,
|
||||
// >(
|
||||
// this: Self
|
||||
// ) {
|
||||
// return new ZodSchemaClassExtender(this, this)
|
||||
// }
|
||||
// })
|
||||
// .build()
|
||||
|
||||
@@ -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()
|
||||
@@ -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 { z } from "zod"
|
||||
import { JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass"
|
||||
import { parseZodTypeEffect } from "../util"
|
||||
import { parseZodSchemaEffect } 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 {
|
||||
jsonify<
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
|
||||
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.parse(this, params)
|
||||
jsonify(params?: Partial<z.ParseParams>) {
|
||||
return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
|
||||
.jsonifySchema
|
||||
.parse(this, params)
|
||||
}
|
||||
|
||||
async jsonifyPromise<
|
||||
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: Values & JsonifiableZodSchemas<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
Values
|
||||
>,
|
||||
|
||||
params?: Partial<z.ParseParams>,
|
||||
) {
|
||||
return this.jsonifySchema.parseAsync(this, params)
|
||||
jsonifyPromise(params?: Partial<z.ParseParams>) {
|
||||
return (this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>)
|
||||
.jsonifySchema
|
||||
.parseAsync(this, params)
|
||||
}
|
||||
|
||||
jsonifyEffect<
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
jsonifyEffect(params?: Partial<z.ParseParams>) {
|
||||
return parseZodSchemaEffect(
|
||||
(this.constructor as unknown as ImplStatic<typeof JsonifiableZodSchemaObject>).jsonifySchema,
|
||||
this,
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
Values extends object,
|
||||
static dejsonify<
|
||||
Instance extends Values
|
||||
>(
|
||||
this: Values & JsonifiableZodSchemas<
|
||||
JsonifySchemaT,
|
||||
JsonifySchemaUnknownKeys,
|
||||
JsonifySchemaCatchall,
|
||||
|
||||
DejsonifySchemaT,
|
||||
DejsonifySchemaUnknownKeys,
|
||||
DejsonifySchemaCatchall,
|
||||
|
||||
JsonifiedValues,
|
||||
Values
|
||||
>,
|
||||
|
||||
this: (
|
||||
Class<Instance, [values: Values]> &
|
||||
ImplStatic<typeof JsonifiableZodSchemaObject>
|
||||
),
|
||||
values: JsonifiedValues,
|
||||
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()
|
||||
|
||||
122
src/traits/ZodSchemaObject.ts
Normal file
122
src/traits/ZodSchemaObject.ts
Normal 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
|
||||
>
|
||||
>
|
||||
)
|
||||
@@ -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()
|
||||
@@ -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.
|
||||
* @returns An Effect monad representing the parsing result.
|
||||
*/
|
||||
export const parseZodTypeEffect = <
|
||||
Output,
|
||||
Input,
|
||||
>(
|
||||
export const parseZodSchemaEffect = <Output, Input>(
|
||||
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
||||
...args: Parameters<typeof schema.safeParseAsync>
|
||||
) => pipe(
|
||||
|
||||
Reference in New Issue
Block a user