Skip to content

requiredWithout ​

The field is required if at least one of the listed fields is not filled. Like required_without in Laravel — e.g. "phone or email".

Signature ​

ts
function requiredWithout<T>(fields: string[], schema: Schema<T>): Schema<T | undefined>

Parameters ​

ParameterTypeDescription
fieldsstring[]Names of sibling fields
schemaSchema<T>The value schema when it is provided

Error codes ​

CodeWhenDefault message
requiredThe value is empty and the field is requiredRequired

You can change the texts: see Messages & i18n.

Example ​

Phone or email ​

ts
const Contact = object({
  phone: requiredWithout(['email'], string()),
  email: requiredWithout(['phone'], pipe(string(), email())),
});

validate(Contact, {}).errors;                  // { phone: 'Required', email: 'Required' }
validate(Contact, { email: 'a@a.ru' }).valid;  // true

See also ​