-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.js
54 lines (43 loc) · 1.41 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var assert = require('assert');
var Joi = require('joi');
var joiObjectId = require('./');
describe('joi-objectid', function() {
it('exports a function', function(done) {
assert.equal('function', typeof joiObjectId);
done();
});
it('expects to receive a Joi module', function(done) {
assert.throws(joiObjectId, /must pass Joi/);
done();
});
it('returns a validator function', function(done) {
var fn = joiObjectId(Joi);
assert.equal('function', typeof fn);
done();
});
it('requires an hexadecimal string of 24 chars', function(done) {
var oid = joiObjectId(Joi);
var tests = [
{ val: '$bcd56789012345678901234', pass: false }
, { val: ' bcd56789012345678901234', pass: false }
, { val: 'abcd5678901234567890123', pass: false }
, { val: 'abcd56789012345678901234', pass: true }
, { val: 123456789012345678901234, pass: false }
, { val: { length: 24 } , pass: false }
]
var schema = { val: oid() };
tests.forEach(function(test) {
var res = Joi.validate({ val: test.val }, schema);
assert(test.pass === ! res.error, res.error);
});
done();
});
it('includes custom message for invalid value', function(done) {
var dbId = joiObjectId(Joi, 'database id');
var result = Joi.validate('blah', dbId());
assert(result.error);
assert(result.error.message.indexOf('database id') >= 0);
done();
});
});