fastest-validator
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -0,1 +1,4 @@ | ||
const Benchmarkify = require("benchmarkify"); | ||
Benchmarkify.printHeader("fastest-validator benchmark"); | ||
require("./suites/" + (process.argv[2] || "simple")); |
@@ -40,3 +40,3 @@ "use strict"; | ||
bench.skip("compile & validate", () => { | ||
bench.add("compile & validate", () => { | ||
let res = v.validate(obj, schema); | ||
@@ -62,2 +62,28 @@ if (res !== true) | ||
bench.run(); | ||
bench.run(); | ||
/* | ||
=============================== | ||
fastest-validator benchmark | ||
=============================== | ||
Platform info: | ||
============== | ||
Windows_NT 6.1.7601 x64 | ||
Node.JS: 6.10.0 | ||
V8: 5.1.281.93 | ||
Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz × 8 | ||
Suite: Simple object | ||
√ compile & validate x 249,659 ops/sec ±0.17% (95 runs sampled) | ||
√ validate with pre-compiled schema x 3,111,667 ops/sec ±0.92% (90 runs sampled) | ||
√ validate with wrong obj x 767,201 ops/sec ±0.91% (92 runs sampled) | ||
compile & validate -91.98% (249,659 ops/sec) | ||
validate with pre-compiled schema 0.00% (3,111,667 ops/sec) | ||
validate with wrong obj -75.34% (767,201 ops/sec) | ||
----------------------------------------------------------------------- | ||
*/ |
"use strict"; | ||
module.exports = { | ||
required: "The '{name}' field is required!", | ||
required: "The '{field}' field is required!", | ||
string: "The '{name}' field must be a string!", | ||
stringEmpty: "The '{name}' field must not be empty!", | ||
stringMin: "The '{name}' field length must be larger than or equal to {0} characters long!", | ||
stringMax: "The '{name}' field length must be less than or equal to {0} characters long!", | ||
stringLength: "The '{name}' field length must be {0} characters long!", | ||
stringPattern: "The '{name}' field fails to match the required pattern!", | ||
stringContains: "The '{name}' field must contain the '{0}' text!", | ||
stringEnum: "The '{name}' field does not match any of the allowed values!", | ||
string: "The '{field}' field must be a string!", | ||
stringEmpty: "The '{field}' field must not be empty!", | ||
stringMin: "The '{field}' field length must be larger than or equal to {expected} characters long!", | ||
stringMax: "The '{field}' field length must be less than or equal to {expected} characters long!", | ||
stringLength: "The '{field}' field length must be {expected} characters long!", | ||
stringPattern: "The '{field}' field fails to match the required pattern!", | ||
stringContains: "The '{field}' field must contain the '{expected}' text!", | ||
stringEnum: "The '{field}' field does not match any of the allowed values!", | ||
number: "The '{name}' field must be a number!", | ||
numberMin: "The '{name}' field must be larger than or equal to {0}!", | ||
numberMax: "The '{name}' field must be less than or equal to {0}!", | ||
numberEqual: "The '{name}' field must be equal with {0}!", | ||
numberNotEqual: "The '{name}' field can't be equal with {0}!", | ||
numberInteger: "The '{name}' field must be an integer!", | ||
numberPositive: "The '{name}' field must be a positive number!", | ||
numberNegative: "The '{name}' field must be a negative number!", | ||
number: "The '{field}' field must be a number!", | ||
numberMin: "The '{field}' field must be larger than or equal to {expected}!", | ||
numberMax: "The '{field}' field must be less than or equal to {expected}!", | ||
numberEqual: "The '{field}' field must be equal with {expected}!", | ||
numberNotEqual: "The '{field}' field can't be equal with {expected}!", | ||
numberInteger: "The '{field}' field must be an integer!", | ||
numberPositive: "The '{field}' field must be a positive number!", | ||
numberNegative: "The '{field}' field must be a negative number!", | ||
array: "The '{name}' field must be an array!", | ||
arrayEmpty: "The '{name}' field must not be an empty array!", | ||
arrayMin: "The '{name}' field must contain at least {0} items!", | ||
arrayMax: "The '{name}' field must contain less than or equal to {0} items!", | ||
arrayLength: "The '{name}' field must contain {0} items!", | ||
arrayContains: "The '{name}' field must contain the '{0}' item!", | ||
arrayEnum: "The '{name} field value '{0}' does not match any of the allowed values!", | ||
array: "The '{field}' field must be an array!", | ||
arrayEmpty: "The '{field}' field must not be an empty array!", | ||
arrayMin: "The '{field}' field must contain at least {expected} items!", | ||
arrayMax: "The '{field}' field must contain less than or equal to {expected} items!", | ||
arrayLength: "The '{field}' field must contain {expected} items!", | ||
arrayContains: "The '{field}' field must contain the '{expected}' item!", | ||
arrayEnum: "The '{field} field value '{expected}' does not match any of the allowed values!", | ||
boolean: "The '{name}' field must be a boolean!", | ||
boolean: "The '{field}' field must be a boolean!", | ||
function: "The '{name}' field must be a function!", | ||
function: "The '{field}' field must be a function!", | ||
date: "The '{name}' field must be a Date!", | ||
dateMin: "The '{name}' field must be larger than or equal to {0}!", | ||
dateMax: "The '{name}' field must be less than or equal to {0}", | ||
date: "The '{field}' field must be a Date!", | ||
dateMin: "The '{field}' field must be larger than or equal to {expected}!", | ||
dateMax: "The '{field}' field must be less than or equal to {expected}!", | ||
forbidden: "The '{name}' field is forbidden!", | ||
forbidden: "The '{field}' field is forbidden!", | ||
email: "The '{name}' field must be a valid e-mail!" | ||
email: "The '{field}' field must be a valid e-mail!" | ||
}; |
@@ -15,7 +15,7 @@ "use strict"; | ||
if (schema.min != null && arrLength < schema.min) { | ||
return this.makeError("arrayMin", [schema.min, arrLength]); | ||
return this.makeError("arrayMin", schema.min, arrLength); | ||
} | ||
if (schema.max != null && arrLength > schema.max) { | ||
return this.makeError("arrayMax", [schema.max, arrLength]); | ||
return this.makeError("arrayMax", schema.max, arrLength); | ||
} | ||
@@ -25,7 +25,7 @@ | ||
if (schema.length != null && arrLength !== schema.length) { | ||
return this.makeError("arrayLength",[schema.length, arrLength]); | ||
return this.makeError("arrayLength", schema.length, arrLength); | ||
} | ||
if (schema.contains != null && value.indexOf(schema.contains) === -1) { | ||
return this.makeError("arrayContains", [schema.contains]); | ||
return this.makeError("arrayContains", schema.contains); | ||
} | ||
@@ -36,3 +36,3 @@ | ||
if (schema.enum.indexOf(value[i]) === -1) { | ||
return this.makeError("arrayEnum", [value[i], schema.enum]); | ||
return this.makeError("arrayEnum", value[i], schema.enum); | ||
} | ||
@@ -39,0 +39,0 @@ } |
@@ -17,7 +17,7 @@ "use strict"; | ||
if (schema.min != null && value < schema.min) { | ||
return this.makeError("numberMin", [schema.min, value]); | ||
return this.makeError("numberMin", schema.min, value); | ||
} | ||
if (schema.max != null && value > schema.max) { | ||
return this.makeError("numberMax", [schema.max, value]); | ||
return this.makeError("numberMax", schema.max, value); | ||
} | ||
@@ -27,3 +27,3 @@ | ||
if (schema.equal != null && value !== schema.equal) { | ||
return this.makeError("numberEqual", [schema.equal, value]); | ||
return this.makeError("numberEqual", schema.equal, value); | ||
} | ||
@@ -33,3 +33,3 @@ | ||
if (schema.notEqual != null && value === schema.notEqual) { | ||
return this.makeError("numberNotEqual", [schema.notEqual]); | ||
return this.makeError("numberNotEqual", schema.notEqual); | ||
} | ||
@@ -39,3 +39,3 @@ | ||
if (schema.integer === true && value % 1 !== 0) { | ||
return this.makeError("numberInteger", [value]); | ||
return this.makeError("numberInteger", value); | ||
} | ||
@@ -45,3 +45,3 @@ | ||
if (schema.positive === true && value <= 0) { | ||
return this.makeError("numberPositive", [value]); | ||
return this.makeError("numberPositive", value); | ||
} | ||
@@ -51,3 +51,3 @@ | ||
if (schema.negative === true && value >= 0) { | ||
return this.makeError("numberNegative", [value]); | ||
return this.makeError("numberNegative", value); | ||
} | ||
@@ -54,0 +54,0 @@ |
@@ -22,23 +22,23 @@ "use strict"; | ||
if (schema.min != null && valueLength < schema.min) { | ||
return this.makeError("stringMin", [schema.min, valueLength]); | ||
return this.makeError("stringMin", schema.min, valueLength); | ||
} | ||
if (schema.max != null && valueLength > schema.max) { | ||
return this.makeError("stringMax", [schema.max, valueLength]); | ||
return this.makeError("stringMax", schema.max, valueLength); | ||
} | ||
if (schema.length != null && valueLength !== schema.length) { | ||
return this.makeError("stringLength", [schema.length, valueLength]); | ||
return this.makeError("stringLength", schema.length, valueLength); | ||
} | ||
if (schema.pattern != null && !schema.pattern.test(value)) { | ||
return this.makeError("stringPattern", [schema.pattern ]); | ||
return this.makeError("stringPattern", schema.pattern ); | ||
} | ||
if (schema.contains != null && value.indexOf(schema.contains) === -1) { | ||
return this.makeError("stringContains", [schema.contains]); | ||
return this.makeError("stringContains", schema.contains); | ||
} | ||
if (schema.enum != null && schema.enum.indexOf(value) === -1) { | ||
return this.makeError("stringEnum", [schema.enum]); | ||
return this.makeError("stringEnum", schema.enum); | ||
} | ||
@@ -45,0 +45,0 @@ |
@@ -192,3 +192,3 @@ "use strict"; | ||
if (!err.message) | ||
err.message = this.resolveMessage(err.type, err.field, err.args); | ||
err.message = this.resolveMessage(err); | ||
@@ -203,8 +203,10 @@ errors.push(err); | ||
* @param {String} type | ||
* @param {Array} args | ||
* @param {Any} expected | ||
* @param {Any} actual | ||
*/ | ||
Validator.prototype.makeError = function(type, args) { | ||
Validator.prototype.makeError = function(type, expected, actual) { | ||
return { | ||
type: type, | ||
args: args || [] | ||
expected: expected, | ||
actual: actual | ||
}; | ||
@@ -216,14 +218,8 @@ }; | ||
* | ||
* @param {String} type | ||
* @param {String} field | ||
* @param {Array} args | ||
* @param {Object} err Validation error object | ||
*/ | ||
Validator.prototype.resolveMessage = function(type, field, args) { | ||
let msg = this.messages[type]; | ||
Validator.prototype.resolveMessage = function(err) { | ||
let msg = this.messages[err.type]; | ||
if (msg != null) { | ||
msg = msg.replace(/\{name\}/g, field); | ||
return msg.replace(/{(\d+)}/g, function(match, i) { | ||
return typeof args[i] !== "undefined" ? args[i] : match; | ||
}); | ||
return msg.replace(/\{field\}/g, err.field).replace(/\{expected\}/g, err.expected || "").replace(/\{actual\}/g, err.actual || ""); | ||
} | ||
@@ -230,0 +226,0 @@ }; |
{ | ||
"name": "fastest-validator", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "balanced-match": { |
{ | ||
"name": "fastest-validator", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "The fastest JS validator library for NodeJS", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"bench": "node benchmark/index.js", | ||
"example": "nodemon examples/simple.js", | ||
"example": "nodemon examples/index.js", | ||
"ci": "jest --watch", | ||
@@ -11,0 +11,0 @@ "test": "jest --coverage", |
499
README.md
@@ -6,3 +6,498 @@ [![Build Status](https://travis-ci.org/icebob/fastest-validator.svg?branch=master)](https://travis-ci.org/icebob/fastest-validator) | ||
# fastest-validator | ||
The fastest JS validator library for NodeJS | ||
# fastest-validator [![NPM version](https://img.shields.io/npm/v/fastest-validator.svg)](https://www.npmjs.com/package/fastest-validator) | ||
:zap: The fastest JS validator library for NodeJS. | ||
## Key features | ||
* fast! Really! | ||
* 9 built-in validator | ||
* nested object & array handling | ||
* customizable error messages | ||
* programmable error object | ||
* unit tests & 100% cover | ||
# How fast? | ||
Very fast! 3 million validation/sec (on Intel i7-4770K, Node.JS: 6.10.0) | ||
``` | ||
√ validate with pre-compiled schema x 3,052,280 ops/sec ±0.82% (93 runs sampled) | ||
``` | ||
Compared to other popular libraries: | ||
[![Result](https://cloud.highcharts.com/images/yqowupa/4/800.png)](https://github.com/icebob/validator-benchmark#result) | ||
> 100x faster than Joi. | ||
**Would you like to test it?** | ||
``` | ||
$ git clone https://github.com/icebob/fastest-validator.git | ||
$ cd fastest-validator | ||
$ npm install | ||
$ npm run bench | ||
``` | ||
## Installation | ||
### NPM | ||
You can install it via [NPM](http://npmjs.org/). | ||
``` | ||
$ npm install fastest-validator --save | ||
``` | ||
or | ||
``` | ||
$ yarn add fastest-validator | ||
``` | ||
## Usage | ||
### Simple method | ||
Call the `validate` method with the `object` and the `schema`. | ||
> If the performance is important, you won't use this method. | ||
```js | ||
let Validator = require("fastest-validator"); | ||
let v = new Validator(); | ||
const schema = { | ||
id: { type: "number", positive: true, integer: true }, | ||
name: { type: "string", min: 3, max: 255 }, | ||
status: "boolean" // short-hand def | ||
}; | ||
console.log(v.validate({ id: 5, name: "John", status: true }, schema)); | ||
// Returns: true | ||
console.log(v.validate({ id: 5, name: "Al", status: true }, schema)); | ||
/* Returns an array with errors: | ||
[ | ||
{ | ||
type: 'stringMin', | ||
expected: 3, | ||
actual: 2, | ||
field: 'name', | ||
message: 'The \'name\' field length must be larger than or equal to 3 characters long!' | ||
} | ||
] | ||
*/ | ||
``` | ||
[Try it on Runkit](https://runkit.com/icebob/fastest-validator-usage-simple) | ||
### Fast method | ||
In this case, the first step is to compile the schema to a compiled "checker" function. After it if you would like to validate your object, just call this "checker" function with your object. | ||
> This method is ~10x faster than "simple method". | ||
```js | ||
let Validator = require("fastest-validator"); | ||
let v = new Validator(); | ||
const schema = { | ||
id: { type: "number", positive: true, integer: true }, | ||
name: { type: "string", min: 3, max: 255 }, | ||
status: "boolean" // short-hand def | ||
}; | ||
const check = v.compile(schema); | ||
console.log(check({ id: 5, name: "John", status: true })); | ||
// Returns: true | ||
console.log(check({ id: 2, name: "Adam" })); | ||
/* Returns an array with errors: | ||
[ | ||
{ | ||
type: 'required', | ||
field: 'status', | ||
message: 'The \'status\' field is required!' | ||
} | ||
] | ||
*/ | ||
``` | ||
[Try it on Runkit](https://runkit.com/icebob/fastest-validator-usage-quick) | ||
# Optional & required fields | ||
Every fields in the schema will be required field. If you would like to define optional fields, set ˙optional: true`. | ||
```js | ||
let schema = { | ||
name: { type: "string" }, // required | ||
age: { type: "number", optional: true } | ||
} | ||
v.validate({ name: "John", age: 42 }, schema); // Valid | ||
v.validate({ name: "John" }, schema); // Valid | ||
v.validate({ age: 42 }, schema); // Fail | ||
``` | ||
# Built-in validators | ||
## `any` | ||
This is not validate the type of value. Accept any types. | ||
```js | ||
let schema = { | ||
prop: { type: "any" } | ||
} | ||
v.validate({ prop: true }, schema); // Valid | ||
v.validate({ prop: 100 }, schema); // Valid | ||
v.validate({ prop: "John" }, schema); // Valid | ||
``` | ||
## `array` | ||
This is an `Array` validator. | ||
**Simple example with strings:** | ||
```js | ||
let schema = { | ||
roles: { type: "array", items: "string" } | ||
} | ||
v.validate({ roles: ["user"] }, schema); // Valid | ||
v.validate({ roles: [] }, schema); // Valid | ||
v.validate({ roles: "user" }, schema); // Fail | ||
``` | ||
**Example with only positive number:** | ||
```js | ||
let schema = { | ||
list: { type: "array", min: 2, items: { | ||
type: "number", positive: true, integer: true | ||
} } | ||
} | ||
v.validate({ list: [2, 4] }, schema); // Valid | ||
v.validate({ list: [1, 5, 8] }, schema); // Valid | ||
v.validate({ list: [1] }, schema); // Fail (min 2 elements) | ||
v.validate({ list: [1, -7] }, schema); // Fail (negative number) | ||
``` | ||
**Example with object list:** | ||
```js | ||
let schema = { | ||
users: { type: "array", items: { | ||
type: "object", props: { | ||
id: { type: "number", positive: true }, | ||
name: { type: "string", empty: false }, | ||
status: "boolean" | ||
} | ||
} } | ||
} | ||
v.validate({ | ||
users: [ | ||
{ id: 1, name: "John", status: true }, | ||
{ id: 2, name: "Jane", status: true }, | ||
{ id: 3, name: "Bill", status: false } | ||
] | ||
}, schema); // Valid | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`empty` | `true` | If true, the validator accepts empty array `[]`. | ||
`min` | `null` | Minimum count of elements. | ||
`max` | `null` | Maximum count of elements. | ||
`length` | `null` | Fix count of elements. | ||
`contains` | `null` | The array must contains this element too. | ||
`enum` | `null` | Every element must be an element of the `enum` array. | ||
**Example for `enum`:** | ||
```js | ||
let schema = { | ||
roles: { type: "array", items: "string", enum: [ "user", "admin" ] } | ||
} | ||
v.validate({ roles: ["user"] }, schema); // Valid | ||
v.validate({ roles: ["user", "admin"] }, schema); // Valid | ||
v.validate({ roles: ["guest"] }, schema); // Fail | ||
``` | ||
## `boolean` | ||
This is a `Boolean` validator. | ||
```js | ||
let schema = { | ||
status: { type: "boolean" } | ||
} | ||
v.validate({ status: true }, schema); // Valid | ||
v.validate({ status: false }, schema); // Valid | ||
v.validate({ status: 1 }, schema); // Fail | ||
v.validate({ status: "true" }, schema); // Fail | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`convert` | `false` | if `true` and the type is not `Boolean`, try to convert. `1`, `"true"`, `"1"`, `"on"` will be true. `0`, `"false"`, `"0"`, `"off"` will be false. | ||
## `date` | ||
This is a `Date` validator. | ||
```js | ||
let schema = { | ||
dob: { type: "date" } | ||
} | ||
v.validate({ dob: new Date() }, schema); // Valid | ||
v.validate({ dob: new Date(1488876927958) }, schema); // Valid | ||
v.validate({ dob: 1488876927958 }, schema); // Fail | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`convert` | `false`| if `true` and the type is not `Date`, try to convert with `new Date()`. | ||
## `email` | ||
This is an e-mail address validator. | ||
```js | ||
let schema = { | ||
email: { type: "email" } | ||
} | ||
v.validate({ email: "john.doe@gmail.com" }, schema); // Valid | ||
v.validate({ email: "james.123.45@mail.co.uk" }, schema); // Valid | ||
v.validate({ email: "abc@gmail" }, schema); // Fail | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`mode` | `quick` | Checker method. Can be `quick` or `precise`. | ||
## `forbidden` | ||
This validator gives error if the property is exists in the object. | ||
```js | ||
let schema = { | ||
password: { type: "forbidden" } | ||
} | ||
v.validate({ user: "John" }, schema); // Valid | ||
v.validate({ user: "John", password: "pass1234" }, schema); // Fail | ||
``` | ||
## `function` | ||
The type of value must be `Function`. | ||
```js | ||
let schema = { | ||
show: { type: "function" } | ||
} | ||
v.validate({ show: function() {} }, schema); // Valid | ||
v.validate({ show: Date.now }, schema); // Valid | ||
v.validate({ show: null }, schema); // Fail | ||
``` | ||
## `number` | ||
This is a number validator. The type of value must be `Number`. | ||
```js | ||
let schema = { | ||
age: { type: "number" } | ||
} | ||
v.validate({ age: 123 }, schema); // Valid | ||
v.validate({ age: 5.65 }, schema); // Valid | ||
v.validate({ age: "100" }, schema); // Fail | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`min` | `null` | Minimum value. | ||
`max` | `null` | Maximum value. | ||
`equal` | `null` | Fix value. | ||
`notEqual` | `null` | Can't be equal with this value. | ||
`integer` | `false` | The value must be a non-decimal value. | ||
`positive` | `false`| The value must be larger than zero. | ||
`negative` | `false`| The value must be less than zero. | ||
`convert` | `false`| if `true` and the type is not `Number`, try to convert with `parseFloat`. | ||
## `object` | ||
This is a nested object validator. | ||
```js | ||
let schema = { | ||
address: { type: "object", props: { | ||
country: { type: "string" }, | ||
city: "string", // short-hand | ||
zip: "number" // short-hand | ||
} } | ||
} | ||
v.validate({ | ||
address: { | ||
country: "Italy", | ||
city: "Rome", | ||
zip: 12345 | ||
} | ||
}, schema); // Valid | ||
v.validate({ | ||
address: { | ||
country: "Italy", | ||
city: "Rome" | ||
} | ||
}, schema); // Fail ("The 'address.zip' field is required!") | ||
``` | ||
## `string` | ||
This is a `string` validator. The type of value must be `String`. | ||
```js | ||
let schema = { | ||
name: { type: "string" } | ||
} | ||
v.validate({ name: "John" }, schema); // Valid | ||
v.validate({ name: "" }, schema); // Valid | ||
v.validate({ name: 123 }, schema); // Fail | ||
``` | ||
### Properties | ||
Property | Default | Description | ||
-------- | -------- | ----------- | ||
`empty` | `true` | If true, the validator accepts empty string `""`. | ||
`min` | `null` | Minimum length of value. | ||
`max` | `null` | Maximum length of value. | ||
`length` | `null` | Fix length of value. | ||
`pattern` | `null` | Regex pattern. | ||
`contains` | `null` | The value must contains this text. | ||
`enum` | `null` | The value must be an element of the `enum` array. | ||
## `url` | ||
This is an URL validator. | ||
```js | ||
let schema = { | ||
url: { type: "url" } | ||
} | ||
v.validate({ url: "http://google.com" }, schema); // Valid | ||
v.validate({ url: "https://github.com/icebob" }, schema); // Valid | ||
v.validate({ url: "www.facebook.com" }, schema); // Fail | ||
``` | ||
# Custom error messages (l10n) | ||
You can set your custom messages in constructor of validator. | ||
```js | ||
const Validator = require("fastest-validator"); | ||
const v = new Validator({ | ||
messages: { | ||
stringMin: "A(z) '{field}' mező túl rövid. Minimum: {expected}, Jelenleg: {actual}", | ||
stringMax: "A(z) '{field}' mező túl hosszú. Minimum: {expected}, Jelenleg: {actual}" | ||
} | ||
}); | ||
v.validate({ name: "John" }, { name: { type: "string", min: 6 }}); | ||
/* Returns: | ||
[ | ||
{ | ||
type: 'stringMin', | ||
expected: 6, | ||
actual: 4, | ||
field: 'name', | ||
message: 'A(z) \'name\' mező túl rövid. Minimum: 6, Jelenleg: 4' | ||
} | ||
] | ||
*/ | ||
``` | ||
## Message types | ||
Name | Default text | ||
---- | ------------- | ||
`required` | The '{field}' field is required! | ||
`string` | The '{field}' field must be a string! | ||
`stringEmpty` | The '{field}' field must not be empty! | ||
`stringMin`: | The '{field}' field length must be larger than or equal to {expected} characters long! | ||
`stringMax`: | The '{field}' field length must be less than or equal to {expected} characters long! | ||
`stringLength` | The '{field}' field length must be {expected} characters long! | ||
`stringPattern` | The '{field}' field fails to match the required pattern! | ||
`stringContains` | The '{field}' field must contain the '{expected}' text! | ||
`stringEnum` | The '{field}' field does not match any of the allowed values! | ||
`number` | The '{field}' field must be a number! | ||
`numberMin` | The '{field}' field must be larger than or equal to {expected}! | ||
`numberMax` | The '{field}' field must be less than or equal to {expected}! | ||
`numberEqual` | The '{field}' field must be equal with {expected}! | ||
`numberNotEqual` | The '{field}' field can't be equal with {expected}! | ||
`numberInteger` | The '{field}' field must be an integer! | ||
`numberPositive` | The '{field}' field must be a positive number! | ||
`numberNegative` | The '{field}' field must be a negative number! | ||
`array` | The '{field}' field must be an array! | ||
`arrayEmpty` | The '{field}' field must not be an empty array! | ||
`arrayMin` | The '{field}' field must contain at least {expected} items! | ||
`arrayMax` | The '{field}' field must contain less than or equal to {expected} items! | ||
`arrayLength` | The '{field}' field must contain {expected} items! | ||
`arrayContains` | The '{field}' field must contain the '{expected}' item! | ||
`arrayEnum`: | The '{field} field value '{expected}' does not match any of the allowed values! | ||
`boolean` | The '{field}' field must be a boolean! | ||
`function` | The '{field}' field must be a function! | ||
`date` | The '{field}' field must be a Date! | ||
`dateMin` | The '{field}' field must be larger than or equal to {expected}! | ||
`dateMax` | The '{field}' field must be less than or equal to {expected}! | ||
`forbidden` | The '{field}' field is forbidden! | ||
`email` | The '{field}' field must be a valid e-mail! | ||
## Message fields | ||
Name | Description | ||
---- | ------------- | ||
`field` | Name of field | ||
`expected` | The expected value of field | ||
`actual` | The actual value of field | ||
`type` | Type of field | ||
## Development | ||
``` | ||
npm run dev | ||
``` | ||
## Test | ||
``` | ||
npm test | ||
``` | ||
### Coverage report | ||
``` | ||
---------------|----------|----------|----------|----------|----------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | | ||
---------------|----------|----------|----------|----------|----------------| | ||
All files | 100 | 100 | 100 | 100 | | | ||
lib | 100 | 100 | 100 | 100 | | | ||
messages.js | 100 | 100 | 100 | 100 | | | ||
validator.js | 100 | 100 | 100 | 100 | | | ||
lib/rules | 100 | 100 | 100 | 100 | | | ||
any.js | 100 | 100 | 100 | 100 | | | ||
array.js | 100 | 100 | 100 | 100 | | | ||
boolean.js | 100 | 100 | 100 | 100 | | | ||
date.js | 100 | 100 | 100 | 100 | | | ||
email.js | 100 | 100 | 100 | 100 | | | ||
forbidden.js | 100 | 100 | 100 | 100 | | | ||
function.js | 100 | 100 | 100 | 100 | | | ||
number.js | 100 | 100 | 100 | 100 | | | ||
object.js | 100 | 100 | 100 | 100 | | | ||
string.js | 100 | 100 | 100 | 100 | | | ||
url.js | 100 | 100 | 100 | 100 | | | ||
---------------|----------|----------|----------|----------|----------------| | ||
``` | ||
## Contribution | ||
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important. | ||
## License | ||
fastest-validator is available under the [MIT license](https://tldrlegal.com/license/mit-license). | ||
## Contact | ||
Copyright (C) 2017 Icebob | ||
[![@icebob](https://img.shields.io/badge/github-icebob-green.svg)](https://github.com/icebob) [![@icebob](https://img.shields.io/badge/twitter-Icebobcsi-blue.svg)](https://twitter.com/Icebobcsi) |
@@ -8,3 +8,3 @@ "use strict"; | ||
it("check default messages", () => { | ||
expect(msg.required).toBe("The '{name}' field is required!"); | ||
expect(msg.required).toBe("The '{field}' field is required!"); | ||
@@ -11,0 +11,0 @@ expect(msg.required).toBeDefined(); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "array" }; | ||
const err = { type: "array", args: [] }; | ||
const err = { type: "array" }; | ||
@@ -33,3 +33,3 @@ expect(check(null, s)).toEqual(err); | ||
expect(check([1], s)).toEqual(true); | ||
expect(check([], s)).toEqual({ type: "arrayEmpty", args: [] }); | ||
expect(check([], s)).toEqual({ type: "arrayEmpty" }); | ||
}); | ||
@@ -40,4 +40,4 @@ | ||
expect(check([], s)).toEqual({ type: "arrayMin", args: [3, 0] }); | ||
expect(check([5, 7], s)).toEqual({ type: "arrayMin", args: [3, 2] }); | ||
expect(check([], s)).toEqual({ type: "arrayMin", expected: 3, actual: 0 }); | ||
expect(check([5, 7], s)).toEqual({ type: "arrayMin", expected: 3, actual: 2 }); | ||
expect(check(["a", "b", "c"], s)).toEqual(true); | ||
@@ -50,3 +50,3 @@ expect(check([1, 2, 3, 4, 5], s)).toEqual(true); | ||
expect(check([1, 2, 3, 4], s)).toEqual({ type: "arrayMax", args: [3, 4] }); | ||
expect(check([1, 2, 3, 4], s)).toEqual({ type: "arrayMax", expected: 3, actual: 4 }); | ||
expect(check(["a", "b", "c"], s)).toEqual(true); | ||
@@ -60,5 +60,5 @@ expect(check([1], s)).toEqual(true); | ||
expect(check([1, 2, 3, 4], s)).toEqual({ type: "arrayLength", args: [2, 4] }); | ||
expect(check([1], s)).toEqual({ type: "arrayLength", args: [2, 1] }); | ||
expect(check([], s)).toEqual({ type: "arrayLength", args: [2, 0] }); | ||
expect(check([1, 2, 3, 4], s)).toEqual({ type: "arrayLength", expected: 2, actual: 4 }); | ||
expect(check([1], s)).toEqual({ type: "arrayLength", expected: 2, actual: 1 }); | ||
expect(check([], s)).toEqual({ type: "arrayLength", expected: 2, actual: 0 }); | ||
expect(check(["a", "b"], s)).toEqual(true); | ||
@@ -70,4 +70,4 @@ }); | ||
expect(check([], s)).toEqual({ type: "arrayContains", args: ["bob"] }); | ||
expect(check(["john"], s)).toEqual({ type: "arrayContains", args: ["bob"] }); | ||
expect(check([], s)).toEqual({ type: "arrayContains", expected: "bob" }); | ||
expect(check(["john"], s)).toEqual({ type: "arrayContains", expected: "bob" }); | ||
expect(check(["john", "bob"], s)).toEqual(true); | ||
@@ -79,9 +79,9 @@ }); | ||
//expect(check([], s)).toEqual({ type: "arrayEnum", args: [["male", "female"]] }); | ||
expect(check(["human"], s)).toEqual({ type: "arrayEnum", args: ["human", ["male", "female"]] }); | ||
//expect(check([], s)).toEqual({ type: "arrayEnum", expected: ["male", "female"]] }); | ||
expect(check(["human"], s)).toEqual({ type: "arrayEnum", expected: "human", actual: ["male", "female"] }); | ||
expect(check(["male"], s)).toEqual(true); | ||
expect(check(["male", "female"], s)).toEqual(true); | ||
expect(check(["male", "female", "human"], s)).toEqual({ type: "arrayEnum", args: ["human", ["male", "female"]] }); | ||
expect(check(["male", "female", "human"], s)).toEqual({ type: "arrayEnum", expected: "human", actual: ["male", "female"] }); | ||
}); | ||
}); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "boolean" }; | ||
const err = { type: "boolean", args: [] }; | ||
const err = { type: "boolean" }; | ||
@@ -32,3 +32,3 @@ expect(check(null, s)).toEqual(err); | ||
const s = { type: "boolean", convert: true }; | ||
const err = { type: "boolean", args: [] }; | ||
const err = { type: "boolean" }; | ||
@@ -35,0 +35,0 @@ expect(check(null, s)).toEqual(err); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "date" }; | ||
const err = { type: "date", args: [] }; | ||
const err = { type: "date" }; | ||
@@ -34,3 +34,3 @@ expect(check(null, s)).toEqual(err); | ||
const s = { type: "date", convert: true }; | ||
const err = { type: "date", args: [] }; | ||
const err = { type: "date" }; | ||
@@ -37,0 +37,0 @@ expect(check(null, s)).toEqual(true); |
@@ -13,4 +13,4 @@ "use strict"; | ||
const s = { type: "email" }; | ||
const err = { type: "email", args: [] }; | ||
const errString = { type: "string", args: [] }; | ||
const err = { type: "email" }; | ||
const errString = { type: "string" }; | ||
@@ -31,3 +31,3 @@ expect(check(null, s)).toEqual(errString); | ||
const s = { type: "email" }; | ||
const err = { type: "email", args: [] }; | ||
const err = { type: "email" }; | ||
@@ -49,3 +49,3 @@ expect(check("abcdefg", s)).toEqual(err); | ||
const s = { type: "email", mode: "precise" }; | ||
const err = { type: "email", args: [] }; | ||
const err = { type: "email" }; | ||
@@ -52,0 +52,0 @@ expect(check("abcdefg", s)).toEqual(err); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "forbidden" }; | ||
const err = { type: "forbidden", args: [] }; | ||
const err = { type: "forbidden" }; | ||
@@ -16,0 +16,0 @@ expect(check(null, s)).toEqual(true); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "function" }; | ||
const err = { type: "function", args: [] }; | ||
const err = { type: "function" }; | ||
@@ -16,0 +16,0 @@ expect(check(null, s)).toEqual(err); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "number" }; | ||
const err = { type: "number", args: [] }; | ||
const err = { type: "number" }; | ||
@@ -37,3 +37,3 @@ expect(check(null, s)).toEqual(err); | ||
const s = { type: "number", convert: true }; | ||
const err = { type: "number", args: [] }; | ||
const err = { type: "number" }; | ||
@@ -56,4 +56,4 @@ expect(check(null, s)).toEqual(err); | ||
expect(check(3, s)).toEqual({ type: "numberMin", args: [5, 3] }); | ||
expect(check(-20, s)).toEqual({ type: "numberMin", args: [5, -20] }); | ||
expect(check(3, s)).toEqual({ type: "numberMin", expected: 5, actual: 3 }); | ||
expect(check(-20, s)).toEqual({ type: "numberMin", expected: 5, actual: -20 }); | ||
expect(check(5, s)).toEqual(true); | ||
@@ -66,4 +66,4 @@ expect(check(8, s)).toEqual(true); | ||
expect(check(8, s)).toEqual({ type: "numberMax", args: [5, 8] }); | ||
expect(check(12345, s)).toEqual({ type: "numberMax", args: [5, 12345] }); | ||
expect(check(8, s)).toEqual({ type: "numberMax", expected: 5, actual: 8 }); | ||
expect(check(12345, s)).toEqual({ type: "numberMax", expected: 5, actual: 12345 }); | ||
expect(check(5, s)).toEqual(true); | ||
@@ -77,3 +77,3 @@ expect(check(0, s)).toEqual(true); | ||
expect(check(8, s)).toEqual({ type: "numberEqual", args: [123, 8] }); | ||
expect(check(8, s)).toEqual({ type: "numberEqual", expected: 123, actual: 8 }); | ||
expect(check(123, s)).toEqual(true); | ||
@@ -86,3 +86,3 @@ }); | ||
expect(check(8, s)).toEqual(true); | ||
expect(check(123, s)).toEqual({ type: "numberNotEqual", args: [123] }); | ||
expect(check(123, s)).toEqual({ type: "numberNotEqual", expected: 123 }); | ||
}); | ||
@@ -93,5 +93,5 @@ | ||
expect(check(8.5, s)).toEqual({ type: "numberInteger", args: [8.5] }); | ||
expect(check(0.001, s)).toEqual({ type: "numberInteger", args: [0.001] }); | ||
expect(check(-5.5, s)).toEqual({ type: "numberInteger", args: [-5.5] }); | ||
expect(check(8.5, s)).toEqual({ type: "numberInteger", expected: 8.5 }); | ||
expect(check(0.001, s)).toEqual({ type: "numberInteger", expected: 0.001 }); | ||
expect(check(-5.5, s)).toEqual({ type: "numberInteger", expected: -5.5 }); | ||
expect(check(0, s)).toEqual(true); | ||
@@ -105,5 +105,5 @@ expect(check(-20, s)).toEqual(true); | ||
expect(check(-5.5, s)).toEqual({ type: "numberPositive", args: [-5.5] }); | ||
expect(check(-45, s)).toEqual({ type: "numberPositive", args: [-45] }); | ||
expect(check(0, s)).toEqual({ type: "numberPositive", args: [0] }); | ||
expect(check(-5.5, s)).toEqual({ type: "numberPositive", expected: -5.5 }); | ||
expect(check(-45, s)).toEqual({ type: "numberPositive", expected: -45 }); | ||
expect(check(0, s)).toEqual({ type: "numberPositive", expected: 0 }); | ||
expect(check(0.001, s)).toEqual(true); | ||
@@ -117,5 +117,5 @@ expect(check(1, s)).toEqual(true); | ||
expect(check(5.5, s)).toEqual({ type: "numberNegative", args: [5.5] }); | ||
expect(check(45, s)).toEqual({ type: "numberNegative", args: [45] }); | ||
expect(check(0, s)).toEqual({ type: "numberNegative", args: [0] }); | ||
expect(check(5.5, s)).toEqual({ type: "numberNegative", expected: 5.5 }); | ||
expect(check(45, s)).toEqual({ type: "numberNegative", expected: 45 }); | ||
expect(check(0, s)).toEqual({ type: "numberNegative", expected: 0 }); | ||
expect(check(-0.001, s)).toEqual(true); | ||
@@ -122,0 +122,0 @@ expect(check(-1, s)).toEqual(true); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "object" }; | ||
const err = { type: "object", args: [] }; | ||
const err = { type: "object" }; | ||
@@ -16,0 +16,0 @@ expect(check(null, s)).toEqual(err); |
@@ -13,3 +13,3 @@ "use strict"; | ||
const s = { type: "string" }; | ||
const err = { type: "string", args: [] }; | ||
const err = { type: "string" }; | ||
@@ -33,3 +33,3 @@ expect(check(null, s)).toEqual(err); | ||
expect(check("abc", s)).toEqual(true); | ||
expect(check("", s)).toEqual({ type: "stringEmpty", args: [] }); | ||
expect(check("", s)).toEqual({ type: "stringEmpty" }); | ||
}); | ||
@@ -40,3 +40,3 @@ | ||
expect(check("John", s)).toEqual({ type: "stringMin", args: [5, 4] }); | ||
expect(check("John", s)).toEqual({ type: "stringMin", expected: 5, actual: 4 }); | ||
expect(check("Icebob", s)).toEqual(true); | ||
@@ -49,3 +49,3 @@ }); | ||
expect(check("John", s)).toEqual(true); | ||
expect(check("Icebob", s)).toEqual({ type: "stringMax", args: [5, 6] }); | ||
expect(check("Icebob", s)).toEqual({ type: "stringMax", expected: 5, actual: 6 }); | ||
}); | ||
@@ -56,3 +56,3 @@ | ||
expect(check("John", s)).toEqual({ type: "stringLength", args: [6, 4] }); | ||
expect(check("John", s)).toEqual({ type: "stringLength", expected: 6, actual: 4 }); | ||
expect(check("Icebob", s)).toEqual(true); | ||
@@ -64,3 +64,3 @@ }); | ||
expect(check("John", s)).toEqual({ type: "stringPattern", args: [/^[A-Z]+$/] }); | ||
expect(check("John", s)).toEqual({ type: "stringPattern", expected: /^[A-Z]+$/ }); | ||
expect(check("JOHN", s)).toEqual(true); | ||
@@ -72,3 +72,3 @@ }); | ||
expect(check("John", s)).toEqual({ type: "stringContains", args: ["bob"] }); | ||
expect(check("John", s)).toEqual({ type: "stringContains", expected: "bob" }); | ||
expect(check("Icebob", s)).toEqual(true); | ||
@@ -80,4 +80,4 @@ }); | ||
expect(check("", s)).toEqual({ type: "stringEnum", args: [["male", "female"]] }); | ||
expect(check("human", s)).toEqual({ type: "stringEnum", args: [["male", "female"]] }); | ||
expect(check("", s)).toEqual({ type: "stringEnum", expected: ["male", "female"] }); | ||
expect(check("human", s)).toEqual({ type: "stringEnum", expected: ["male", "female"] }); | ||
expect(check("male", s)).toEqual(true); | ||
@@ -84,0 +84,0 @@ expect(check("female", s)).toEqual(true); |
@@ -13,4 +13,4 @@ "use strict"; | ||
const s = { type: "url" }; | ||
const err = { type: "url", args: [] }; | ||
const errString = { type: "string", args: [] }; | ||
const err = { type: "url" }; | ||
const errString = { type: "string" }; | ||
@@ -17,0 +17,0 @@ expect(check(null, s)).toEqual(errString); |
@@ -27,3 +27,3 @@ "use strict"; | ||
expect(v.messages.numberMin).toBe("Custom validation error message"); | ||
expect(v.messages.numberMax).toBe("The '{name}' field must be less than or equal to {0}!"); | ||
expect(v.messages.numberMax).toBe("The '{field}' field must be less than or equal to {expected}!"); | ||
}); | ||
@@ -90,3 +90,3 @@ | ||
it("should resolve variables in message string", () => { | ||
let res = v.resolveMessage("stringLength", "age", [3, 6]); | ||
let res = v.resolveMessage({ type: "stringLength", field: "age", expected: 3, actual: 6 }); | ||
expect(res).toBe("The 'age' field length must be 3 characters long!"); | ||
@@ -96,5 +96,5 @@ }); | ||
it("should resolve more variables in message string", () => { | ||
v.messages.custom = "Field {name} and again {name}. Args {0}, {2}, {1}."; | ||
let res = v.resolveMessage("custom", "country", ["London", true, 350]); | ||
expect(res).toBe("Field country and again country. Args London, 350, true."); | ||
v.messages.custom = "Field {field} and again {field}. Expected: {expected}, actual: {actual}."; | ||
let res = v.resolveMessage({ type: "custom", field: "country", expected: "London", actual: 350 }); | ||
expect(res).toBe("Field country and again country. Expected: London, actual: 350."); | ||
}); | ||
@@ -323,6 +323,7 @@ | ||
expect(res[0]).toEqual({ | ||
"type": "stringMin", | ||
"field": "name", | ||
"message": "The 'name' field length must be larger than or equal to 5 characters long!", | ||
"args": [5, 4] | ||
type: "stringMin", | ||
field: "name", | ||
message: "The 'name' field length must be larger than or equal to 5 characters long!", | ||
expected: 5, | ||
actual: 4 | ||
}); | ||
@@ -329,0 +330,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166294
43
1720
503
3