New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

required-keys

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

required-keys

make sure the desired key value pairs exist in a given object


Version published
Weekly downloads
20
increased by11.11%
Maintainers
1
Weekly downloads
 
Created

Required-Keys

Make sure the desired key value pairs exist in a given object

Installation

npm install -S required-keys

Usage

Async

You must pass a callback where the error parameter will be null if there are no errors rk.truthy

var rk = require('required-keys');
var keys = ['foo', 'bar'];
var data = {
  foo: false,
  bar: 'test'
}
rk.truthy(data, keys, function(err) {
  console.log('error', err);
});

rk.nonNull

var rk = require('required-keys');
var data = {
  foo: false,
  bar: 'test'
}
var keys = ['foo', 'bar'];
rk.nonNull(data, keys, function(err) {
  console.log('error should be null:', err);
});

rk.keysOnly

var rk = require('required-keys');
var data = {
  foo: null,
  bar: undefined
}
var keys = ['foo', 'bar'];
rk.keysOnly(data, keys, function(err) {
  console.log('error should be null:', err);
});

Sync

All sync methods return null if all checks pass or an array of errors

rk.truthySync

var keys = ['dog', 'cat', 'lemon']
var data = {
  dog: 'dog',
  cat: 'cat',
  lemon: true,
  apple: ''
}
// err is either null if all keys map to truthy values or an array of errors
var err = rk.truthySync(data, keys)

rk.nonNullSync

var keys = ['dog', 'cat', 'lemon']
var data = {
  dog: 'dog',
  cat: 'cat',
  lemon: false
}
// err is either null if all keys map to truthy values or an array of errors
var err = rk.nonNullSync(data, keys)
should.not.exist(err);

rk.keysOnlySync

var keys = ['dog', 'cat', 'lemon']
var data = {
  dog: null,
  cat: undefined,
  fruit: 'banana'
}
// err is either null if all keys map to truthy values or an array of errors
var err = rk.keysOnlySync(data, keys)

Keywords

FAQs

Package last updated on 09 Apr 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts