Files
zod-schema-class/src/newSchemable.ts
2023-12-31 00:48:02 +01:00

18 lines
551 B
TypeScript

type ParamsArgs = [] | [Partial<z.ParseParams>]
type NewEntityArgs<Input extends object> =
HasRequiredKeys<Input> extends true
? [Input, ...ParamsArgs]
: [] | [Input, ...ParamsArgs]
export const newEntity = <
$Config extends EntityConfig,
Static extends EntityClassStatic<$Config>,
T extends Entity<$Config>,
>(
class_: EntityClass<$Config, Static, T>,
...[values, params]: NewEntityArgs<$Config["input"]>
) =>
new class_(class_.schemaWithDefaultValues.parse(values || {}, params))