2.0.2 • Published 4 years ago

simplejsonvalidator v2.0.2

Weekly downloads
4
License
GPL-3.0
Repository
github
Last release
4 years ago

SimpleJsonValidator

BCH compliance

Json Validator for NodeJS with simple ruby-like syntax

Installation

You can install this library using npm:

npm i --save simplejsonvalidator

Usage

First, you need to create a new JsonValidator instance calling to the exported function:

const jsonValidator = require('simplejsonvalidator');
const validator = jsonValidator();

Then, you can create validators using the 'create' method, which accepts a callback and the validator name

validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}), 'validatorName');

or you can omit the validator name and assign the returning validator to a variable:

const myValidator = validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}));

Finally, you can validate any json using the 'validate' function:

const json = {
  user: 'user',  
  age: 22,
  text: '123456789',
};
myValidator.validate(json);

You can also nest objects like this:

const demoValidator = validator.create(t => ({
  user: t.string.required,
  age: t.number,
  text: t.string.max(10).required,
  data: {
    token: t.string,
  },
}));

And you can check why a json is invalid:

const errors = demoValidator.errors();

Errors look like these:

[
  {
    message: 'element "key" must be string',
    type: 'string',
  },
  {
    message: 'key "user.name" should be upper than 10',
    type: 'string',
  },
  {
    message: 'key "user.age" should be positive',
    type: 'number',
  },
  {
    message: 'element "user.connection" must be date',
    type: 'date',
  },
]

Types

String

Validates if type is string. Example:

validator.create(t => ({
  key: t.string,
}));

You can use these validators in string type:

ValidatorExplainationExample
requiredmakes key requiredt.string.required
shouldBechecks if the values matchest.string.shouldBe('apples', 'oranges')
max(number)maximum lenght limitt.string.max(10)
min(number)minimum lenght limitt.string.min(1)
matches(regex)tests if string matches regext.string.matches(/Regex/)

Number

Validates if type is number. Example:

validator.create(t => ({
  key: t.number,
}));

You can use these validators in number type:

ValidatorExplainationExample
requiredmakes key requiredt.number.required
shouldBechecks if the values matchest.number.shouldBe(22, 21)
positivechecks if number is positivet.number.positive
negativechecks if number is negativet.number.negative

Boolean

Validates if type is boolean. Example:

validator.create(t => ({
  key: t.boolean,
}));

You can use these validators in boolean type:

ValidatorExplainationExample
requiredmakes key requiredt.boolean.required
shouldBechecks if the value matchest.boolean.shouldBe(false)

Array

Validates if type is array. Example:

validator.create(t => ({
  key: t.array,
}));

You can use these validators in array type:

ValidatorExplainationExample
requiredmakes key requiredt.array.required
shouldBechecks if the values matchest.array.shouldBe(22, 21, 1, 'hi')
exactLength(number)check if array length is exactly the specified lengtht.array.exactLength(20)
lengthLowerTo(number)check if array length is lower to the specified lengtht.array.lengthLowerTo(9)
lengthUpperTo(number)check if array length is upper to the specified lengtht.array.lengthUpperTo(1)
notEmptycheck if array legnth is not emptyt.array.notEmpty

Date

Validates if type is date. Example:

validator.create(t => ({
  key: t.date,
}));

You can use these validators in date type:

ValidatorExplainationExample
requiredmakes key requiredt.date.required
shouldBechecks if the values matchest.date.shouldBe(new Date())
beforeDate(Date)check if the date is before desired datet.date.beforeDate(new Date())
afterDate(Date)check if the date is after desired datet.date.afterDate(new Date())

Express Framework Integration

You can use an express middleware to check jsons. You can do it calling to createMiddleware function to create a middleware:

const myValidator = validator.create(t => ({
  user: t.string.required,  
  age: t.number,
  text: t.string.max(10).required,
}));

app.post('/', validator.createMiddleware(myValidator), (req, res) => res.send(req.body););

The middleware, when a json is not valid, returns found errors. You can customize the returning status code:

const middleware = validator.createMiddleware(myValidator, 401);
2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

7 years ago

0.4.0

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago