Skip to content

Check objects for the existence of certain keys βœ… πŸ“

Notifications You must be signed in to change notification settings

JakeDawkins/object-key-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

object-key-validator

npm version

check objects for the existence of certain keys

API

validateKeys

(rule: Rule, obj: Object) => boolean

Accepts a rule definition and a test object. If the rule definition is invalid, the validation function will throw.

Rule Types

There are three rule types: $and, $or, and $not. Rules can be nested (as shown below).

$and

Checks an array of rules. If all of the rules pass, validation returns true.

Examples

// checks to make sure an object has BOTH `a` AND `b` keys
// logically: (a && b)
{ $and: ['a', 'b'] }

// checks to make sure an object has `a`, `b`, AND `c` keys
// logically: (a && (b && c))
{
  $and: [
    'a',
    { $and: ['b', 'c']}
  ]
}

$or

Checks an array of rules. If one or more of the rules pass, validation returns true.

Examples

// checks to make sure an object has EITHER `a` OR `b` keys
// logically: (a || b)
{ $or: ['a', 'b'] }

// checks to make sure an object has either an `a` key OR a `b` AND `c` key
// logically: (a || (b && c))
{
  $or: [
    'a',
    { $and: ['b', 'c']}
  ]
}

$not

Inverts the response value of a single rule. $not does not accept an array of conditions like $and and $or.

Examples

// checks to make sure an object doesn't have EITHER `a` OR `b` keys
// logically: !(a || b)
{ $not: { $or: ['a', 'b'] } }

// checks to make sure an object doesn't have either an `a` key OR a `b` AND `c` key
// logically: !(a || (b && c))
{
  $not: {
    $or: [
      'a',
      { $and: ['b', 'c']}
    ]
  }
}

About

Check objects for the existence of certain keys βœ… πŸ“

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published