pick
创建只包含列出字段的新对象 Schema。
签名
ts
function pick<S, K extends keyof S>(schema: ObjectSchema<S>, keys: K[]): ObjectSchema<Pick<S, K>>参数
| 参数 | 类型 | 说明 |
|---|---|---|
schema | ObjectSchema | 由 object 创建的 Schema |
keys | string[] | 要保留的字段。TypeScript 会检查它们是否存在于 Schema 中 |
示例
使用邮箱和密码登录
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' }