@aeres-games/validator

A JS data validator

Usage no npm install needed!

<script type="module">
  import aeresGamesValidator from 'https://cdn.skypack.dev/@aeres-games/validator';
</script>

README

Aeres Games Validator

A JS data validator

Installing

npm install @aeres-games/validator

Usage

Here is a JS usage guide of this library.
Please note that this library is developed using Typescript and that the ts declaration files are available in the npm package.

Create a Validator instance with a Rule tree

const Validator = require('@aeres-games/validator');

let validator = new Validator.Validator(new Validator.ObjectRule({
    foo: new Validator.BooleanRule(),
    bar: new Validator.NumberRule({min: 0, max: 20})
}));

Then use the validate method :

let errors = validator.validate({bar: -20});
// errors is an array of validation errors (objects with a message string and a position string array)

console.log(errors);
// Should output something like :
// [ {message: 'Object must have a property named foo', position: []}, {message: 'Number must not be less than 0', position: ['bar']} ]

You can also configure the Validator to throw a Validator.ValidationError when a validation error occurs instead of returning an array of errors

const Validator = require('@aeres-games/validator');

let validator = new Validator.Validator(new Validator.BooleanRule(), {throw: true});
validator.validate('this is not a boolean');
// Throws a ValidationError with the list of errors as the message

Rule classes

Each Rule class has a validate() method which takes the data to validate as a parameter and returns an array of validation errors ({message: '...', position: ['prop', 'subprop']}).
You can create your own validations rules as long as you define this method (implement the Validator.IRule interface if you are using Typescript). Check out the source code of existing Rules for more information.
Below is the list of built-in validation rules.

ObjectRule

Used to validate an object. Takes a structure as a constructor parameter which must be an object with either a Rule or a set of rule + options :

let rule = new Validator.ObjectRule({
    foo: new Validator.BooleanRule(),
    bar: {rule: new Validator.BooleanRule(), options: {optional: true}}
});

Valid options :

  • optional (boolean) : Wether the element can be omitted or not (default false)

ArrayRule

Used to validate an array. Takes a subrule as a constructor parameter that will be used to validate array elements :

let rule = new Validator.ArrayRule(new Validator.BooleanRule());
let errors = rule.validate([true, false]); // All good

It can also take an options object as a second constructor parameter.
Valid options:

  • minSize (number) : The minimum size of the array (default none)
  • maxSize (number) : The maximum size of the array (default none)

BooleanRule

Used to validate a boolean. Can take an options object as a constructor parameter.
Valid options :

  • nullable (boolean) : Wether the value can be null or not (default false)

StringRule

Used to validate a string. Can take an options object as a constructor parameter.
Valid options :

  • nullable (boolean) : Wether the value can be null or not (default false)
  • minLength (number) : The minimum length of the string (default none)
  • maxLength (number) : The maximum length of the string (default none)
  • regex (RegExp) : A RegExp used to validate the string (default none)

NumberRule

Used to validate a number. Can take an options object as a constructor parameter.
Valid options :

  • nullable (boolean) : Wether the value can be null or not (default false)
  • floating (boolean) : Wether the number can be floating or not (default false)
  • min (number) : The minimum value of the number (default none)
  • max (number) : The maximum value of the number (default none)

OrRule

Used to allow multiple values. Take a list of Rules or arrays of Rules as constructor parameters.

ArrayableRule

Data being validated can either match the subrule (passed as a constructor parameter) or be an array of elements that match the subrule.

License

This package is licensed under the MIT license.
Please see the LICENSE file for more informations.