Skip to content

mrbrunelli/object-validator

Repository files navigation

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.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy