Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
js-formdata-validator
Advanced tools
JS Form Validator is a simple form data validation library for JavaScript. It provides a set of base rules for checking the type and value of various inputs, and allows you to define custom rules as well.
JS Form Validator is a simple form data validation library for JavaScript. It provides a set of base rules for checking the type and value of various inputs, and allows you to define custom rules as well.
To install JS Form Validator, use one of the following package managers:
npm install --save js-formdata-validator
pnpm add js-formdata-validator
// nuxt.config.js
// Add transpile
build: {
transpile: [/js-formdata-validator/],
},
To use JS Form Validator, import the Validator class and create a new instance, passing in an object with the following properties:
import { Validator } from "js-formdata-validator";
const formData = {
name: null,
deep: {
neested: {
object: {
value: null
}
}
},
arrayObject: [
{
objectName: "object a name"
},
{
objectName: null
},
{
objectName: "object c name"
},
]
};
const validator = new Validator({
formData: formData,
rules: {
name: ["required"],
"deep.nested.object.value": ["required"],
"arrayObject.*.objectName": ["required"]
},
});
// Validate the form data
await validator.validate();
// Check if the validation failed
if (validator.fail()) {
// Get the validation error messages
const error = validator.getErrorBag();
console.log(error); // Output: {name: ["The field is required."], "deep.nested.object.value": ["The field is required."], "arrayObject.1.objectName": ["The field is required."]}
}
JS Form Validator provides the following base rules for validating form data:
JS Form Validator provides extendable custom rule to be runs alongside base rules, heres the code example:
const formData = {
age: 25,
};
const validator = new Validator({
formData: formData,
rules: {
age: ["custom"],
},
}).mergeCustomRules({
custom(value) {
if (value === 25) {
return "Test Error";
}
},
});
// Validate the form data
await validator.validate();
// Check if the validation failed
if (validator.fail()) {
// Get the validation error messages
const error = validator.getErrorBag();
console.log(error);
}
We can also parse parameters to the custom rules
const formData = {
age: 25
};
const validator = new Validator({
formData: formData,
rules: {
age: ["ageBetween:26,50"]
},
})
validator.mergeCustomRules({
ageBetween(value, paramA, paramB) {
// paramA will be 26
// paramB will be 50
if (value < paramA || value > paramB) {
return `Age must be between ${paramA} - ${paramB}`;
}
},
});
Or set anonymous function inside the array rules
const formData = {
age: 25
};
const validator = new Validator({
formData: formData,
rules: {
age: [
(value) {
const min = 26
const max = 50
if (value < min || value > max) {
return `Age must be between ${min} - ${max}`;
}
}
]
},
})
It can also use async / await syntax to fetch data and wait it to be fetched from some external source
const formData = {
age: 25
};
const validator = new Validator({
formData: formData,
rules: {
age: [
async (value) {
const { min, max } await fetch('/path/to/your/api');
if (value < min || value > max) {
return `Age must be between ${min} - ${max}`;
}
}
]
},
})
FAQs
JS Form Validator is a simple form data validation library for JavaScript. It provides a set of base rules for checking the type and value of various inputs, and allows you to define custom rules as well.
We found that js-formdata-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.