forked from raisch/chai-joi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
251 lines (228 loc) · 6.36 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Created by raisch on 4/16/15. */
/*jshint node:true, bitwise:true, camelcase:false, curly:true, undef:false, unused:false, eqeqeq:true, shadow:true, expr:true */
/*global define, require*/
'use strict';
//@formatter:off
/** @ignore */
var _ = require('lodash'),
chai = require('chai');
//@formatter:on
/**
* @module chai-joi
* @version 0.0.5
* @mixes chai
*
* @description
* Extends Chai with Joi specific assertions.
*
* See test/chai-joi.js for possible uses.
*
* @example
*
* var joi = require('joi'),
* chai = require('chai')
* expect = chai.expect;
*
* chai.should();
*
* chai.use( require('chai-joi') );
*
* var data={
* str:'foo',
* num: 100
* };
*
* var schema=joi.object({
* str:joi.string().allow(''),
* num:joi.number().min(1).max(10)
* });
*
* var result=joi.validate(data,schema);
*
* result.should.not.validate;
* result.should.have.errmsg('"value" must be less than or equal to 10');
*
* data.num=1;
* result=joi.validate(data,schema);
*
* expect(result).to.validate;
* expect(result).to.not.have.errmsgs; // same thing, really.
*
*/
/**
* Returns a (possibly empty) array of error messages from a Joi.validate response.
* @param {object} result
* @returns {*|Array}
* @private
*/
var getErrmsgs = function (result) {
var err = result.error || {},
details = err.details || [],
errmsgs = _.pluck(details, 'message');
return errmsgs || [];
};
/**
* @namespace assert
*
* @description
*
* Extension to chai.assert to test for a valid Joi validation result.
*
*/
//noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
/**
* Asserts that the target object is result of a call to Joi.validate()
* @param {object} target
* @returns {boolean}
*/
function isValidation(target) { //stub for jsdoc
return true;
}
/**
* @namespace property
*
* @description
*
* Chainable properties.
*
*/
/**
* Assert that target is a Joi validation
* @example
* expect(target).to.[not].be.a.validation
* target.should.[not].be.a.validation
*/
var validation = function () {
var target = this._obj;
this.assert(_.isObject(target), '#{this} is not a Joi validation because it must be an object');
this.assert(!_.isEmpty(target), '#{this} is not a Joi validation because it is an empty object');
var fields = _.keys(target);
this.assert(_.contains(fields, 'value', 'error'), '#{this} is not a Joi validation because it is missing required keys');
target = _.omit(target, ['error', 'value', 'then', 'catch']);
this.assert(_.isEmpty(target), '#{this} is not a validation because it contains unexpected keys');
};
/**
* Assert that target validates correctly
* @example
* expect(target).should.[not].validate
* target.should.[not].validate
*/
var validate = function () {
var target = this._obj;
chai.assert.isValidation(target);
this.assert(_.has(target, 'error') && null === target.error,
'#{this} should validate but does not because '+getErrmsgs(target),
'#{this} should not validate but it does'
);
};
/**
* Assert that target contains one or more errors (unsuccessful validation).
* Mutates current chainable object to be target.error.
* @example
* expect(target).to.[not].have.an.error
* target.should.[not].have.an.error
*/
var error = function (utils) {
var target = this._obj;
chai.assert.isValidation(target);
var error = target.error || null,
json = JSON.stringify(target, null, '\t');
this.assert(null !== error,
'#{this} should have error but does not: ' + json,
'#{this} should not not have error but does: ' + json
);
utils.flag(this, 'object', error);
};
/**
* Assert that target contains a value.
* Mutates current chainable object to be target.value.
* @example
* expect(target).to.[not].have.a.value
* target.should.[not].have.a.value
*/
var value = function value(utils) {
var target = this._obj,
value = target.value || null;
chai.assert.isValidation(target);
this.assert(null !== value,
'#{this} should have value',
'#{this} should not have value'
);
utils.flag(this, 'object', value);
};
/**
* Assert that target contains one or more error messages (unsuccessful validation).
* Mutates current chainable object to be list {String[]} of error messages.
*
* @example
* expect(target).to.[not].have.errmsgs
* target.should.[not].have.errmsgs
* expect(target).to.[not].have.errmsgs.that.include(errmsg)
* target.should.have.errmsgs.that.include(errmsg)
*/
var errmsgs = function errmsgs(utils) {
var obj = this._obj,
errmsgs = getErrmsgs(obj);
this.assert(
'array' === utils.type(errmsgs) && errmsgs.length > 0,
'expected #{this} to have errmsgs',
'expected #{this} to not have errmsgs'
);
utils.flag(this, 'object', errmsgs);
};
/**
* @namespace method
*
* @description
*
* Chainable methods.
*
*/
/**
* Assert that target contains specified error message (unsuccessful validation).
* @param {String} msg - errmsg to match
* @example
* target.should.[not].have.errmsg(msg)
*/
var errmsg = function errmsg(msg) {
var obj = this._obj,
errmsgs = getErrmsgs(obj);
this.assert(
_.contains(errmsgs, msg),
'expected #{this} to have an error message: #{errmsg}',
'expected #{this} to not an error message: #{errmsg}',
msg, // expected
errmsgs // actual
);
};
//@formatter:off
var chai_joi=function (_chai, utils) { // plugin
var Assertion = _chai.Assertion;
// properties
Assertion.addProperty('validation', validation );
Assertion.addProperty('validate', validate );
Assertion.addProperty('value', function () { _.bind(value, this)(utils); });
Assertion.addProperty('error', function () { _.bind(error, this)(utils); });
Assertion.addProperty('errmsgs', function () { _.bind(errmsgs, this)(utils); });
// methods
Assertion.addMethod('errmsg', errmsg);
// raw assertions
_chai.assert.isValidation = function (obj, msg) {
//noinspection BadExpressionStatementJS
new Assertion(obj, msg).is.a.validation;
};
};
//@formatter:on
(function (plugin) {
if (_.isFunction(require) && _.isObject(exports) && _.isObject(module)) { // node
module.exports = plugin;
}
else if (_.isFunction(define) && define.amd) { // amd
define(function () {
return plugin;
});
}
else { // other
chai.use(plugin);
}
}(chai_joi));