Namespace: validator

validator

This is an object created by the module vulcanval by specified settings to validate data maps.

This object has some methods to help you validate data possibly extracted from forms through a configuration that can be used in client side and server side.

See:
  • vulcanval to see how to create this object.

Methods


<static> cleanMap(isPlain, map)

Clean a map from properties outside the validation process.

This is done by removing all properties which are not present in the fields list of validation and the fields which are disabled or intented to be only used in client side.

Parameters:
Name Type Description
isPlain Boolean

If the map is plain. false for nested.

map map

The map to clean.

See:
Returns:
  • The cleaned map.
Type
map

<static> validate(map)

Validate provided data map and get an object describing each field error if there are.

Parameters:
Name Type Description
map map

The data map.

Returns:

If the map is valid, false will be returned. Otherwise there will be an object describing each field error as a plain map with its keys as the fields names even if the property settings.enableNestedMaps is enabled. Use the vulcanval.convertMapTo method if needed.

Type
Boolean | Object
Example
const map = {
  name: 'Romel',
  age: 22,
  likesPumpkin: false
};

const settings = {
  fields: [{
    name: 'name',
    required: true,
    validators: {
      isAlphanumeric: 'en-US',
      isLowercase: true
    }
  }, {
    name: 'age',
    validators: {
      isInt: { min: 1, max: 500 }
    }
  }, {
    name: 'likesPumpkin',
    required: true
  }]
};

const vv = vulcanval(settings);

const result = vv.validate(map);
console.log(result);
// {
//   name: 'This field should only contain lowercase text.',
//   likesPumpkin: 'Please fill out this field.'
// }

map.name = 'romel';
map.likesPumpkin = true;
const result2 = vv.validate(map);
console.log(result2);
// false

<static> validateField(fieldName, map)

Validate a field in provided data map.

Parameters:
Name Type Description
fieldName String

The field name to validate. If the map is nested, the field name should be set as in plain map. Ex: {user: {name: 'romel'}} will be 'user.name'.

map map

The data map (plain or nested).

Returns:

If it is valid, false will be returned. Otherwise there will be an string message describing the error.

Type
Boolean | String
Example
const map = {
  name: 'Romel',
  age: 22,
  likesPumpkin: false
};

const settings = {
  fields: [{
    name: 'name',
    required: true,
    validators: {
      isAlphanumeric: 'en-US',
      isLowercase: true
    }
  }, {
    name: 'age',
    validators: {
      isInt: { min: 1, max: 500 }
    }
  }]
};

const vv = vulcanval(settings);

const nameResult = vv.validateField('name', map);
console.log(nameResult); // 'This field should only contain lowercase text.'

const ageResult = vv.validateField('age', map);
console.log(ageResult); // false

<static> validateFields(namesList, map)

Validate the fields provided and their affected fields set by their listenTo properties.

This method is not available for the jQuery plugin currently.

Parameters:
Name Type Description
namesList String | Array

An array with the names of the fields to validate or just one field name.

map Object

The map to validate.

Returns:
  • If the fields are valid, returns false. Otherwise there will be an object describing each field error as a plain map with its keys as the fields names even if the property settings.enableNestedMaps is enabled. Use the vulcanval.convertMapTo method if needed.
Type
Boolean | Object
Example
const vv = vulcanval({
  fields: [
    { name: 'married', required: true },
    { name: 'height', required: true },
    { name: 'age', required: true, listenTo: 'height' },
    { name: 'weight', required: true, listenTo: ['height', 'age'] }
  ]
});
const map = { married: false, height: '', age: '23', weight: '' };

console.log(vv.validateFields('height', map));
// { height: 'Err message.', age: false, weight: 'Err message' }
// Height is validated and age and weight which are listening.

console.log(vv.validateFields(['married', 'age'], map));
// { married: 'Err message', age: false, weight: 'Err message' }
// Married and age are validated and weight which is listening.

<static> validateFieldset(fieldsetName, map)

Validate all fields of fieldset in map provided.

Parameters:
Name Type Description
fieldsetName String

The fieldset name to validate.

map map

The data map (plain or nested).

Returns:

If the fieldset fields are valid, false will be returned. Otherwise there will be an object describing each field error as a plain map with its keys as the fields names even if the property settings.enableNestedMaps is enabled. Use the vulcanval.convertMapTo method if needed.

Type
Boolean | Object
Example
const map = {
  user: {
    name: 'Romel',
    age: 22
  },
  course: {
    id: '4cdfb11e1f3c000000007822',
    register: 'wrong value'
  }
};

const settings = {

  // Usually you would use nested maps with fieldsets.
  enableNestedMaps: true,

  fieldsets: [{
    name: 'user',
    fields: ['user.name', 'user.age'],
    required: true
  }, {
    name: 'course',
    fields: ['course.id', 'course.register'],
    required: true
  }],

  fields: [{
    name: 'user.name',
    validators: { isAlphanumeric: true, isLowercase: true }
  }, {
    name: 'user.age',
    validators: { isInt: { min: 1, max: 500 } }
  }, {
    name: 'course.id',
    validators: { isMongoId: true }
  }, {
    name: 'course.register',
    validators: { isISO8601: true }
  }]
};

const vv = vulcanval(settings);

const nameResult = vv.validateFieldset('user', map);
console.log(nameResult);
// { 'user.name': 'This field should only contain lowercase text.' }

const ageResult = vv.validateFieldset('course', map);
console.log(ageResult);
// { 'course.register': 'Please type a valid date.' }