object-validatordb
v1.0.34
Published
Library to validate an object from object rules
Downloads
18
Maintainers
Readme
This library allows you to easily check if an object has the names of its keys and expected value types. github: https://github.com/DiegoBreeg/object-validator.git
$ npm i object-validatordb
Import or require the library to your code and instantiate
const {ObjectValidator} = require('object-validatordb')
const validator = new ObjectValidator()
import { ObjectValidator } from "object-validatordb"
const validator = new ObjectValidator()
Validator has a method called validate()
that takes two parameters.
-dummy: which will receive the object to be validated.
-rules: an object with the validation rules.
If dummy follows the rules described in rules validator returns true, otherwise it returns false
ObjectValidator.validate(dummy: any, rule: any): boolean
const dummy = { name: 'Joe', lastName: 'doe', age: 27}
const rules = {name: String, lastName: String, age: Number}
validator.validate(dummy, rules) //true
const dummy = { name: 'Joe', lastName: 'doe'}
const rules = {name: String, lastName: String, age: Number}
validator.validate(dummy, rules) //false
Rules also accepts Arrays and Objects.
const dummy = {
name: 'Joe',
lastName: 'Doe',
age: 27,
hobbies: ['programing', 'read books', 'commit to github' ]
}
const rules = {
name: String,
lastName: String,
age: Number,
hobbies: []
}
validator.validate(dummy, rules) //true