Recommend this page to a friend! |
Classes of Harcharan Singh | Node Input Validator | README.md | Download |
|
|
DownloadValidation Library[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Coverage Status][codecov-img]][codecov-url] [![David deps][david-image]][david-url] [![node version][node-image]][node-url] [travis-image]: https://api.travis-ci.org/bitnbytesio/node-input-validator.svg?branch=master [travis-url]: https://travis-ci.org/bitnbytesio/node-input-validator?branch=master [codecov-img]: https://codecov.io/gh/bitnbytesio/node-input-validator/branch/master/graph/badge.svg [codecov-url]: https://codecov.io/gh/bitnbytesio/node-input-validator [david-image]: https://david-dm.org/bitnbytesio/node-input-validator.svg?style=flat-square&branch=master [david-url]: https://david-dm.org/bitnbytesio/node-input-validator?branch=master [npm-image]: https://img.shields.io/npm/v/node-input-validator.svg?style=flat-square [npm-url]: https://www.npmjs.com/package/node-input-validator [node-image]: https://img.shields.io/badge/node.js-%3E=_8.16-green.svg?style=flat-square [node-url]: http://nodejs.org/download/ Validation library for node.js NIV (Node Input Validator) is a validation library for node.js. You can also extend library to add custom rules. Note: For use case of any rule, please check test cases, If you have any doubt or confusion with documentation or rule behaviour. Installation
Features
Usage
Simple Example
With in express application
With async-await
For Koa2Attach koa middleware
Then in controller
With custom inputs
With custom inputs and custom messages
In case you wants control over validator, Then use
This method (ctx.validator(inputs, rules, messages={})) also support same options as like ctx.validateObjects ValidationExample 1
Array ValidationExample 1
Example 2
Add or Modify messagesPlaceholder in messages, :attribute will be replaced with field/attribute name, :value with field value and :arg0, :arg1 ...n with arguments passed to rule. Add/Update rule based messages
Add custom messages
for message in another languageYou can easliy add messages in another language.
Set default language
Toggle Multiple Errors SupportBy default, Validator will run in bailable mode ie. break if some rule failed. You can change this behaviour: Globally
Now instead of breaking, it will continues apply other rules. Errors example
LocallyTo toggle multiple errors on specific instance only.
Set attribute nice/custom nameYou can also declare Nice Names / Custom Attributes names.
If your are editing other languages, set lang parameter.
In error messages you will get "phone number" instead of phone. For Example: In case required rule failed, Error message will be: The phone number field is mandatory. For current instance only
This will only change attribute names for current instance. Add your own custom validation rules
Example of using other fields in rule
Example of using async rules
Example of using rules as array
RulesYou can check test cases for rule usage/examples. required The field under validation cannot be left blank.
requiredIf:field,value The field under validation cannot be left blank, if provided seed value equals to provided value seed.
requiredNotIf:field,value The field under validation may left blank, if provided seed value equals to provided value seed.
requiredWith:field requiredWith:field,field,field The field under validation may required in case provided seed present.
requiredWithout:field requiredWithout:field,field,field The field under validation may left blank in case provided seed present.
accepted The field under validation must be yes, on, 1, or true. accepted:seeds The field under validation must be accepted if value exists in provided seed. after:YYYY-MM-DD The field under validation must be date after provided seed.
alpha alpha:locale The field under validation must be entirely alphabetic characters. alphaDash The field under validation may have alpha-numeric characters, as well as dashes and underscores. alphaNumeric alphaNumeric:locale The field under validation only contains letters and numbers. array The field under validation must be an array. arrayUnique <sub>Added in: v3.5</sub> The field under validation must be an array and must contains unique values. No need to use array rule. This rule will take care of that. arrayUniqueObjects:attributes <sub>Added in: v3.5</sub> The field under validation must be an array and should have objects with unique attributes as per seed. No need to use array rule. This rule will take care of that. ascii The field under validation only contains ascii characters. base64 The field under validation must be valid base64 encoded string. between:min,max The field under validation must be betwwen min and max seed. This will work with number valus as well as with arrays using array count. boolean boolean:custom The field under validation must be boolean (true, false, 'true', 'false', 0, 1, '0', '1') or in custom seed. contains:value The field under validation must contains provided seeds.
creditCard The field under validation must be valid credit card string. date The field under validation must be a valid date (YYYY-MM-DD). dateAfterToday:number,unit The field under validation must be a valid date after provided seed.
see moment docs(https://momentjs.com/docs/#/manipulating/add/) for supported units. dateBeforeToday:number,unit The field under validation must be a valid date before provided seed.
dateFormat:format The field under validation must match the given date format. Note: use array of rules style declaration to deal with colon (:) in time formats.
Check https://momentjs.com/docs/ for supported formats dateiso <sub>Added in: v3.6</sub> The field under validation must be a valid iso date. datetime <sub>Added in: v3.6</sub> The field under validation must match YYYY-MM-DD HH:mm:ss format. decimal The field under validation must be a decimal value. digits:length The field under validation must be numeric and must have an exact length. digitsBetween:min,max The field under validation must have a length between provided min and max values. domain The field under validation must a qualified domain. dimensions:seed <sub>Added in: v3.7</sub> The image under validation must meet the dimension constraints as specified by in seed.
Note: image mimes validation is required before. Available constraints are: minWidth, maxWidth, minHeight, maxHeight, width, height. email The field under validation must be formatted as an e-mail address. equals The field under validation must be equal to given value. gt:another_field <sub>Added in: v3.4</sub> The field under validation must be greater then another field value. This rule is for Numbers comparision. gte:another_field <sub>Added in: v3.4</sub> The field under validation must be greater or equals to another field value. This rule is for Numbers comparision. hash:algo The field under validation must be a valid hash as per provided seed.
Supported algorithms: md4, md5, sha1, sha256, sha384, sha512, ripemd128, ripemd160, tiger128, tiger160, tiger192, crc32, crc32b. hex The field under validation must be valid hex. hexColor The field under validation must be valid hex color code. in:a,b...n The field under validation must exist in the given list of values.
integer The field under validation must be an integer. ip The field under validation must be an IP address. iso8601 The field under validation must be valid Iso8601 date. json The field under validation must be a valid JSON string. length:max length:max,min <sub>Added in: v3.5</sub> The field under validation must be less then or equals to max seed provided in rule. In case of min,max seed, field under validation must be less or equals to max seed and less then min seed. Can only be used with strings|arrays or any other object that supports length (str.length) property. latLong The field under validation must be a valid latitude-longitude coordinate. lengthBetween:min,max The field under validation value length must be between provided values.
lt:another_field <sub>Added in: v3.4</sub> The field under validation must be less then another field value. This rule is for Numbers comparision. lte:another_field <sub>Added in: v3.4</sub> The field under validation must be less or equals to another field value. This rule is for Numbers comparision. macAddress The field under validation should be a valid Mac Address. max:seed The field under validation must be less than given value.
maxLength:seed The length of field under validation should be less than given value.
mime:seed The file under validation must have a MIME type corresponding to one of the listed extensions.
min The field under validation must be greater than given value. minLength The length of field under validation should be greater than given value. mongoId The field under validation should be a valid MongoDB ID. notContains:seed The field under validation may not contains provided seeds. notIn:seeds The field under validation must not exist in the given list of values. nullable The field under validation is required only is not left empty. numeric The field under validation must be numeric. phoneNumber The field under validation must be a valid phone number. regex The field under validation must match the given regular expression. Note: Currently regex rules break on using colon (:) or pipe delimiters. Use array of rules style declaration instead of string.
same The given field must match the field under validation. size:max size:max,min The file field under validation must have a file size matching the given maximum value or should be between size range. Supported unit sufix: b(Bytes),kb/k(KilloBytes),mb/m(MegaBytes),gb/g(GigaBytes).
sometimes The field under validation is required if present. string The field under validation must be string. url The field under validation must be a valid URL. Post Rules There is set of rules which can be used to validate constraints of whole input, rather than validity of singular fields.
Post validator errors are returned in the
any Any of the fields must be present in input. all All of the fields must be present in input. Typescript SupportPartial Support. |