1.0.34 • Published 5 years ago

xyjax-object-validator v1.0.34

Weekly downloads
13
License
MIT
Repository
github
Last release
5 years ago

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
1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.1

5 years ago