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
npm install --save-dev tape-schema
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()
})
Validate schema agains given object
Specifies that the input is an typeof string.
const schemaObject = {
name: schema.string
}
Specifies that the input is number.
const schemaObject = {
discount: schema.number
}
Specifies that the input is natural number. (including 0)
const schemaObject = {
age: schema.naturalNumber
}
Specifies that the input is boolean.
const schemaObject = {
isOnline: schema.boolean
}
Specifies that the input is any value in array.
const schemaObject = {
task: schema.any([null, schema.string, 'assigned'])
}
Specifies that the input is latitude.
const schemaObject = {
latitude: schema.latitude
}
Specifies that the input is longitude.
const schemaObject = {
longitude: schema.longitude
}
Specifies that the input is typeof function.
const schemaObject = {
foo: schema.func
}
Specifies that the input shoud be validated by regex test
const schemaObject = {
string: schema.regexTest(/^Edgars$/)
}
Specifies that the input shoud be typeof undefined
const schemaObject = {
string: schema.undef
}