object-validatordb
TypeScript icon, indicating that this package has built-in type declarations

1.0.34 • Public • Published

Object-Validator

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

Usage

Install this library:
$ 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

Package Sidebar

Install

npm i object-validatordb

Weekly Downloads

1

Version

1.0.34

License

ISC

Unpacked Size

20.4 kB

Total Files

11

Last publish

Collaborators

  • diegobreeg