This commit is contained in:
@@ -35,7 +35,7 @@ export function extendSchemableClass<
|
|||||||
>,
|
>,
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
schema: (
|
schema: (props: {
|
||||||
schema: z.ZodObject<
|
schema: z.ZodObject<
|
||||||
ExtendSchemaT,
|
ExtendSchemaT,
|
||||||
ExtendSchemaUnknownKeys,
|
ExtendSchemaUnknownKeys,
|
||||||
@@ -43,7 +43,9 @@ export function extendSchemableClass<
|
|||||||
ExtendSchemaValues,
|
ExtendSchemaValues,
|
||||||
ExtendSchemaValues
|
ExtendSchemaValues
|
||||||
>
|
>
|
||||||
) => z.ZodObject<
|
|
||||||
|
shape: ExtendSchemaT
|
||||||
|
}) => z.ZodObject<
|
||||||
SchemaT,
|
SchemaT,
|
||||||
SchemaUnknownKeys,
|
SchemaUnknownKeys,
|
||||||
SchemaCatchall,
|
SchemaCatchall,
|
||||||
@@ -60,7 +62,10 @@ export function extendSchemableClass<
|
|||||||
: AbstractClass<T, Arguments>
|
: AbstractClass<T, Arguments>
|
||||||
)
|
)
|
||||||
|
|
||||||
const schema = props.schema(extend.schema)
|
const schema = props.schema({
|
||||||
|
schema: extend.schema,
|
||||||
|
shape: extend.schema.shape,
|
||||||
|
})
|
||||||
const defaultValues = props.defaultValues(extend.defaultValues)
|
const defaultValues = props.defaultValues(extend.defaultValues)
|
||||||
|
|
||||||
return class extends extend {
|
return class extends extend {
|
||||||
|
|||||||
213
src/jsonifiable/dejsonifySchemable.ts
Normal file
213
src/jsonifiable/dejsonifySchemable.ts
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
import { Effect, pipe } from "effect"
|
||||||
|
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { JsonifiableSchemableClass } from "."
|
||||||
|
import { parseZodTypeEffect } from "../util"
|
||||||
|
|
||||||
|
|
||||||
|
export function dejsonifySchemable<
|
||||||
|
C extends JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues 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,
|
||||||
|
>(
|
||||||
|
class_: C | JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
values: JsonifiedValues,
|
||||||
|
params?: Partial<z.ParseParams>,
|
||||||
|
) {
|
||||||
|
return new class_(
|
||||||
|
class_.dejsonifySchema.parse(values, params)
|
||||||
|
) as InstanceType<C>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function dejsonifySchemablePromise<
|
||||||
|
C extends JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues 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,
|
||||||
|
>(
|
||||||
|
class_: C | JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
values: JsonifiedValues,
|
||||||
|
params?: Partial<z.ParseParams>,
|
||||||
|
) {
|
||||||
|
return new class_(
|
||||||
|
await class_.dejsonifySchema.parseAsync(values, params)
|
||||||
|
) as InstanceType<C>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function dejsonifySchemableEffect<
|
||||||
|
C extends JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
|
Values extends {},
|
||||||
|
DefaultValues 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,
|
||||||
|
>(
|
||||||
|
class_: C | JsonifiableSchemableClass<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
|
||||||
|
Values,
|
||||||
|
DefaultValues,
|
||||||
|
|
||||||
|
JsonifySchemaT,
|
||||||
|
JsonifySchemaUnknownKeys,
|
||||||
|
JsonifySchemaCatchall,
|
||||||
|
|
||||||
|
DejsonifySchemaT,
|
||||||
|
DejsonifySchemaUnknownKeys,
|
||||||
|
DejsonifySchemaCatchall,
|
||||||
|
|
||||||
|
JsonifiedValues,
|
||||||
|
|
||||||
|
"Class"
|
||||||
|
>,
|
||||||
|
|
||||||
|
values: JsonifiedValues,
|
||||||
|
params?: Partial<z.ParseParams>,
|
||||||
|
) {
|
||||||
|
return pipe(
|
||||||
|
parseZodTypeEffect(class_.dejsonifySchema, values, params),
|
||||||
|
Effect.map(values => new class_(values) as InstanceType<C>),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from "./JsonifiableSchemableClass"
|
export * from "./JsonifiableSchemableClass"
|
||||||
|
export * from "./dejsonifySchemable"
|
||||||
export * from "./makeJsonifiableSchemableClass"
|
export * from "./makeJsonifiableSchemableClass"
|
||||||
|
|||||||
12
src/tests.ts
12
src/tests.ts
@@ -1,7 +1,7 @@
|
|||||||
import { pipeInto } from "ts-functional-pipe"
|
import { pipeInto } from "ts-functional-pipe"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
import { extendSchemableClass, makeSchemableClass, newSchemable } from "."
|
||||||
import { makeJsonifiableSchemableClass } from "./jsonifiable"
|
import { dejsonifySchemable, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||||
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable"
|
import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable"
|
||||||
|
|
||||||
|
|
||||||
@@ -49,11 +49,13 @@ class User extends pipeInto(
|
|||||||
|
|
||||||
|
|
||||||
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
const user1 = newSchemable(User, { id: 1n, name: "User" })
|
||||||
console.log(user1.jsonify())
|
const jsonifiedUser1 = user1.jsonify()
|
||||||
|
console.log(jsonifiedUser1)
|
||||||
|
console.log(dejsonifySchemable(User, jsonifiedUser1))
|
||||||
|
|
||||||
|
|
||||||
const UserWithPhone = extendSchemableClass(User, {
|
const UserWithPhone = extendSchemableClass(User, {
|
||||||
schema: schema => schema.extend({
|
schema: ({ schema }) => schema.extend({
|
||||||
phone: z.string()
|
phone: z.string()
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -66,5 +68,5 @@ const UserWithPhone = extendSchemableClass(User, {
|
|||||||
UserWithPhone.schema
|
UserWithPhone.schema
|
||||||
|
|
||||||
|
|
||||||
const user2 = newSchemable(UserWithPhone, { id: 1n, name: "User" })
|
// const user2 = newSchemable(UserWithPhone, { id: 1n, name: "User" })
|
||||||
console.log(user2.jsonify())
|
// console.log(user2.jsonify())
|
||||||
|
|||||||
Reference in New Issue
Block a user