jaywalk
Version:
Runtime type validation
46 lines • 1.59 kB
JavaScript
;
var test = require('blue-tape');
var index_1 = require('../index');
test('array', function (t) {
t.test('min items', function (t) {
var schema = new index_1.Types.Array({
items: new index_1.Types.Any(),
minItems: 2
});
var validate = index_1.compile(schema);
t.test('should type check', function (t) {
t.equal(index_1.is(schema, []), 0);
t.equal(index_1.is(schema, ['test', 'more']), 4);
t.equal(index_1.is(schema, 123), 0);
t.end();
});
t.test('should fail with min items', function (t) {
t.plan(2);
return validate([1])
.catch(function (err) {
t.equal(err.errors.length, 1);
t.deepEqual(err.errors[0].keyword, 'minItems');
});
});
t.test('should pass with greater than min value', function (t) {
return validate([1, 2, 3]);
});
});
t.test('map items to new types', function (t) {
var schema = new index_1.Types.Array({
items: new index_1.Types.Object({
properties: {
a: new index_1.Types.String()
}
})
});
var validate = index_1.compile(schema);
t.test('use validated objects as result', function (t) {
return validate([{ a: 'a', b: 'b' }])
.then(function (res) {
t.deepEqual(res, [{ a: 'a' }]);
});
});
});
});
//# sourceMappingURL=array.spec.js.map