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
| Parameter | Type | Description |
|---|---|---|
fields | string[] | Names of sibling fields |
schema | Schema<T> | The value schema when it is provided |
Error codes
| Code | When | Default message |
|---|---|---|
required | The value is empty and the field is required | Required |
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