jaywalk
Version:
Runtime type validation
72 lines • 2.5 kB
JavaScript
;
var test = require('blue-tape');
var index_1 = require('../index');
test('union', function (t) {
t.test('basic', function (t) {
var schema = new index_1.Types.Union({
types: [
new index_1.Types.Object({
properties: {
userId: new index_1.Types.String()
}
}),
new index_1.Types.Object({
properties: {
profileId: new index_1.Types.String()
}
})
]
});
var validate = index_1.compile(schema);
t.test('accept and return valid input', function (t) {
return validate({
userId: 'abc'
})
.then(function (result) {
t.equal(result.userId, 'abc');
});
});
t.test('reject invalid input', function (t) {
t.plan(4);
return validate({ foo: 'bar' })
.catch(function (err) {
t.equal(err.errors.length, 3);
t.deepEqual(err.errors[0].path, ['userId']);
t.deepEqual(err.errors[1].path, ['profileId']);
t.deepEqual(err.errors[2].path, []);
});
});
});
t.test('most specific path', function (t) {
var schema = new index_1.Types.Union({
types: [
new index_1.Types.Object({
properties: {
userId: new index_1.Types.String()
}
}),
new index_1.Types.Object({
properties: {
userId: new index_1.Types.String(),
profileId: new index_1.Types.String()
}
})
]
});
var validate = index_1.compile(schema);
t.test('validate with matching schema', function (t) {
return validate({ userId: 'test' })
.then(function (res) {
t.equal(res.userId, 'test');
});
});
t.test('validate with more specific schema', function (t) {
return validate({ userId: 'test', profileId: 'more' })
.then(function (res) {
t.equal(res.userId, 'test');
t.equal(res.profileId, 'more');
});
});
});
});
//# sourceMappingURL=union.spec.js.map