is-my-json-valid
Version:
A JSONSchema / orderly validator that uses code generation to be extremely fast
133 lines (110 loc) • 2.5 kB
JavaScript
var tape = require('tape')
var cosmic = require('./fixtures/cosmic')
var validator = require('../')
var validatorRequire = require('../require')
tape('orderly', function(t) {
var validate = validatorRequire('./fixtures/test.schema')
t.ok(validate({
name: 'test'
}), 'should be valid')
t.ok(validate({
name: 53
}), 'should be valid')
t.notOk(validate({
name: false
}), 'should be invalid')
t.same(validate.errors, [{field:'data.name', message:'must be a string or a number'}])
t.notOk(validate(), 'should be invalid')
t.end()
})
tape('simple', function(t) {
var schema = {
type: 'object',
properties: {
hello: {type:'string', required:true}
}
}
var validate = validator(schema)
t.ok(validate({hello: 'world'}), 'should be valid')
t.notOk(validate(), 'should be invalid')
t.notOk(validate({}), 'should be invalid')
t.end()
})
tape('advanced', function(t) {
var validate = validator(cosmic.schema)
t.ok(validate(cosmic.valid), 'should be valid')
t.notOk(validate(cosmic.invalid), 'should be invalid')
t.end()
})
tape('additional props', function(t) {
var validate = validator({
type: 'object',
additionalProperties: false
})
t.ok(validate({}))
t.notOk(validate({foo:'bar'}))
t.end()
})
tape('array', function(t) {
var validate = validator({
type: 'array',
required: true,
items: {
type: 'string'
}
})
t.notOk(validate({}), 'wrong type')
t.notOk(validate(), 'is required')
t.ok(validate(['test']))
t.end()
})
tape('nested array', function(t) {
var validate = validator({
type: 'object',
properties: {
list: {
type: 'array',
required: true,
items: {
type: 'string'
}
}
}
})
t.notOk(validate({}), 'is required')
t.ok(validate({list:['test']}))
t.notOk(validate({list:[1]}))
t.end()
})
tape('enum', function(t) {
var validate = validator({
type: 'object',
properties: {
foo: {
type: 'number',
required: true,
enum: [42]
}
}
})
t.notOk(validate({}), 'is required')
t.ok(validate({foo:42}))
t.notOk(validate({foo:43}))
t.end()
})
tape('minimum/maximum', function(t) {
var validate = validator({
type: 'object',
properties: {
foo: {
type: 'number',
minimum: 0,
maximum: 0
}
}
})
t.notOk(validate({foo:-42}))
t.ok(validate({foo:0}))
t.notOk(validate({foo:42}))
t.end()
})