Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Add type validator #80

@Jokero

Description

@Jokero

Basic types:

  • object (plain object)
  • array
  • integer
  • number
  • string
  • boolean
  • null

Example:

validate.single([1, 2, 3], { type: 'array' }); // undefined
validate.single('123', { type: 'array' }); // ['is not an array']

User should be able to add own or modify existing types:

validate.validators.type.options = { notIntegerArray: 'is not an array of integers' };

// validate.validators.type.checks contains all types validation rules
validate.validators.type.checks.integerArray = function(value) {
    if (!Array.isArray(value)) {
        return false;
    }

    for (var i in value) {
        if (!Number.isInteger(value[i])) {
            return false;
        }
    }

    return true;
};

// ...

validate.single([1, 2, 3], { type: 'integerArray' }); // undefined
validate.single(['1', '2', '3'], { type: 'integerArray' }); // ['is not an array of integers']

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions