0.1.0 #1
@@ -1,2 +1,3 @@
|
|||||||
export * from "./SchemableClass"
|
export * from "./SchemableClass"
|
||||||
export * from "./makeSchemableClass"
|
export * from "./makeSchemableClass"
|
||||||
|
export * from "./newSchemable"
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
import { Effect, pipe } from "effect"
|
||||||
import { HasRequiredKeys } from "type-fest"
|
import { HasRequiredKeys } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { SchemableClass, SchemableConfig } from "."
|
import { SchemableClass, SchemableConfig } from "."
|
||||||
|
import { parseZodTypeEffect } from "./util"
|
||||||
|
|
||||||
|
|
||||||
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
type ParamsArgs = [] | [Partial<z.ParseParams>]
|
||||||
|
|
||||||
type NewEntityArgs<Input extends object> =
|
type NewSchemableArgs<Input extends object> =
|
||||||
HasRequiredKeys<Input> extends true
|
HasRequiredKeys<Input> extends true
|
||||||
? [Input, ...ParamsArgs]
|
? [Input, ...ParamsArgs]
|
||||||
: [] | [Input, ...ParamsArgs]
|
: [] | [Input, ...ParamsArgs]
|
||||||
@@ -16,6 +18,34 @@ export const newSchemable = <
|
|||||||
$Config extends SchemableConfig,
|
$Config extends SchemableConfig,
|
||||||
>(
|
>(
|
||||||
class_: C | SchemableClass<$Config>,
|
class_: C | SchemableClass<$Config>,
|
||||||
...[values, params]: NewEntityArgs<$Config["input"]>
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
||||||
) =>
|
) =>
|
||||||
new class_(class_.schemaWithDefaultValues.parse(values || {}, params))
|
new class_(class_.schemaWithDefaultValues.parse(values || {}, params)) as InstanceType<C>
|
||||||
|
|
||||||
|
export const newSchemablePromise = async <
|
||||||
|
C extends SchemableClass<$Config>,
|
||||||
|
$Config extends SchemableConfig,
|
||||||
|
>(
|
||||||
|
class_: C | SchemableClass<$Config>,
|
||||||
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
||||||
|
) =>
|
||||||
|
new class_(await class_.schemaWithDefaultValues.parseAsync(values || {}, params)) as InstanceType<C>
|
||||||
|
|
||||||
|
export const newSchemableEffect = <
|
||||||
|
C extends SchemableClass<$Config>,
|
||||||
|
$Config extends SchemableConfig,
|
||||||
|
>(
|
||||||
|
class_: C | SchemableClass<$Config>,
|
||||||
|
...[values, params]: NewSchemableArgs<$Config["input"]>
|
||||||
|
) => pipe(
|
||||||
|
parseZodTypeEffect<
|
||||||
|
z.output<typeof class_.schemaWithDefaultValues>,
|
||||||
|
z.input<typeof class_.schemaWithDefaultValues>
|
||||||
|
>(
|
||||||
|
class_.schemaWithDefaultValues,
|
||||||
|
values || {},
|
||||||
|
params,
|
||||||
|
),
|
||||||
|
|
||||||
|
Effect.map(values => new class_(values) as InstanceType<C>),
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { makeSchemableClass } from "."
|
import { makeSchemableClass, newSchemable } from "."
|
||||||
import { newSchemable } from "./newSchemable"
|
|
||||||
|
|
||||||
|
|
||||||
const UserSchema = z.object({
|
const UserSchema = z.object({
|
||||||
|
|||||||
Reference in New Issue
Block a user