Moved everything to legacy
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
64
src/legacy/tests.ts
Normal file
64
src/legacy/tests.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { z } from "zod"
|
||||
import { makeSchemableClass, newSchemable } from "."
|
||||
import { dejsonifyBigIntSchema, dejsonifySchemable, dejsonifySchemableSchema, jsonifyBigIntSchema, jsonifySchemableSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
|
||||
|
||||
const GroupSchema = z.object({
|
||||
/** Group ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Group name */
|
||||
name: z.string(),
|
||||
})
|
||||
|
||||
const GroupSchemableObject = makeSchemableClass({ schema: GroupSchema })
|
||||
|
||||
const GroupJsonifiableSchemableObject = makeJsonifiableSchemableClass(GroupSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id)
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(s.id)
|
||||
}),
|
||||
})
|
||||
|
||||
class Group extends GroupJsonifiableSchemableObject {}
|
||||
|
||||
|
||||
const UserSchema = z.object({
|
||||
/** User ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Name string */
|
||||
name: z.string(),
|
||||
|
||||
/** User group */
|
||||
group: z.instanceof(Group),
|
||||
})
|
||||
|
||||
const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
|
||||
|
||||
const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id),
|
||||
group: jsonifySchemableSchema(Group, s.group),
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(s.id),
|
||||
group: dejsonifySchemableSchema(Group, s.group),
|
||||
}),
|
||||
})
|
||||
|
||||
class User extends UserJsonifiableSchemableObject {}
|
||||
|
||||
|
||||
const group1 = new Group({ id: 1n, name: "Group 1" })
|
||||
|
||||
const user1 = new User({ id: 1n, name: "User 1", group: group1 })
|
||||
const user2 = newSchemable(User, { id: 2n, name: "User 2", group: group1 })
|
||||
|
||||
const jsonifiedUser2 = user2.jsonify()
|
||||
const dejsonifiedUser2 = dejsonifySchemable(User, jsonifiedUser2)
|
||||
console.log(dejsonifiedUser2)
|
||||
82
src/legacy/util.ts
Normal file
82
src/legacy/util.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { mapValues } from "lodash-es"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
/**
|
||||
* Represents the static members of a class.
|
||||
* @template C - The class type.
|
||||
*/
|
||||
export type StaticMembers<C> = {
|
||||
[Key in keyof C as Key extends "prototype" ? never : Key]: C[Key]
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes default values from a ZodObject schema and returns a new schema.
|
||||
*
|
||||
* @param schema - The ZodObject schema to process.
|
||||
* @returns A new ZodObject schema with default values removed.
|
||||
*/
|
||||
export const zodObjectRemoveDefaults = <
|
||||
T extends z.ZodRawShape,
|
||||
UnknownKeys extends z.UnknownKeysParam,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Output extends {},
|
||||
Input extends {},
|
||||
>(
|
||||
schema: z.ZodObject<
|
||||
T,
|
||||
UnknownKeys,
|
||||
Catchall,
|
||||
Output,
|
||||
Input
|
||||
>
|
||||
) =>
|
||||
schema.extend(zodShapeRemoveDefaults(schema.shape))
|
||||
|
||||
/**
|
||||
* Removes default values from a ZodObject shape and returns a new shape.
|
||||
*
|
||||
* @param shape - The ZodObject shape to process.
|
||||
* @returns A new shape with default values removed.
|
||||
*/
|
||||
export const zodShapeRemoveDefaults = <
|
||||
Shape extends z.ZodRawShape
|
||||
>(
|
||||
shape: Shape
|
||||
): {
|
||||
[K in keyof Shape]:
|
||||
Shape[K] extends z.ZodDefault<infer T>
|
||||
? T
|
||||
: Shape[K]
|
||||
} =>
|
||||
mapValues(shape, el =>
|
||||
el instanceof z.ZodDefault
|
||||
? el.removeDefault()
|
||||
: el
|
||||
)
|
||||
|
||||
|
||||
/**
|
||||
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
||||
*
|
||||
* @param schema - The ZodType 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,
|
||||
>(
|
||||
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
||||
...args: Parameters<typeof schema.safeParseAsync>
|
||||
) => pipe(
|
||||
Effect.promise(() => schema.safeParseAsync(...args)),
|
||||
|
||||
Effect.flatMap(response =>
|
||||
response.success
|
||||
? Effect.succeed(response.data)
|
||||
: Effect.fail(response.error)
|
||||
),
|
||||
)
|
||||
64
src/tests.ts
64
src/tests.ts
@@ -1,64 +0,0 @@
|
||||
import { z } from "zod"
|
||||
import { makeSchemableClass, newSchemable } from "."
|
||||
import { dejsonifyBigIntSchema, dejsonifySchemable, dejsonifySchemableSchema, jsonifyBigIntSchema, jsonifySchemableSchema, makeJsonifiableSchemableClass } from "./jsonifiable"
|
||||
|
||||
|
||||
const GroupSchema = z.object({
|
||||
/** Group ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Group name */
|
||||
name: z.string(),
|
||||
})
|
||||
|
||||
const GroupSchemableObject = makeSchemableClass({ schema: GroupSchema })
|
||||
|
||||
const GroupJsonifiableSchemableObject = makeJsonifiableSchemableClass(GroupSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id)
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(s.id)
|
||||
}),
|
||||
})
|
||||
|
||||
class Group extends GroupJsonifiableSchemableObject {}
|
||||
|
||||
|
||||
const UserSchema = z.object({
|
||||
/** User ID */
|
||||
id: z.bigint(),
|
||||
|
||||
/** Name string */
|
||||
name: z.string(),
|
||||
|
||||
/** User group */
|
||||
group: z.instanceof(Group),
|
||||
})
|
||||
|
||||
const UserSchemableObject = makeSchemableClass({ schema: UserSchema })
|
||||
|
||||
const UserJsonifiableSchemableObject = makeJsonifiableSchemableClass(UserSchemableObject, {
|
||||
jsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: jsonifyBigIntSchema(s.id),
|
||||
group: jsonifySchemableSchema(Group, s.group),
|
||||
}),
|
||||
|
||||
dejsonifySchema: ({ schema, s }) => schema.extend({
|
||||
id: dejsonifyBigIntSchema(s.id),
|
||||
group: dejsonifySchemableSchema(Group, s.group),
|
||||
}),
|
||||
})
|
||||
|
||||
class User extends UserJsonifiableSchemableObject {}
|
||||
|
||||
|
||||
const group1 = new Group({ id: 1n, name: "Group 1" })
|
||||
|
||||
const user1 = new User({ id: 1n, name: "User 1", group: group1 })
|
||||
const user2 = newSchemable(User, { id: 2n, name: "User 2", group: group1 })
|
||||
|
||||
const jsonifiedUser2 = user2.jsonify()
|
||||
const dejsonifiedUser2 = dejsonifySchemable(User, jsonifiedUser2)
|
||||
console.log(dejsonifiedUser2)
|
||||
|
||||
47
src/util.ts
47
src/util.ts
@@ -1,5 +1,4 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { mapValues } from "lodash-es"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
@@ -12,52 +11,6 @@ export type StaticMembers<C> = {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes default values from a ZodObject schema and returns a new schema.
|
||||
*
|
||||
* @param schema - The ZodObject schema to process.
|
||||
* @returns A new ZodObject schema with default values removed.
|
||||
*/
|
||||
export const zodObjectRemoveDefaults = <
|
||||
T extends z.ZodRawShape,
|
||||
UnknownKeys extends z.UnknownKeysParam,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Output extends {},
|
||||
Input extends {},
|
||||
>(
|
||||
schema: z.ZodObject<
|
||||
T,
|
||||
UnknownKeys,
|
||||
Catchall,
|
||||
Output,
|
||||
Input
|
||||
>
|
||||
) =>
|
||||
schema.extend(zodShapeRemoveDefaults(schema.shape))
|
||||
|
||||
/**
|
||||
* Removes default values from a ZodObject shape and returns a new shape.
|
||||
*
|
||||
* @param shape - The ZodObject shape to process.
|
||||
* @returns A new shape with default values removed.
|
||||
*/
|
||||
export const zodShapeRemoveDefaults = <
|
||||
Shape extends z.ZodRawShape
|
||||
>(
|
||||
shape: Shape
|
||||
): {
|
||||
[K in keyof Shape]:
|
||||
Shape[K] extends z.ZodDefault<infer T>
|
||||
? T
|
||||
: Shape[K]
|
||||
} =>
|
||||
mapValues(shape, el =>
|
||||
el instanceof z.ZodDefault
|
||||
? el.removeDefault()
|
||||
: el
|
||||
)
|
||||
|
||||
|
||||
/**
|
||||
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user