sameAs
The value equals a sibling field of the same object. Like Laravel's confirmed.
Signature
ts
function sameAs(field: string, msg?: Message): Action<unknown>Parameters
| Parameter | Type | Description |
|---|---|---|
field | string | Field name in the same object |
msg | Message | Custom error message. A string with {params} or a function |
Error codes
| Code | When | Default message |
|---|---|---|
sameAs | The values differ | Must match {field} |
You can change the texts: see Messages & i18n.
Notes
- Compared with the raw value of the field (before
trimand other transforms). If the field is trimmed, trim the confirmation too. - Works only within one
object. To compare with fields elsewhere usecheckandroot.
Example
Password confirmation
ts
const Signup = object({
password: pipe(string(), minLength(8)),
passwordConfirm: pipe(string(), sameAs('password', 'Passwords do not match')),
});See also
- check — comparing with other fields