Skip to content
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
Loading