0.2.0 • Published 3 years ago

@mbokinala/data-validator v0.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Data Validation

A (work in progress) data validation library designed to validate JSON data for REST APIs and provide useful validation error output

Installation

npm install @mbokinala/data-validator

or

yarn add @mbokinala/data-validator

Example

const {validate, isDefined, isNotEmpty, isType, isGreaterThan, validateArray, includes} = require('@mbokinala/data-validator');

let data = {
    age: 18,
    profession: false,
    favoriteLanguages: [
        'java',
        'go',
        12,
        'python'
    ]
}

let [valid, errors] = validate(data, {
    name: [isDefined, isType('string'), isNotEmpty],
    age: [isDefined, isType('number'), isGreaterThan(21)],
    profession: [isDefined, isType('string')],
    favoriteLanguages: [isDefined, validateArray([isDefined, isType('string')]), includes('typescript')]
})

console.log('Validation result is', valid);

if (!valid) {
    console.log(JSON.stringify(errors, null, 4));
} else {
    console.log('valid');
}

Output:

Validation result is false
{
    "name": [
        "is null or undefined",
        "is not of type `string`",
        "is empty"
    ],
    "age": [
        "is not greater than 21"
    ],
    "profession": [
        "is not of type `string`"
    ],
    "favoriteLanguages": [
        {
            "2": [
                "is not of type `string`"
            ]
        },
        "does not include `typescript`"
    ]
}

Usage

validate(data, validators) => [valid: boolean, errors? ValidationErrors] The library is designed so that you can return errors as a JSON object in something like a REST API.

validators should match the structure of the data you want to validate. For each key, the value should be an array of validator functions to be run on the field. There are some built-in validator functions (see below), but you can create custom validators using the format:

// valid: true if input passes the validation test, false if otherwise
// message: an error message to be displayed if validation fails
(input: any) => [valid: boolean, message?: string];

Built-in validators:

FunctionDescription
isDefinedPasses if input is not null or undefined
isType(type: string)Passes if input matches provided type
isArrayPasses if input is an array
isNotEmptyPasses if input string or array is not empty
equals(val: any)Passes if input matches provided value
includes(element: any)Passes if input array contains provided element
isGreaterThan(val: number)Passes if input is greater than provided value
isGreaterThanOrEqualTo(val: number)Passes if input is greater than or equal to provided value
isLessThan(val: number)Passes if input is less than provided value
isLessThanOrEqualTo(val: number)Passes if input is less than or equal to provided value
validateArray(validators: ValidatorFunction[])Passes if all elements in input array pass all provided validators
ifDefined(validators: ValidatorFunction[])Passes if input is defined or if input passes all provided validators. Useful for when a validator is only meant to be run when a value is defined.
0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago