Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
livr-extra-rules
Advanced tools
LIVR specification contains the most common rules that every implementation should support.
The module contains extra rules for JavaScript LIVR. It is absolutely ok for LIVR to have your own custom rules in your project. But there are some rules that are useful cross projects.
Moreover, LIVR itself and this module have zero dependecies. It means:
npm install --save livr livr-extra-rules
import LIVR from livr;
import extraRules from 'livr-extra-rules';
LIVR.Validator.registerDefaultRules(extraRules);
You can use these rules with AsyncValidator as well
import LIVR from livr/async;
import extraRules from 'livr-extra-rules';
LIVR.AsyncValidator.registerDefaultRules(extraRules);
Example:
{
field: 'ipv4';
}
Error code: 'NOT_IP'
Checks that the value is true or false
true
, 1
, '1'
false
, 0
, '0'
String values (except empty string) will force error "NOT_BOOLEAN".
Return value will converted to JavaScript boolean values - true
or false
Example:
{
field: 'boolean';
}
Error code: 'NOT_BOOLEAN'
Сhecks the presence of the value and its correspondence to the specified value
Example:
{
field: { 'is': 'some value' }
}
Error codes: 'REQUIRED', 'NOT_ALLOWED_VALUE
Checks that the value is a credit card number with Lunh Algorithm
Example:
{
field: 'credit_card';
}
Error code: 'WRONG_CREDIT_CARD_NUMBER'
Example:
{
field1: 'uuid', // default v4
field2: {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
Example:
{
field: 'mongo_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.
Example:
{
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 items
list3: {
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'
Example:
{
list: 'list_items_unique';
}
Error codes: 'FORMAT_ERROR', 'NOT_UNIQUE_ITEMS', 'INCOMPARABLE_ITEMS'
Checks that the value is a base64 string
Example:
{
field1: 'base64'; // by default, padding is required
field2: {
base64: 'relaxed';
} // padding is optional
}
Error code: 'MALFORMED_BASE64'
Checks the value is a md5 hash string
Example:
{
field: 'md5';
}
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.
Example:
{
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"} },
}
Supported options:
If you pass only date (without time) to "min" or "max" and expected format of user's input is "datetime" then:
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.
Simple example:
{
sendMeEmails: { one_of: [0, 1] },
email: { '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'
Checks that the value is a instaceof a class. This rule is JS specific and not serializable but can be useful for runtime validations
Example:
class Dog {}
{
dog1: {'instance_of': Dog};
}
Error code: 'WRONG_INSTANCE'
Checks that the value is an object which has all of required methods. This rule is JS specific and not serializable but can be useful for runtime validations
Example:
{
dog1: {'has_methods': 'bark'};
dog2: {'has_methods': ['bark', 'getName']};
}
Error code: 'NOT_HAVING_METHOD [${method}]' like 'NOT_HAVING_METHOD [bark]'
if you want to add own rule, you will need:
FAQs
Extra rules for LIVR Validator
We found that livr-extra-rules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.