Skip to content
Merged
30 changes: 30 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

# Change these settings to your own preference
indent_style = tab
indent_size = 4
space_after_anon_function = true

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
indent_style = space
indent_size = 4

[{package,bower}.json]
indent_style = space
indent_size = 2

[*.js]
quote_type = "double"
99 changes: 71 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ You can install it via [NPM](http://npmjs.org/).
```
$ npm install fastest-validator --save
```
or
or
```
$ yarn add fastest-validator
```

## Usage

### Simple method
Call the `validate` method with the `object` and the `schema`.
Call the `validate` method with the `object` and the `schema`.
> If performance is important, you won't use this method.

```js
Expand All @@ -76,7 +76,7 @@ console.log(v.validate({ id: 5, name: "John", status: true }, schema));
console.log(v.validate({ id: 5, name: "Al", status: true }, schema));
/* Returns an array with errors:
[
{
{
type: 'stringMin',
expected: 3,
actual: 2,
Expand Down Expand Up @@ -111,7 +111,7 @@ console.log(check({ id: 5, name: "John", status: true }));
console.log(check({ id: 2, name: "Adam" }));
/* Returns an array with errors:
[
{
{
type: 'required',
field: 'status',
message: 'The \'status\' field is required!'
Expand Down Expand Up @@ -187,7 +187,7 @@ v.validate({ prop: "John" }, schema); // Valid
```

## `array`
This is an `Array` validator.
This is an `Array` validator.

**Simple example with strings:**
```js
Expand Down Expand Up @@ -226,7 +226,7 @@ let schema = {
} }
}

v.validate({
v.validate({
users: [
{ id: 1, name: "John", status: true },
{ id: 2, name: "Jane", status: true },
Expand Down Expand Up @@ -259,7 +259,7 @@ v.validate({ roles: ["guest"] }, schema); // Fail


## `boolean`
This is a `Boolean` validator.
This is a `Boolean` validator.

```js
let schema = {
Expand All @@ -278,7 +278,7 @@ Property | Default | Description


## `date`
This is a `Date` validator.
This is a `Date` validator.

```js
let schema = {
Expand All @@ -295,7 +295,7 @@ 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.
This is an e-mail address validator.

```js
let schema = {
Expand All @@ -313,7 +313,7 @@ Property | Default | Description
`mode` | `quick` | Checker method. Can be `quick` or `precise`.

## `enum`
This is an enum validator.
This is an enum validator.

```js
let schema = {
Expand All @@ -332,7 +332,7 @@ Property | Default | Description


## `forbidden`
This validator returns an error if the property exists in the object.
This validator returns an error if the property exists in the object.

```js
let schema = {
Expand Down Expand Up @@ -393,15 +393,15 @@ let schema = {
} }
}

v.validate({
v.validate({
address: {
country: "Italy",
city: "Rome",
zip: 12345
}
}
}, schema); // Valid

v.validate({
v.validate({
address: {
country: "Italy",
city: "Rome"
Expand Down Expand Up @@ -440,7 +440,7 @@ Property | Default | Description


## `url`
This is an URL validator.
This is an URL validator.

```js
let schema = {
Expand Down Expand Up @@ -502,9 +502,9 @@ let v = new Validator({

const schema = {
name: { type: "string", min: 3, max: 255 },
weight: {
type: "custom",
minWeight: 10,
weight: {
type: "custom",
minWeight: 10,
check(value, schema) {
return (value < schema.minWeight)
? this.makeError("weightMin", schema.minWeight, value)
Expand All @@ -518,12 +518,12 @@ console.log(v.validate({ name: "John", weight: 50 }, schema));

console.log(v.validate({ name: "John", weight: 8 }, schema));
/* Returns an array with errors:
[{
type: 'weightMin',
expected: 10,
actual: 8,
field: 'weight',
message: 'The weight must be greater than 10! Actual: 8'
[{
type: 'weightMin',
expected: 10,
actual: 8,
field: 'weight',
message: 'The weight must be greater than 10! Actual: 8'
}]
*/
```
Expand All @@ -542,18 +542,61 @@ const v = new Validator({

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: 'A(z) \'name\' mező túl rövid. Minimum: 6, Jelenleg: 4'
}
]
*/
```
# Personalised Messages
Sometimes the standard messages are too generic. You can customise messages per validation type per field:

```js
const Validator = require("fastest-validator");
const v = new Validator();
const schema = {
firstname: {
type: "string",
min: 6,
messages: {
string: "Please check your firstname",
stringMin: "Your firstname is too short"
}
},
lastname: {
type: "string",
min: 6,
messages: {
string: "Please check your lastname",
stringMin: "Your lastname is too short"
}
}
}
v.validate({ firstname: "John", lastname: 23 }, schema );
/* Returns:
[
{
type: 'stringMin',
expected: 6,
actual: 4,
field: 'firstname',
message: 'Your firstname is too short'
},
{
type: 'string',
expected: undefined,
actual: undefined,
field: 'lastname',
message: 'Please check your lastname'
}
]
*/
```
## Message types
Name | Default text
------------------- | -------------
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/email.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const PRECISE_PATTERN = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const PRECISE_PATTERN = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const BASIC_PATTERN = /^\S+@\S+\.\S+$/;

module.exports = function checkEmail(value, schema) {
Expand All @@ -13,7 +13,7 @@ module.exports = function checkEmail(value, schema) {
pattern = PRECISE_PATTERN;
else
pattern = BASIC_PATTERN;

if (!pattern.test(value)) {
return this.makeError("email");
}
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
const ALPHA_PATTERN = /^[a-zA-Z]+$/;
const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/;
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_-]+$/;

module.exports = function checkString(value, schema) {
if (typeof value !== "string") {
Expand All @@ -26,7 +26,7 @@ module.exports = function checkString(value, schema) {

if (schema.length != null && valueLength !== schema.length) {
return this.makeError("stringLength", schema.length, valueLength);
}
}

if (schema.pattern != null) {
const pattern = typeof schema.pattern == "string" ? new RegExp(schema.pattern, schema.patternFlags) : schema.pattern;
Expand All @@ -36,7 +36,7 @@ module.exports = function checkString(value, schema) {

if (schema.contains != null && value.indexOf(schema.contains) === -1) {
return this.makeError("stringContains", schema.contains);
}
}

if (schema.enum != null && schema.enum.indexOf(value) === -1) {
return this.makeError("stringEnum", schema.enum);
Expand All @@ -59,4 +59,4 @@ module.exports = function checkString(value, schema) {
}

return true;
};
};
Loading