has-required-props
v0.0.4
Published
Assert that your object props have non-undefined values for each required prop
Downloads
3
Maintainers
Readme
has-required-props
Assert that your object props have non-undefined values for each required prop
Install
$ npm install --save has-required-props
Not using Node or a module bundler? Use a UMD build via the <script>
tag.
- https://unpkg.com/has-required-props/dist/has-required-props.js
- https://unpkg.com/has-required-props/dist/has-required-props.min.js
Usage
import hasRequiredProps from 'has-required-props'
let props = { prop1: 'a', prop2: 'b' }
let requiredProps = ['prop1', 'prop2']
hasRequiredProps(requiredProps, props) // true
hasRequiredProps(requiredProps, [props, props]) // true
requiredProps = ['prop1', 'prop2', 'prop3']
hasRequiredProps(requiredProps, props) // false
// go crazy
props = {
a: {
b: 1,
c: {
d: 2
}
},
e: 3,
f: [
3,
{
g: 1,
h: {}
}
]
}
requiredProps = ['a.b', 'a.c.d', 'e', 'f.0', 'f.1.g', 'f.1.h']
hasRequiredProps(requiredProps, props) // true
// keep it simple
props = { a: 1, b: 2 }
requiredProps = 'a'
hasRequiredProps(requiredProps, props) // true
props.a = undefined
hasRequiredProps(requiredProps, props) // false
props.a = null
hasRequiredProps(requiredProps, props) // true
// curry on
const curry = require('lodash/curry')
const duckCharacteristics = ['bill', 'plumage', 'webbedFeet']
const quacksLikeADuck = curry(hasRequiredProps(duckCharacteristics))
quacksLikeADuck({ bill: {}, plumage: {}, webbedFeet: {} }) // true
quacksLikeADuck({ bill: {}, plumage: {} }) // false