-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
validate.spec.js
108 lines (99 loc) · 3.08 KB
/
validate.spec.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const Joi = require('joi')
const { expect } = require('chai')
const sinon = require('sinon')
const trace = require('../services/trace')
const { InvalidParameter } = require('../services/errors')
const validate = require('./validate')
describe('validate', function() {
const schema = Joi.object({
requiredString: Joi.string().required(),
}).required()
let sandbox
beforeEach(function() {
sandbox = sinon.createSandbox()
})
afterEach(function() {
sandbox.restore()
})
beforeEach(function() {
sandbox.stub(trace, 'logTrace')
})
const ErrorClass = InvalidParameter
const prettyErrorMessage = 'parameter does not match schema'
const traceErrorMessage = 'Params did not match schema'
const traceSuccessMessage = 'Params after validation'
const options = {
ErrorClass,
prettyErrorMessage,
traceErrorMessage,
traceSuccessMessage,
}
context('schema is not provided', function() {
it('throws the expected programmer error', function() {
try {
validate(options, { requiredString: 'bar' }, undefined)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Error)
expect(e.message).to.equal('A Joi schema is required')
}
})
})
context('data matches schema', function() {
it('logs the data', function() {
validate(options, { requiredString: 'bar' }, schema)
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceSuccessMessage,
{ requiredString: 'bar' },
{ deep: true }
)
})
})
context('data does not match schema', function() {
it('logs the data and throws the expected error', function() {
try {
validate(
options,
{ requiredString: ['this', "shouldn't", 'work'] },
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: child "requiredString" fails because ["requiredString" must be a string]'
)
expect(e.prettyMessage).to.equal(prettyErrorMessage)
}
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceErrorMessage,
'child "requiredString" fails because ["requiredString" must be a string]'
)
})
context('with includeKeys: true', function() {
it('includes keys in the error text', function() {
try {
validate(
{ ...options, includeKeys: true },
{ requiredString: ['this', "shouldn't", 'work'] },
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: child "requiredString" fails because ["requiredString" must be a string]'
)
expect(e.prettyMessage).to.equal(
`${prettyErrorMessage}: requiredString`
)
}
})
})
})
})