xyjax-object-validatordeprecated

Simple and powerful validator for javascript objects

Usage no npm install needed!

<script type="module">
  import xyjaxObjectValidator from 'https://cdn.skypack.dev/xyjax-object-validator';
</script>

README

npm npm bundle size npm NPM

About

xyjax-object-validator is the lightweight and standalone tool for javascript objects validation. You can validate fields list and fields values independently. This package was originally developed for Xyjax NPM package.

Methods

There is one method you can import:

1. validate(configuration)(target)

Validates an object with the given validation config.

Arguments description:

  • configuration is the object will be used as validation rules. It consists of two optional fields: fieldsList and fieldsValues (see examples below)
  • target is the object will be validated

Configuration object

Following is example of correct configuration object:

{
    // checks of fields list
    fieldsList: {
        //possible checks - mustOnly, must, mustNot, shouldOnly, should, shouldNot
        must: ['counter']
    },
    //checks of field values
    fieldsValues: {
        //consists of object of following structure: { fieldName: checksObject }
        //checksObject consists of two optional fields: must and should
        //checkObject.must is an array of required checks
        //checkObject.should is an array of optional checks
        //check is an object of following structure: {title, check}
        counter: {
            must: [
                {title: 'beNumber', check: (x) => typeof(x) == 'number'},
                {title: 'bePositive', check: (x) => x > 0}
            ],
            should: [
                {title: 'beLessThanThousand', check: (x) => x < 1000}
            ]
        }
    }
}

Usage example

Fields list example

import { validate } from 'xyjax-object-validator'
const validationConfig = {
    fieldsList: {
        must: ['a'],
        should: ['b', 'c']
    }
}
validate(validationConfig)({a: 0, b: 1, c: 2}) //success
validate(validationConfig)({a: 0, b: 1}) //warning, 'c' field is missing
validate(validationConfig)({b: 1, c: 2}) //throws the error, 'a' field is missing

Complex example

Click here to take a look at RunKit + NPM embed example.

import { validate } from 'xyjax-object-validator'
const validationConfig = {
    fieldsList: {
        must: ['a']
    },
    fieldsValues: {
        a: {
            must: [
                {title: 'number', check: (x) => typeof(x) == 'number'},
                {title: 'positive', check: (x) => x > 0}
            ],
            should: [
                {title: '> 10', check: (x) => x > 10}
            ]
        }
    }
}
validate(validationConfig)({a: 0, b: 1, c: 2}) //throws the error, 'a' is not positive
validate(validationConfig)({a: 'two', b: 1}) //throws the error, 'a' is not a number
validate(validationConfig)({b: 1, c: 2}) //throws the error, there is no 'a' field
validate(validationConfig)({a: 5}) //warning, 'a' field is less than 10
validate(validationConfig)({a: 100}) //success