extend
创建带有额外字段的新对象 Schema。同名字段会被新的 Schema 替换。
签名
ts
function extend<S, E>(schema: ObjectSchema<S>, fields: E): ObjectSchema<Omit<S, keyof E> & E>参数
| 参数 | 类型 | 说明 |
|---|---|---|
schema | ObjectSchema | 由 object 创建的 Schema |
fields | Record<string, Schema> | 新字段或对已有字段的替换 |
示例
新字段
ts
const User = object({ name: string(), email: pipe(string(), email()) });
const Admin = extend(User, {
role: oneOf(['admin', 'moderator']),
});
// { name: string; email: string; role: 'admin' | 'moderator' }字段使用不同的规则
ts
const StrongSignup = extend(Signup, {
password: pipe(string(), minLength(12)),
});