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
| Parameter | Type | Description |
|---|---|---|
schema | ObjectSchema | A schema created with object |
keys | string[] | 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' }