LIVR specification contains the most common rules that every implementation should support.
Moreover, LIVR itself and this module have zero dependecies. It means:
- Lighter builds
- Easier to maintain high level of security
npm install --save livr livr-extra-rules
- iso_date (extended version)
Error code: 'NOT_IP'
Checks that the value is true or false
- True values:
- False values:
String values (except empty string) will force error "NOT_BOOLEAN".
Error code: 'NOT_BOOLEAN'
Сhecks the presence of the value and its correspondence to the specified value
field: 'is': 'some value'
Error codes: 'REQUIRED', 'NOT_ALLOWED_VALUE
Checks that the value is a credit card number with Lunh Algorithm
Error code: 'WRONG_CREDIT_CARD_NUMBER'
field1: 'uuid' // default v4field2: uuid: 'v1'field3: uuid: 'v2'field4: uuid: 'v3'field5: uuid: 'v4'field6: uuid: 'v5'
Error code: 'NOT_UUID'
Checks that the value looks like mongo object id
Error code: 'NOT_ID'
Checks that the value is a list and it contains required number of elements. You can pass exact number of elements required or a range.
Do not forget about "required" rule if you want the field to be required.
list1: 'required' list_length: 10 ; // List is required and should contain exactly 10 items,list2:list_length: 10;// List is not required but if it is present, it should contain exactly 10 itemslist3:list_length: 3 10;// List is not required but if it is present, it should has from 3 to 10 items
Error codes: 'FORMAT_ERROR', 'TOO_FEW_ITEMS', 'TOO_MANY_\ITEMS'
Checks that items in list are unique. if the value is not an array, the rule will return "FORMAT_ERROR". The rule will check string representations of the values and supports only primitive values. if the value is not primitive (array, object) then the rule will return 'INCOMPARABLE_ITEMS'
Error codes: 'FORMAT_ERROR', 'NOT_UNIQUE_ITEMS', 'INCOMPARABLE_ITEMS'
Checks that the value is a base64 string
field1: 'base64'; // by default, padding is requiredfield2:base64: 'relaxed';// padding is optional
Error code: 'MALFORMED_BASE64'
Checks the value is a md5 hash string
Error code: 'NOT_MD5'
This rule is compatible with the standard "iso_date" rule (and will redefine it) but allows you to pass extra params - "min" and "max" dates.
There are special dates: "current", "yesterday", "tomorrow". You can use them if you want to check that passed date is in the future or in the past.
date1: "iso_date"date2: "iso_date": min: "2017-10-15"date3: "iso_date": max: "2017-10-30"date4: "iso_date": min: "2017-10-15T15:30Z" max: "2017-10-30" format: "datetime"date5: "iso_date": min: "current" max: "tomorrow"date6: "iso_date": format: "datetime"
- "min" - can be iso8601 date, iso 8601 datetime, "current", "tomorrow", "yesterday".
- "max" - can be iso8601 date, iso 8601 datetime, "current", "tomorrow", "yesterday".
- "format" - can be "date", "datetime". (default "date")
If you pass only date (without time) to "min" or "max" and expected format of user's input is "datetime" then:
- "min" starts from the beginning of min date.
- "max" ends at the end of the max date.
If you pass the time along with the date, then you need to specify the time zone.
Error codes: 'WRONG_DATE', 'DATE_TOO_LOW', 'DATE_TOO_HIGH'
Checks that the value is present if another field is present and has value.
sendMeEmails: one_of: 0 1email: 'required_if': sendMeEmails: '1'
Example with JSON pointer:
address: nested_object:city: 'required'street: 'required'email: 'required_if': 'address/city': 'Kyiv'
You cannot access parent fields with JSON pointers here, only siblings and nested values.
Error code: 'REQUIRED'
How to add own rule?
if you want to add own rule, you will need:
- Create a new file for the rule in src/rules (see existing rules)
- Add rule to src/index.js
- Add positive tests to tests/test_suite/positive/your_rule_name/ (see existing tests)
- Add negative tests to tests/test_suite/negative/your_rule_name/ (see existing tests)
- Update this README!