0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
6 changed files with 3 additions and 574 deletions
Showing only changes of commit cb89de90c2 - Show all commits

View File

@@ -1,133 +0,0 @@
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 { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
import { ZodSchemaObject } from "../traits/ZodSchemaObject"
export class ZodSchemaClassBuilder<
Superclass extends AbstractClass<object>,
const Traits extends readonly Trait<any, any, any, any>[],
Schemas extends object,
> {
declare ["constructor"]: typeof ZodSchemaClassBuilder
constructor(
protected readonly expression: TraitExpressionBuilder<Superclass, Traits>,
protected readonly schemas: Schemas,
) {}
schema<
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>,
>(
this: ZodSchemaClassBuilder<Superclass, Traits, EmptyObject>,
props: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schemaWithDefaultValues: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
},
) {
const schema = props.schema
const schemaWithDefaultValues = props.schemaWithDefaultValues(props.schema)
abstract class ZodSchemaObjectConstructor {
constructor(values: Values) {
Object.assign(this, values)
}
}
return new this.constructor(
this.expression
.extends(ZodSchemaObjectConstructor as AbstractClass<Values, [values: Values]>)
.expresses(ZodSchemaObject(schema, schemaWithDefaultValues)),
{ schema, schemaWithDefaultValues } as const,
)
}
jsonifiable<
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,
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,
>(
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: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
dejsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
},
) {
const jsonifySchema = props.jsonifySchema(this.schemas.schema)
const dejsonifySchema = props.dejsonifySchema(this.schemas.schema)
return new this.constructor(
this.expression.expresses(
JsonifiableZodSchemaObject(
this.schemas.schema,
this.schemas.schemaWithDefaultValues,
jsonifySchema,
dejsonifySchema,
)
),
{ ...this.schemas as S, jsonifySchema, dejsonifySchema } as const,
)
}
toExpression() {
return this.expression
}
}
export const zodSchemaClass = new ZodSchemaClassBuilder(expression, {})

View File

@@ -1,196 +0,0 @@
// 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
// constructor(
// readonly superclass: Superclass,
// readonly subclass: Subclass,
// ) {}
// schema<
// Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
// SuperSchemaT extends z.ZodRawShape,
// SuperSchemaUnknownKeys extends z.UnknownKeysParam,
// SuperSchemaCatchall extends z.ZodTypeAny,
// SuperValues extends object,
// SuperDefaultValues extends Partial<SuperValues>,
// 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
// >,
// 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
// }
// return new this.constructor(
// this.superclass as Super,
// Schemas as unknown as AbstractClass<
// Simplify<
// Extend<[InstanceType<Super>, Values]>
// >,
// [values: Values]
// > &
// Simplify<
// Override<[
// StaticMembers<Super>,
// StaticMembers<typeof Schemas>,
// ]>
// >
// )
// }
// jsonifiable<
// /** Superclass jsonifiable schemas */
// Super extends JsonifiableZodSchemaAbstractClass<
// any,
// SuperJsonifySchemaT,
// SuperJsonifySchemaUnknownKeys,
// SuperJsonifySchemaCatchall,
// SuperDejsonifySchemaT,
// SuperDejsonifySchemaUnknownKeys,
// SuperDejsonifySchemaCatchall,
// SuperJsonifiedValues,
// SuperValues
// >,
// SuperJsonifySchemaT extends z.ZodRawShape,
// SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// SuperJsonifySchemaCatchall extends z.ZodTypeAny,
// SuperDejsonifySchemaT extends z.ZodRawShape,
// SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
// SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
// SuperJsonifiedValues extends JsonifiableObject,
// SuperValues extends object,
// /** New schemas */
// Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
// SelfSchemaT extends z.ZodRawShape,
// SelfSchemaUnknownKeys extends z.UnknownKeysParam,
// SelfSchemaCatchall extends z.ZodTypeAny,
// SelfValues extends object,
// SelfDefaultValues extends Partial<SelfValues>,
// /* 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,
// JsonifiedValues extends SuperJsonifiedValues,
// Values extends SelfValues,
// >(
// this: ZodSchemaClassExtender<
// Super | JsonifiableZodSchemaAbstractClass<
// any,
// SuperJsonifySchemaT,
// SuperJsonifySchemaUnknownKeys,
// SuperJsonifySchemaCatchall,
// SuperDejsonifySchemaT,
// SuperDejsonifySchemaUnknownKeys,
// SuperDejsonifySchemaCatchall,
// SuperJsonifiedValues,
// SuperValues
// >,
// Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
// >,
// 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)
// 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,
// 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>,
// ]>
// >,
// )
// }
// toClass() {
// return this.subclass
// }
// toExpressionBuilder() {
// return expression.extends(this.subclass)
// }
// }

View File

@@ -1,4 +1,5 @@
export { ZodSchemaClassBuilder, zodSchemaClass } from "./builders/ZodSchemaClassBuilder"
export { JsonifiableZodSchemaObject } from "./traits/JsonifiableZodSchemaObject"
export { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass"
export { ZodSchemaClass } from "./ZodSchemaClass"
export { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
export { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
export { ZodSchemaObject, ZodSchemaObjectTrait } from "./traits/ZodSchemaObject"

View File

@@ -1,78 +0,0 @@
import { AbstractClass, Class } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
export type JsonifiableZodSchemaClass<
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,
> = (
Class<Instance, [values: Values]> &
JsonifiableZodSchemas<
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>
)
export type JsonifiableZodSchemaAbstractClass<
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,
> = (
AbstractClass<Instance, [values: Values]> &
JsonifiableZodSchemas<
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
JsonifiedValues,
Values
>
)
export type JsonifiableZodSchemas<
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,
> = {
readonly jsonifySchema: z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
readonly dejsonifySchema: z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
}

View File

@@ -1,55 +0,0 @@
import { AbstractClass, Class } from "type-fest"
import { z } from "zod"
export type ZodSchemaClass<
Instance extends Values,
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>,
> = (
Class<Instance, [values: Values]> &
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
)
export type ZodSchemaAbstractClass<
Instance extends Values,
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>,
> = (
AbstractClass<Instance, [values: Values]> &
ZodSchemas<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
)
export type ZodSchemas<
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>,
> = {
readonly schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
readonly schemaWithDefaultValues: z.ZodObject<SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues>
}

View File

@@ -1,110 +0,0 @@
import { ImplStatic, expression } from "@thilawyn/traitify-ts"
import { Class } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { parseZodSchemaEffect } from "../util"
import { ZodSchemaObject } from "./ZodSchemaObject"
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 {
declare ["constructor"]: typeof JsonifiableZodSchemaObject
static readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
jsonify(params?: Partial<z.ParseParams>) {
return this.constructor.jsonifySchema.parse(this, params)
}
jsonifyPromise(params?: Partial<z.ParseParams>) {
return this.constructor.jsonifySchema.parseAsync(this, params)
}
jsonifyEffect(params?: Partial<z.ParseParams>) {
return parseZodSchemaEffect(
this.constructor.jsonifySchema,
this,
params,
)
}
static dejsonify<
Instance extends Values
>(
this: (
Class<Instance, [values: Values]> &
ImplStatic<typeof JsonifiableZodSchemaObject>
),
values: JsonifiedValues,
params?: Partial<z.ParseParams>,
) {
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()