jaywalk
Version:
Runtime type validation
73 lines • 2.58 kB
JavaScript
;
var test = require('blue-tape');
var index_1 = require('../index');
test('intersection', function (t) {
t.test('basic', function (t) {
var schema = new index_1.Types.Intersection({
types: [
new index_1.Types.Object({
properties: {
username: new index_1.Types.String()
}
}),
new index_1.Types.Object({
properties: {
password: new index_1.Types.String()
}
})
]
});
var validate = index_1.compile(schema);
t.test('accept valid input', function (t) {
return validate({
username: 'blakeembrey',
password: 'hunter2'
})
.then(function (result) {
t.equal(result.username, 'blakeembrey');
t.equal(result.password, 'hunter2');
});
});
t.test('reject invalid input', function (t) {
t.plan(2);
return validate({ username: 'blakeembrey' })
.catch(function (err) {
t.equal(err.errors.length, 1);
t.deepEqual(err.errors[0].path, ['password']);
});
});
});
t.test('union inside intersection', function (t) {
var schema = new index_1.Types.Intersection({
types: [
new index_1.Types.Object({
properties: {
username: new index_1.Types.String()
}
}),
new index_1.Types.Union({
types: [
new index_1.Types.Object({
properties: {
x: new index_1.Types.String()
}
}),
new index_1.Types.Object({
properties: {
y: new index_1.Types.String()
}
})
]
})
]
});
var validate = index_1.compile(schema);
t.test('return valid data from nested union', function (t) {
return validate({ username: 'test', y: 'test' })
.then(function (result) {
t.deepEqual(result, { username: 'test', y: 'test' });
});
});
});
});
//# sourceMappingURL=intersection.spec.js.map