2.0.1 • Published 8 years ago
js-validate v2.0.1
js-validate
Functional, extensible, input validation. Make sure the value you receive is EXACTLY what you expect.
Getting Started
Installation
npm install js-validateInstantiate
const Validator = require('js-validate');
const validate = new Validator;Validate against a single rule:
validate('123', 'min-length 2'); //--> trueValidate against multiple rules:
validate('123', ['min-length 2', 'number']); //-->trueCreate a validator alias (single-rule):
Validator.alias('short', 'max-length 3');Create a validator alias (multiple rules):
Validator.alias('account-number', [
'alphanumeric -',
'min-length 7',
'starts-with 000-',
'ends-with -00'
]);Create a custom rule:
Validator.rule('isOkay', (input) => {
return String(input).toLowerCase() === 'ok';
});Integrate other great validators:
const tin = require('tin-validator');
Validator.rule('ssn-ein-tin', tin.isValid);Test custom rules and aliases:
validate('ok', 'isOkay'); //--> true
validate('OK', ['isOkay', 'short']); //--> true
validate('000-23422823934-00', 'account-number'); //--> trueKeep this in mind when creating custom rules:
- Validator rules must return a pure boolean (true | false)
- The first parameter must be the user input
- Unlimited additional parameters are supported
- Custom rules are added to the default rule list so overrides are possible
- Custom rules may be used in conjunction with defaults with aliases
Built-in Rules
- alpha
- Input is composed entirely of alpha characters (A-Za-z)
validate('Abc', 'alpha'); //--> truevalidate('ABC123', 'alpha'); //--> false
- alphanumeric
- Input is contains only numbers and letters
validate('abc123', 'alphanumeric'); //--> true- Extensible to include other specified characters
validate('123 Main Street', 'alphanumeric'); //--> falsevalidate('123 Main Street', 'alphanumeric _space_'); //--> true
- boolean
- Input contains some variation of true or false
- Data type not considered
validate('true', 'boolean'); //--> truevalidate('false', boolean'); //--> truevalidate(FALSE, 'boolean'); //--> true
- capitals
- Input has a specified amount of capital letters
validate('JoJo', 'capitals 2'); //--> true
- ends-with
- Input ends with a certain phrase or character
validate('abc123', 'endsWith 123'); //--> truevalidate('555-888-0000', 'endsWith 0'); //--> true
- equals
- Input equals a given value.
validate('abc', 'equal abc'); //--> truevalidate('abc', 'equal ABC'); //--> falsevalidate('123', 'equal 123'); //--> true- ISSUE: Does not yet work with numbers
validate(123, 'equal 123'); //--> false
- length
- Input has exactly the amount of characters specified
validate(123, 'length 3'); //--> true- NOTICE: Leading zeros are not considered because leading zeros in Javascript are no longer base-10 numbers.
validate(000123, 'length 6'); //--> falsevalidate(000123, 'length 2'); //--> true- FIX by making a string before validating.
validate('000123', 'length 6'); //--> true
- matches
- Input superficially (non-typed) matches a given value
validate('123', 'matches 123'); //--> truevalidate(123, 'matches 123'); //--> true
- matchesExactly
- Input exactly matches a given value
validate('123', 'matches 123'); //--> true- NOTICE: will not validate numbers
validate(123, 'matches 123'); //--> false
- max
- Input is less than or equal to a given number
validate(123, 'max 125'); //--> true
- max-length
- Validates that the number of characters in an input is less-than or equal to a given value
validate(123, 'max-length 3'); //--> truevalidate(123, 'max-length 2'); //--> false
- min
- Validates that a given input is less than or equal to a minimum number
validate(123, 'min 122'); //--> true
- min-length
- Validates that a given input is more than or equal to a given number
validate(123, 'min-length 3'); //--> truevalidate(123, 'min-length 4'); //--> false
- number - Deprecation Warning (use numeric)
- Tests if an input is a number
validate('50.00'', 'number'); //--> truevalidate('50A', 'number'); //--> false
- numbers (Number Characters)
- Validates that an exact amount of numerals is present in the input
validate('#1 Fan', 'numbers 1'); //--> truevalidate('Catch 22', 'numbers 1'); //--> false
- numeric
- Input consists entirely of numerical characters
validate('123', 'numeric'); //--> truevalidate(123, 'numeric'); //--> truevalidate('ABC123', 'numeric'); //--> false
- specials
- Input contains at least a given number of special (non-alphanumeric) characters
validate('ABC123', 'specials 0'); //--> truevalidate('ABC*123', 'specials 1'); //--> truevalidate('Hello!!!', 'specials 3'); //--> true
- starts-with
- Input starts with a precise character or phrase
validate('$1', 'starts-with $'); //--> truevalidate('ABC123', 'starts-with ABC'); //--> truevalidate('ABC123', 'starts-with abc'); //--> false
Please contribute:
We need to greatly expand the rules, prune, and modify them. We are also open to changes in the API of this utility. Make and issue on github to connect.
2.0.1
8 years ago
1.4.0
8 years ago
1.3.0
8 years ago
1.2.0
8 years ago
1.1.2
8 years ago
0.6.0
10 years ago
0.5.1
10 years ago
0.4.2
10 years ago
0.4.1
10 years ago
0.4.0
10 years ago
0.3.1
10 years ago
0.3.0
10 years ago
0.2.2
10 years ago
0.2.1
10 years ago
0.1.1
10 years ago
0.1.0
10 years ago
0.0.3
11 years ago
0.0.2
11 years ago