tape-schema
v1.1.4
Published
tap-producing test harness schema validation for node
Downloads
79
Readme
tape-schema
Schema validation for node.js inside tape test harness.
- compare javascript object to a schema
- combine schemas to make testing more consistent
- pinpoint error, even in deeply nested objects
- powerful any function provides flexibility
- validate missing schema definition, error if an object contains more data than schema
install
npm install --save-dev tape-schema
example
const test = require('tape')
const schema = require('tape-schema')
const schemaObject = {
id: schema.number,
name: schema.string,
age: schema.any([null, schema.naturalNumber]),
friendList: [
{
id: schema.number,
name: schema.string
}
]
}
const object = {
id: 1,
name: 'Edgars',
age: null,
friendList: [
{
id: 2,
name: 'Joe'
},
{
id: 3,
name: 'Kate'
}
]
}
test('Readme test', t => {
schema.test(t, schemaObject, object)
t.end()
})
API
schema.test([tape], [schema], [object])
Validate schema agains given object
string
Specifies that the input is an typeof string.
const schemaObject = {
name: schema.string
}
number
Specifies that the input is number.
const schemaObject = {
discount: schema.number
}
naturalNumber
Specifies that the input is natural number. (including 0)
const schemaObject = {
age: schema.naturalNumber
}
boolean
Specifies that the input is boolean.
const schemaObject = {
isOnline: schema.boolean
}
any([values])
Specifies that the input is any value in array.
const schemaObject = {
task: schema.any([null, schema.string, 'assigned'])
}
latitude
Specifies that the input is latitude.
const schemaObject = {
latitude: schema.latitude
}
longitude
Specifies that the input is longitude.
const schemaObject = {
longitude: schema.longitude
}
func
Specifies that the input is typeof function.
const schemaObject = {
foo: schema.func
}
regexTest
Specifies that the input shoud be validated by regex test
const schemaObject = {
string: schema.regexTest(/^Edgars$/)
}
undef
Specifies that the input shoud be typeof undefined
const schemaObject = {
string: schema.undef
}