validatify
v1.0.2
Published
Extremely simple form and input validation for Node.js
Maintainers
Readme
Extremely simple form and input validation for Node.js
Install
npm install validatify --saveUsage
var Form = require("validatify");Example
// Initialize a new Form
var myForm = new Form([{
name: "email",
validators: {
required: true,
email: true
}
}, {
name: "password",
validators: {
required: true,
minlength: 8
}
}, {
name: "exampleField",
validators: {
required: true,
containsString: "string to test for"
}
}]);
// Add a custom validator to myForm
myForm.validator("containsString", function (value, string) {
return value.indexOf(string) > -1;
});
// Listen for "valid" event
myForm.on("valid", function (result) {
console.log(result);
});
// Listen for "invalid" event
myForm.on("invalid", function (result) {
// Should not run, as the values we are providing are valid.
console.log(result);
});
// Run validation
myForm.validate({
email: "[email protected]",
password: "this is a string over 8 characters long",
exampleField: "this field contains the string to test for"
});Chaining
All functions support chaining, so you can write your code however you want!
new Form([{
name: "email",
validators: {
required: true,
email: true
}
}])
.on("valid", function (result) {
console.log(result);
})
.on("invalid", function (result) {
// should not be called because the email is valid
console.log(result);
})
.validate({email: "[email protected]"});API
Form(fields)
fields- An array containing all the fields to iterate over and validate.Example
[ { name: "email", validators: { required: true, email: true } }, { name: "password", validators: { required: true, minlength: 8 } } ]
.validate(values, continueOnFail)
values- An object containing the values for all the fields passed to the constructor.continueOnFail- Optional boolean that when true executes all validators, and does not stop at first failed validatorExample
{ email: "[email protected]", password: "an example password", }
.validateField(field, value, continueOnFail)
field- Name of the field we want to validate.Example
"email"value- Value of the field specified.Example
"[email protected]"continueOnFail- Optional boolean that when true executes all validators, and does not stop at first failed validator
.validator(name, validator)
name- Name of the validator to add (this can also be used to overwrite default validators)validator- Function that should return either true or false based on the value and argumentExample
new Form([ { name: "someField", validators: { newValidator: "this is the expectedValue argument" } } ]) .validator("newValidator", function (value, expectedValue) { return value == expectedValue; }) .on("invalid", function (result) { // This will run, because the input will fail our new validator's check. console.log(result); }) .validateField("someField", "this is not the expected string")
.on(event, callback)
event- Name of the event to listen for, can be any of the below:validvalid fieldinvalidinvalid field
callback- Function that only gets called on any eventExample
function (result) { console.log(result); }
Validators
These are the default validators, however you can add your own easily. (Here's how).
Usage
This is an example of what you would pass to the Form constructor.
[
{
name: "someField",
validators: {
validatorName: validatorArgument
}
}
]Validator names and usages
number- Checks if input is a number Sourceboolean- Checks if input is either "true" or "false" as a string Sourcestring- Checks if input is a string Sourceinteger- Checks if input is a number without decimals Sourcefloat- Checks if input is a number with decimals Sourcealphanumeric- Checks if input is a string and contains only alphanumeric characters Sourceemail- Checks if input is an email address Sourcedefined- Checks if input is defined Sourcerequired- Checks if value is defined and not empty Sourcerange- Checks if value is a number within a range defined as an array Sourcemin- Checks if value is a number more than or equal to the min setpoint Sourcemax- Checks if value is a number less than or equal to the max setpoint Sourceminlength- Checks if value as a string has a length more than or equal to the min setpoint Sourcemaxlength- Checks if value as a string has a length less than or equal to the max setpoint Sourcelength- Checks if value as a string has a length equal to the setpoint Sourceregex- Checks if input matches a regex Source
