Skip to content

egon-orm-workspace / @egon-orm/zod / castWith

Function: castWith() ​

Call Signature ​

ts
function castWith<S, Z>(
   target, 
   params, 
   schema
): Changeset<S, true>;

Defined in: zod/src/cast-with.ts:43

Casts params with the keys of a Zod object schema as the permitted fields, then validates the changeset with that schema: shorthand for cast(target, params, Object.keys(schema.shape)).validate(schema). Every key of the schema must be a field of the target.

Type Parameters ​

S ​

S extends AnySchema

Z ​

Z extends ZodObject<$ZodLooseShape, $strip>

Parameters ​

target ​

S

params ​

unknown

schema ​

ObjectOf<S, Z>

Returns ​

Changeset<S, true>

Example ​

ts
const SignUp = toZod(User, { mode: "insert", pick: ["email", "name"] }).extend({ email: z.string().email() });
const cs = castWith(User, params, SignUp);

Throws ​

TypeError when schema is not a Zod object schema.

Throws ​

QueryError when a key of the schema is not a field of the target.

Call Signature ​

ts
function castWith<S, New, Z>(
   target, 
   params, 
   schema
): Changeset<S, New>;

Defined in: zod/src/cast-with.ts:48

Casts params with the keys of a Zod object schema as the permitted fields, then validates the changeset with that schema: shorthand for cast(target, params, Object.keys(schema.shape)).validate(schema). Every key of the schema must be a field of the target.

Type Parameters ​

S ​

S extends ChangesetSource

New ​

New extends boolean

Z ​

Z extends ZodObject<$ZodLooseShape, $strip>

Parameters ​

target ​

Changeset<S, New>

params ​

unknown

schema ​

ObjectOf<S, Z>

Returns ​

Changeset<S, New>

Example ​

ts
const SignUp = toZod(User, { mode: "insert", pick: ["email", "name"] }).extend({ email: z.string().email() });
const cs = castWith(User, params, SignUp);

Throws ​

TypeError when schema is not a Zod object schema.

Throws ​

QueryError when a key of the schema is not a field of the target.

Call Signature ​

ts
function castWith<R, Z>(
   target, 
   params, 
   schema
): Changeset<RowSchema<R>, false>;

Defined in: zod/src/cast-with.ts:53

Casts params with the keys of a Zod object schema as the permitted fields, then validates the changeset with that schema: shorthand for cast(target, params, Object.keys(schema.shape)).validate(schema). Every key of the schema must be a field of the target.

Type Parameters ​

R ​

R extends object

Z ​

Z extends ZodObject<$ZodLooseShape, $strip>

Parameters ​

target ​

RowOnly<R>

params ​

unknown

schema ​

ObjectOf<RowSchema<R>, Z>

Returns ​

Changeset<RowSchema<R>, false>

Example ​

ts
const SignUp = toZod(User, { mode: "insert", pick: ["email", "name"] }).extend({ email: z.string().email() });
const cs = castWith(User, params, SignUp);

Throws ​

TypeError when schema is not a Zod object schema.

Throws ​

QueryError when a key of the schema is not a field of the target.

Call Signature ​

ts
function castWith<T, Z>(
   target, 
   params, 
   schema
): Changeset<Extract<T, AnySchema>, boolean>;

Defined in: zod/src/cast-with.ts:58

Casts params with the keys of a Zod object schema as the permitted fields, then validates the changeset with that schema: shorthand for cast(target, params, Object.keys(schema.shape)).validate(schema). Every key of the schema must be a field of the target.

Type Parameters ​

T ​

T extends object

Z ​

Z extends ZodObject<$ZodLooseShape, $strip>

Parameters ​

target ​

T & ClassOrRow<T>

params ​

unknown

schema ​

ObjectOf<Extract<T, AnySchema>, Z>

Returns ​

Changeset<Extract<T, AnySchema>, boolean>

Example ​

ts
const SignUp = toZod(User, { mode: "insert", pick: ["email", "name"] }).extend({ email: z.string().email() });
const cs = castWith(User, params, SignUp);

Throws ​

TypeError when schema is not a Zod object schema.

Throws ​

QueryError when a key of the schema is not a field of the target.