Skip to content

omit ​

A new object schema without the listed fields.

Signature ​

ts
function omit<S, K extends keyof S>(schema: ObjectSchema<S>, keys: K[]): ObjectSchema<Omit<S, K>>

Parameters ​

ParameterTypeDescription
schemaObjectSchemaA schema created with object
keysstring[]Fields to remove

Notes ​

By default unknown keys are removed from data (strip mode), so omit is also handy for API responses: run a database record through a schema without the password, and the password never reaches the client.

Example ​

Public profile ​

ts
const User = object({
  id: number(),
  name: string(),
  email: string(),
  password: string(),
});

const PublicUser = omit(User, ['password', 'email']);

parse(PublicUser, userFromDb); // { id: 1, name: 'Ivan' }

See also ​