Module: vulcanval

The vulcan validator (vulcanval) creator.

This module is a function which receives a settings object and returns a validator object to validate data maps.

Also this has some properties and methods to configure the validations globally.

In Node.js environments use like:

const vulcanval = require('vulcanval');

In browser environments this object is available in window.vulcanval.

See:

Example

// Changing a global configuration in settings.
vulcanval.settings.locale = 'pt';

// Addind a new validator method.
vulcanval.addValidator('valName', function () { ... });

// Extending/creating a new locale/language.
vulcanval.extendLocale({ ... });

// A form configuration to create a validator.
const settings = {
  fields: [{
    name: 'fieldName',
    validators: {}
  }]
};

// Creating a validator from settings.
const validator = vulcanval(settings);

// A data map to validate.
const map = {
  fieldName: 'field value'
};

// Validating the map with the validator.
const result = validator.validate(map);

Members


<static> log :Object

This is a reference to the prhone-log package instance used to log messages.

Type:
  • Object

<static> settings :Object

This is a reference to the settings global configuration.

Type:
  • Object

<static> utilityContext :Object

This is a reference to the utilityContext global configuration.

This can be mutated to use your own custom properties and methods in any method/function that make use of this context.

Type:
  • Object

<static> validator :Object

This is a reference to the validator package.

Type:
  • Object

Methods


<static> addValidator(name, validator)

Add a custom validator globally.

All validators in the package validator are installed and ready to use.

Parameters:
Name Type Description
name String

An alphanumeric validator name.

validator function

The validator function. Receives as a first parameter the value of the field and has to return a truthy value. This function will have the utility context as function context. Don't pass arrow functions.

See:
Example
vulcanval.addValidator('isGreat', function (value) {
  return value.length > 4 && value.indexOf('great') >= 0;
});

const map = {
  field0: 'normal value'
};

const settings = {
  msgs: {
    isGreat: 'This field needs to be great!'
  },
  fields: [{
    name: 'field0',
    validators: {
      isGreat: true
    }
  }]
};

const vv = vulcanval(settings);

const field0Valid = vv.validateField('field0', map);
console.log(field0Valid); // 'This field needs to be great!'

<static> convertMapTo(to, map)

Convert a map from nested to plain and viceversa.

Parameters:
Name Type Description
to String

It can have two values: plain or nested.

map map

The object to convert.

Returns:

The converted object map.

Type
map

<static> extendLocale(locale)

Extend validators messages in an specific localization globally. If it does not exist it will be created.

Parameters:
Name Type Description
locale Object

A plain object describing the locale.

Properties
Name Type Description
id String

The identifier of the locale. It should be like: 'en', 'es', 'jp' or similar.

msgs Object

A plain object with validators names as keys and messages formats as values. It should have a default value with the key general, which will be used when there is no message for an specific validator on error.

Example
const locale = {
  id: 'jp',
  msgs: {

    // Default error message: "Invalid form field error".
    general: '無効なフォームフィールド。',

    // Message: "Form field has to be alphanumeric error message."
    isAlphanumeric: 'フォームフィールドは、英数字である必要があります。'
  }
};

vulcanval.extendLocale(locale);

// To set it as default, use:
vulcanval.settings.locale = 'jp';  // The identifier