pick
A new object schema with only the listed fields.
Signature
ts
function pick<S, K extends keyof S>(schema: ObjectSchema<S>, keys: K[]): ObjectSchema<Pick<S, K>>Parameters
| Parameter | Type | Description |
|---|---|---|
schema | ObjectSchema | A schema created with object |
keys | string[] | Fields to keep. TypeScript checks that they exist in the schema |
Example
Login with email and password
ts
const User = object({
name: string(),
email: pipe(string(), email()),
password: pipe(string(), minLength(8)),
});
const Login = pick(User, ['email', 'password']);
validate(Login, { email: 'a@a.ru', password: '12345678', name: 'x' }).data;
// { email: 'a@a.ru', password: '12345678' }