@lemoncode/fonk-array-required-validator
v1.0.0
Published
This is a [fonk](https://github.com/Lemoncode/fonk) microlibrary that brings validation capabilities to validate if an array field of a form has items
Downloads
83
Maintainers
Readme
fonk-array-required-validator
This is a fonk microlibrary that brings validation capabilities to:
- Validate if an array field of a form has items
How to install it:
npm install @lemoncode/fonk-array-required-validator --save
How to add it to an existing form validation schema:
We have the following form model:
const myFormValues = {
product : 'shoes',
price: 20,
sizes: [36, 38, 41]
}
We can add a arrayRequired validation to the myFormValues
import { arrayRequired } from '@lemoncode/fonk-array-required-validator';
const validationSchema = {
field: {
sizes: [arrayRequired.validator],
},
};
We must specify the minimum number (1 by default) and optionally the maximum number of items allowed:
import { arrayRequired } from '@lemoncode/fonk-array-required-validator';
const validationSchema = {
field: {
sizes: [
{
validator: arrayRequired.validator,
customArgs: { minLength: 1, maxLength: 10 },
},
],
},
};
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 { arrayRequired } from '@lemoncode/fonk-array-required-validator';
arrayRequired.setErrorMessage('El campo debe de ser una lista');
- Locally just override the error message for this validationSchema:
import { arrayRequired } from '@lemoncode/fonk-array-required-validator';
const validationSchema = {
field: {
price: [
{
validator: arrayRequired.validator,
message: 'Error message only updated for the validation schema',
},
],
},
};
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