@lemoncode/fonk-match-field-validator
v1.0.1
Published
Update description: This is a [fonk](https://github.com/Lemoncode/fonk) microlibrary that brings validation capabilities to validate if a field of a form matchs with another field in same form.
Downloads
120
Maintainers
Readme
fonk-match-field-validator
This is a fonk microlibrary that brings validation capabilities to:
- Validate if a field of a form matchs with another field in same form.
How to add it to an existing form validation schema:
We have the following form model:
const myFormValues = {
login: 'user1',
password: 1234,
confirmPassword: 1234,
};
We can add a matchField validation to the myFormValues
import { matchField } from '@lemoncode/fonk-match-field-validator';
const validationSchema = {
field: {
confirmPassword: [
{
validator: matchField.validator,
customArgs: { field: 'password' },
},
],
},
};
You can customize the error message displayed in two ways:
- Globally, replace the default error message in all validationSchemas (e.g. porting to spanish):
import { matchField } from '@lemoncode/fonk-match-field-validator';
matchField.setErrorMessage('El campo debe coincidir con {{field}}');
- Locally just override the error message for this validationSchema:
import { matchField } from '@lemoncode/fonk-match-field-validator';
const validationSchema = {
field: {
confirmPassword: [
{
validator: matchField.validator,
customArgs: { field: 'password' },
message: 'The field must match with {{field}}',
},
],
},
};
- Even if you have a nested field:
const myFormValues = {
user: {
name: 'user1',
password: 1234,
confirmPassword: 1234,
},
};
- Overriding the message:
import { matchField } from '@lemoncode/fonk-match-field-validator';
const validationSchema = {
field: {
'user.confirmPassword': [
{
validator: matchField.validator,
customArgs: { field: 'user.password' },
message: 'The field must match with {{field}}',
},
],
},
};
Please, refer to fonk to know more.
License
About Basefactor + Lemoncode
We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.
Basefactor, consultancy by Lemoncode provides consultancy and coaching services.
Lemoncode provides training services.
For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend