A set of extra asserts for validator.js.
Install the package via npm
:
$ npm install --save validator.js-asserts
Some asserts require manually installing some peer dependencies. Failing to do so will result in runtime errors, as packages are required dynamically.
Peer dependencies are not listed on package.json
because npm 2 considers them mandatory peer dependencies and, therefore, always installs them, while npm 3 follows a more consensual approach of optional peer dependencies, not installing them by default but generating warning and confusing error messages instead.
You should pin these package's peer dependencies to the ranges listed on the optionalPeerDependencies key of this package's manifest.
The following set of extra asserts are provided by this package:
- AbaRoutingNumber (requires
abavalidator
) - BankIdentifierCode (BIC)
- BigNumber (requires
bignumber.js
) - BigNumberEqualTo (requires
bignumber.js
) - BigNumberGreaterThan (requires
bignumber.js
) - BigNumberGreaterThanOrEqualTo (requires
bignumber.js
) - BigNumberLessThan (requires
bignumber.js
) - BigNumberLessThanOrEqualTo (requires
bignumber.js
) - Boolean
- Callback (requires
callback
) - CpfNumber (requires
cpf
) - CreditCard (requires
creditcard
) - CurpNumber (requires
curp
) - Date (requires
moment
for format validation only) - DateDiffGreaterThan (requires
moment
) - DateDiffGreaterThanOrEqualTo (requires
moment
) - DateDiffLessThan (requires
moment
) - DateDiffLessThanOrEqualTo (requires
moment
) - Email (requires
validator
) - EqualKeys
- Hash
- Integer
- InternationalBankAccountNumber (IBAN, requires
iban
) - Ip
- Iso3166Country (requires
isoc
) - Json
- NotEmpty
- NullOr
- NullOrDate
- NullOrBoolean
- NullOrString
- Phone (requires
google-libphonenumber
) - PlainObject
- RfcNumber (requires
validate-rfc
) - TaxpayerIdentificationNumber (TIN, requires
tin-validator
) - UkModulusChecking (requires
uk-modulus-checking
) - Uri (requires
urijs
) - UsSubdivision
- UsZipCode
- Uuid
Tests if the value is a valid ABA Routing Number.
Tests if the value is a valid Bank Identifier Code (BIC) as defined in the ISO-9362 standard.
Tests if the value is a valid BigNumber
.
Tests if a BigNumber
is equal to a given value.
value
(required)
Tests if a BigNumber
is greater than a given threshold.
threshold
(required)
Tests if a BigNumber
is greater than or equal to a given threshold.
threshold
(required)
Tests if a BigNumber
is less than a given threshold.
threshold
(required)
Tests if a BigNumber
is less than or equal to a given threshold.
threshold
(required)
Tests if the value is a boolean.
Allows you to add custom rules by giving a callback function and a custom class.
callback
(required) - the callback function.customClass
(required) - the name of the class.
Tests if the value is valid CPF number.
Tests if the value is a valid credit card number using the Luhn10 algorithm.
Tests if the value is valid CURP number.
Tests if the value is a valid date.
format
(optional) - the format in which the date must be in.
Tests if the difference between two dates is greater than a given threshold.
threshold
(required)options
absolute
- whether the comparison should use the absolute value of the measured difference.asFloat
- whether to return the difference rounded down or as float.fromDate
- the date where the diff is measured with. If omitted, defaults tonow
.unit
- the unit of the difference measurement (years
,months
,weeks
,days
,hours
,minutes
andseconds
).
Tests if the difference between two dates is greater than or equal to a given threshold.
threshold
(required)options
absolute
- whether the comparison should use the absolute value of the measured difference.asFloat
- whether to return the difference rounded down or as float.fromDate
- the date where the diff is measured with. If omitted, defaults tonow
.unit
- the unit of the difference measurement (years
,months
,weeks
,days
,hours
,minutes
andseconds
).
Tests if the difference between two dates is less than a given threshold.
threshold
(required)options
absolute
- whether the comparison should use the absolute value of the measured difference.asFloat
- whether to return the difference rounded down or as float.fromDate
- the date where the diff is measured with. If omitted, defaults tonow
.unit
- the unit of the difference measurement (years
,months
,weeks
,days
,hours
,minutes
andseconds
).
Tests if the difference between two dates is less than or equal to a given threshold.
threshold
(required)options
absolute
- whether the comparison should use the absolute value of the measured difference.asFloat
- whether to return the difference rounded down or as float.fromDate
- the date where the diff is measured with. If omitted, defaults tonow
.unit
- the unit of the difference measurement (years
,months
,weeks
,days
,hours
,minutes
andseconds
).
Tests if the value is a valid email.
Tests if the object has the exact given set of keys (missing or extra keys are not allowed).
keys
(optional) - the keys that the object being tested must equal. If none are defined, no keys will be allowed.
Tests if the value is a valid hash.
algorithm
(required) - the algorithm to test the hash for. Supported algorithms aresha1
,sha256
andsha512
.
Tests if the value is an integer.
Tests if the value is a valid International Bank Account Number (IBAN) as defined in the 13616-1 standard.
Tests if the value is a valid ip (v4 or v6).
Tests if the value is a valid ISO-3166 country by alpha-3 code, alpha-2 code, short name or uppercase name. All officially-assigned, transitionally-assigned and user-assigned codes are considered valid.
Tests if the value is valid json.
Tests if the value is not an empty (empty object, empty array, empty string, etc).
Tests if the value is a null
or validates agains the assert received as an argument.
Tests if the value is a null
or boolean
.
Tests if the value is a null
or string
, optionally within some boundaries.
boundaries
(optional) -max
and/ormin
boundaries to test the string for.
Tests if the phone is valid and optionally if it belongs to the given country. The phone can be in the national or E164 formats.
countryCode
(optional) - the ISO-3166 alpha-2 country code to test the phone validity in.
Tests if the value is a plain object.
Tests if the value is a valid RFC number.
Tests if the value is a valid Taxpayer Identification Number (TIN) as defined by the U.S. IRS.
Tests if the given accountNumber
and sortCode
represent a valid Faster Payment Account
.
Tests if the value is a valid uri
which must contain at least a protocol and a hostname.
constraints
(optional) - additional uri parts to test for (e.g.{ is: 'domain', protocol: 'https' }
).
Tests if the value is a valid US subdivision or not. By default, codes in the short ("alpha2", e.g. CA
) or full form (e.g. US-CA
) are allowed. All US subdivisions categories are supported: districts
(1), states
(50) and outlying
territories (6).
alpha2Only
(optional) - whether to restrict validation to the "alpha2" short code form only.categories
(optional) - a list of categories to restrict code validation to (e.g.['states', 'outlying']
).
Tests if the value is a valid US zip code.
Tests if the value is a valid uuid.
version
(optional) - the version to test the uuid for. Supported version are3
,4
and5
. Defaults to test forall
three if omitted.
The following is an example for the extra ip assert:
const Validator = require('validator.js').Validator;
const is = require('validator.js').Assert.extend(require('validator.js-asserts'));
const validator = new Validator();
// Validate ip `1.3.3.7`.
let violation = validator.validate('1.3.3.7', is.ip());
if (true === violation) {
console.log('"1.3.3.7" is a valid IP'); // => "1.3.3.7" is a valid IP
}
// Validate ip `foo`.
violation = validator.validate('foo', is.ip());
if (true !== violation) {
console.log('"foo" is not a valid IP. Violation:', violation[0].show());
// => "foo" is not a valid IP. Violation: { assert: 'Ip', value: 'foo' }
}
// Make the validation nullable.
violation = validator.validate(null, is.nullOr(is.ip()));
if (true === violation) {
console.log('null is null or a valid IP'); // => null is null or a valid IP
}
$ npm test
The release of a version is automated via the release GitHub workflow. Run it by clicking the "Run workflow" button.
MIT