0.0.5 • Published 5 years ago
@packmate/assure v0.0.5
@packmate/assure
Simple Node.js object property validations, with companion Chai assertions.
Installation
npm install @packmate/assure
The Validation Function
Usage
const { assureProperties } = require('@packmate/assure')
Syntax
assureProperties(properties, target)Example Usage
const object = { a: 1, b: 2, c: 3 }
assureProperties([ 'a', 'b', 'c' ], { of: object, type: 'arbitrary' })Arguments
| Name | Type | Description |
|---|---|---|
| properties | Array | A list of properties to validate. |
| target | Object: Target | Information about the object to validate. |
Returns
| Type | Description |
|---|---|
| Object | The targeted object. |
Exceptions
Throws a standard Error if all properties are not present in the target object.
The Target Object
| Attribute | Type | Description |
|---|---|---|
| of | Object | The object to validate. |
| type | String (optional) | A name for the type of object to include in error messages. |
Chai Assertions
const chai = require('chai')
const { isAssured } = require('@packmate/assure')
chai.use(isAssured).assuredBy()
Checks if all an object's properties are assured within a test function.
Syntax
assuredBy(test, exclusions)Arguments
| Name | Type | Description |
|---|---|---|
| test | Function | A testing function which will be passed one or more invalid version(s) of an object. |
| exclusions | Object: Exclusions | Fields not to validate. |
Example Usage
function eat(food) {
assureProperties([ 'flavor', 'temperature' ], { of: food })
}
// Passing test.
it('only accepts valid foods', () => {
expect({ flavor: 'salty', temperature: 'hot', texture: 'smooth' }).to.be.assuredBy(invalidObject => {
eat(invalidObject)
}, { exclude: 'texture' })
})
// Failing test.
it('only accepts valid foods', () => {
expect({ color: 'brown' }).to.be.assuredBy(invalidObject => {
eat(invalidObject)
})
})The Exclusions Object
| Attribute | Type | Description |
|---|---|---|
| exclude | Array | A list of fields to exclude from validation. |
.assuredByAsync()
The same as .assuredBy(), but accepts an asynchronous test function.
Example Usage
async function sendDataAbout(food) {
assureProperties([ 'flavor', 'temperature' ], { of: food })
await MyFoodAPI.post({ food })
}
// Passing test.
it('only accepts valid foods', () => {
return expect({ flavor: 'salty', temperature: 'hot', texture: 'smooth' }).to.be.assuredByAsync(async (invalidObject) => {
await sendDataAbout(invalidObject)
}, { exclude: 'texture' })
})
// Failing test.
it('only accepts valid foods', () => {
return expect({ color: 'brown' }).to.be.assuredByAsync(async (invalidObject) => {
await sendDataAbout(invalidObject)
})
})