Skip to content

extend ​

A new object schema with additional fields. A field with the same name is replaced with the new schema.

Signature ​

ts
function extend<S, E>(schema: ObjectSchema<S>, fields: E): ObjectSchema<Omit<S, keyof E> & E>

Parameters ​

ParameterTypeDescription
schemaObjectSchemaA schema created with object
fieldsRecord<string, Schema>New fields or replacements for existing ones

Examples ​

New fields ​

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' }

Different rules for a field ​

ts
const StrongSignup = extend(Signup, {
  password: pipe(string(), minLength(12)),
});

See also ​