@mrbrunelli/object-validator
TypeScript icon, indicating that this package has built-in type declarations

2.0.8 • Public • Published

Object Validator

A Javascript object validator for tiny schemas.

NPM

Test

Get started

 yarn add @mrbrunelli/object-validator

How to use

import Validate from '@mrbrunelli/object-validator'

const objectExample = {
  foo: {
    bar: {
      message: 'Hello!'
    }
  }
}

const isValid = Validate.isValid(objectExample, [
  ['foo'],
  ['foo.bar.message', 'Hello!']
])
console.log(isValid) // returns true

Why

Validating multiple fields can be very tiring in old Node versions.

  // Work only Node 14 or >
  if (objectExample?.foo?.bar?.message === 'Hello!') // anything...

Node 12 require massive validations.

  if (objectExample && objectExample.foo && objectExample.foo.bar && objectExample.foo.bar.example === 'Hello!') // anything...

In this case, using a validator is better and safe.

  if (Validator.isValid(objectExample, [['foo.bar.example', 'Hello!']])) // anything...

Now imagine validating multiple fields and values ​​from a single object in a old version of Node. Very tiring and verbose.

Package Sidebar

Install

npm i @mrbrunelli/object-validator

Weekly Downloads

1

Version

2.0.8

License

MIT

Unpacked Size

6.82 kB

Total Files

12

Last publish

Collaborators

  • mrbrunelli