<commit_before># validators-constructor
Constructor for library of validators

[![NPM version](https://img.shields.io/npm/v/validators-constructor.svg)](https://npmjs.org/package/validators-constructor)
[![Build status](https://img.shields.io/travis/tamtakoe/validators-constructor.svg)](https://travis-ci.org/tamtakoe/validators-constructor)

**Note:** This module works in browsers and Node.js >= 4.0

## Installation

```sh
npm install validators-constructor
```

## Usage

```js
const Validators = require('validators-constructor');
const validators = Validators();

validators.load({
    //Simple validator
    maxLength: function(value, comparedValue, options) {
        //if you use comparedValue you must specify the options in arguments, even if they do not use
        //comparedValue also is available as options.comparedValue

        if ((isString(value) || isArray(value)) && value.length > comparedValue) {
            return 'is too long (maximum is %{comparedValue})';
        }
    },
    maxlength: 'maxLength', //alias for `maxLength`

    //Difficult validator (validate by several params)
    range: function(value, options) {
        if (isNumber(value)) {
            if (value > options.to) {
                return {
                    type: 'many',
                    message: options.manyMessage || 'is too many (should be from %{from} to %{to})'
                }

            } else if (value < options.from) {
                return {
                    type: 'less',
                    message: options.lessMessage || 'is too less (should be from %{from} to %{to})'
                }
            }
        }
    }
});

validators.maxlength('abc', 2);
/* returns:
{
    message: 'is too long (maximum is 2)',
    error: 'maxlength',
    comparedValue: 2
}
*/

validators.range(7, {from: 1, to: 5, lessMessage: 'is too less', manyMessage: 'is too many'});
/* returns (options end in `Message` are not in the result):
{
    type: 'many',
    message: 'is too many',
    error: 'range',
    from: 1,
    to: 5
}
*/
```

## API

### Validators([options])

- **options** (`Object`)
  * `errorFormat` (`Object`) - Output format of error. By default:
                           `{error: '%{validator}', message: '%{message}', $options: true, $origin: true}`
                           You can use %{template} syntax (by default). Next variables are enabled:
                           `validator` - validator name. f.e. `maxLength`;
                           `message` - string which validator returns if error;
                           options which you set in validator `options` if `$options: true`;
                           options which validator returns instead string (except options that end in `Message`) if `$origin: true`
  * `formatStr` (`Function`) - Custom template parser for error strings.
                           *get* `(templateStr, variablesObj)`
                           *return* `str`

**return** (`Validators`) instance of Validators


### validators.add(validatorName, validatorFn)<commit_msg>fix format<commit_after># validators-constructor
Constructor for library of validators

[![NPM version](https://img.shields.io/npm/v/validators-constructor.svg)](https://npmjs.org/package/validators-constructor)
[![Build status](https://img.shields.io/travis/tamtakoe/validators-constructor.svg)](https://travis-ci.org/tamtakoe/validators-constructor)

**Note:** This module works in browsers and Node.js >= 4.0

## Installation

```sh
npm install validators-constructor
```

## Usage

```js
const Validators = require('validators-constructor');
const validators = Validators();

validators.load({
    //Simple validator
    maxLength: function(value, comparedValue, options) {
        //if you use comparedValue you must specify the options in arguments, even if they do not use
        //comparedValue also is available as options.comparedValue

        if ((isString(value) || isArray(value)) && value.length > comparedValue) {
            return 'is too long (maximum is %{comparedValue})';
        }
    },
    maxlength: 'maxLength', //alias for `maxLength`

    //Difficult validator (validate by several params)
    range: function(value, options) {
        if (isNumber(value)) {
            if (value > options.to) {
                return {
                    type: 'many',
                    message: options.manyMessage || 'is too many (should be from %{from} to %{to})'
                }

            } else if (value < options.from) {
                return {
                    type: 'less',
                    message: options.lessMessage || 'is too less (should be from %{from} to %{to})'
                }
            }
        }
    }
});

validators.maxlength('abc', 2);
/* returns:
{
    message: 'is too long (maximum is 2)',
    error: 'maxlength',
    comparedValue: 2
}
*/

validators.range(7, {from: 1, to: 5, lessMessage: 'is too less', manyMessage: 'is too many'});
/* returns (options end in `Message` are not in the result):
{
    type: 'many',
    message: 'is too many',
    error: 'range',
    from: 1,
    to: 5
}
*/
```

## API

### Validators([options])

- **options** (`Object`)
  * `errorFormat` (`Object`) - Output format of error. By default:
                           `{error: '%{validator}', message: '%{message}', $options: true, $origin: true}`
                           You can use %{template} syntax (by default). Next variables are enabled:
                           `validator` - validator name. f.e. `maxLength`;
                           `message` - string which validator returns if error;
                           options which you set in validator `options` if `$options: true`;
                           options which validator returns instead string (except options that end in `Message`) if `$origin: true`
  * `formatStr` (`Function`) - Custom template parser for error strings.
                           *get* `(templateStr, variablesObj)`
                           *return* `str`

- **return** (`Validators`) instance of Validators


### validators.add(validatorName, validatorFn)
{'fraction_non_alphanumeric': 0.13345341411495007, 'fraction_numerical': 0.0032749304077288356, 'mean_word_length': 2.3123644251626896, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '34625', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1679, 'n_words': 526}