Skip to content

partial ​

A new object schema where all fields (or the listed ones) are optional. The typical case is PATCH: an update can send only the changed fields.

Signature ​

ts
function partial<S, K extends keyof S = keyof S>(schema: ObjectSchema<S>, keys?: K[]): ObjectSchema<...>

Parameters ​

ParameterTypeDescription
schemaObjectSchemaA schema created with object
keysstring[]Which fields to make optional. Default: all

Notes ​

  • The original schema is not changed — a new one is returned.
  • If a field is present, its rules are checked as usual.
  • Defaults from optional are not applied: with PATCH a missing field shouldn't be overwritten with a default.
  • Object options (unknown) are preserved.

Examples ​

Create and update ​

ts
const CreateUser = object({
  name: pipe(string(), minLength(2)),
  email: pipe(string(), email()),
  password: pipe(string(), minLength(8)),
});

const UpdateUser = partial(CreateUser);

validate(UpdateUser, { name: 'Ivan' }).data; // { name: 'Ivan' }
validate(UpdateUser, { name: 'I' }).errors;  // { name: 'Must be at least 2 characters long' }

Only some fields ​

ts
const EditProfile = partial(CreateUser, ['password']);

See also ​