Skip to content

Parameter Validator

View in BitBucket

Parameters

Object or string - instanceOrName

Either this or a string representing the name of the function that validator is executed in. Used to enhance the error messages that are thrown.

Return Value

A ParameterValidator instance.

Methods

Type Checks

All type check methods contain the following params/options:

  • key - The name of the property to test
  • value - The value of the property to test
  • Options:
    • required - whether to first check if value is defined
    • throwError - whether to throw a TypeError or not
    • returnValue - whether to return the value on success
    • returnMessage - whether to return the message on success

  • isArray
  • isBoolean
  • isDefined
  • isElement - adds additional option, container; defaults to document
  • isElementList - adds additional option, container; defaults to document
  • isFloat
  • isFunction
  • isInstance (e.g. constructor = HTMLFormElement)
  • isInt
  • isNumber
  • isObject
  • isRegExp
  • isString
  • isUrl - adds additional option, isRelativeUrl; defaults to false; set to true for testing relative path URLs.

Other

  • inSet - Check if value exists within the array provided
  • validate - Validate an object or an array against a "ruleset"

Example Usage

Type Checks

import ParameterValidator from '@mvps-genesis/parameter-validator';

class Example {

    constructor (a, b, c, d, e) {
        const validator = new ParameterValidator(this);

        const aReturnValue = validator.isString('a', a);
        const bReturnValue = validator.isNumber('b', b);
        const cReturnValue = validator.isObject('c', c);
        const dReturnValue = validator.isFunction('d', d);
        const eReturnValue = validator.isElement('e', e, {
            container: '#my-container'
        });
    }

}

// PASS!
new Example(
    'myString',
    123,
    { foo: 'bar' },
    () => console.log( 'Hello World!' ),
    '#my-element'
);

// FAIL! error = TypeError: Example :: `a` must be a string
new Example(
    123,
    ...
    ...
)

Other

import ParameterValidator from '@mvps-genesis/parameter-validator';

const validator = new ParameterValidator('Example');

// ==== inSet ==== //

// PASS! 'foo' is within the set
validator.inSet('myValue', 'foo', [
    'foo',
    'bar',
    'baz'
]);

// FAIL! 'foo' is not in the set
validator.inSet('myValue', 'foo', [
    'bar',
    'baz'
]);

// ==== validate ==== //

const myRules = {
    products: [
        {
            code: 'String',
            customfield_values: {
                customfields: {
                    color: 'String'
                }
            },
            id: 'Int',
            name: 'String',
            // Optional property descriptions include the `?`
            url: 'URL?'
        }
    ],
    productsPerPage: 'Int'
};

const myData = {
    products: [
        {
            code: 'product-1',
            customfield_values: {
                customfields: {
                    color: 'red'
                }
            },
            id: 1,
            name: 'Product 1'
        },
        {
            code: 'product-2',
            customfield_values: {
                customfields: {
                    color: 'blue'
                }
            },
            id: 2,
            name: 'Product 2'
        }
    ],
    productsPerPage: 12
};

validator.validate('myData', myData, myRules);