@depup/joi 18.1.1-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +11 -0
- package/README.md +31 -0
- package/changes.json +10 -0
- package/dist/joi-browser.min.js +1 -0
- package/lib/annotate.js +175 -0
- package/lib/base.js +1317 -0
- package/lib/cache.js +142 -0
- package/lib/common.js +234 -0
- package/lib/compile.js +283 -0
- package/lib/errors.js +271 -0
- package/lib/extend.js +317 -0
- package/lib/index.d.ts +2666 -0
- package/lib/index.js +282 -0
- package/lib/manifest.js +475 -0
- package/lib/messages.js +177 -0
- package/lib/modify.js +267 -0
- package/lib/ref.js +412 -0
- package/lib/schemas.js +304 -0
- package/lib/state.js +165 -0
- package/lib/template.js +461 -0
- package/lib/trace.js +346 -0
- package/lib/types/alternatives.js +430 -0
- package/lib/types/any.js +174 -0
- package/lib/types/array.js +937 -0
- package/lib/types/binary.js +124 -0
- package/lib/types/boolean.js +151 -0
- package/lib/types/date.js +279 -0
- package/lib/types/function.js +93 -0
- package/lib/types/keys.js +1170 -0
- package/lib/types/link.js +191 -0
- package/lib/types/number.js +399 -0
- package/lib/types/object.js +22 -0
- package/lib/types/string.js +986 -0
- package/lib/types/symbol.js +114 -0
- package/lib/validator.js +759 -0
- package/lib/values.js +262 -0
- package/package.json +68 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { assert, clone } = require('@hapi/hoek');
|
|
4
|
+
|
|
5
|
+
const Cache = require('./cache');
|
|
6
|
+
const Common = require('./common');
|
|
7
|
+
const Compile = require('./compile');
|
|
8
|
+
const Errors = require('./errors');
|
|
9
|
+
const Extend = require('./extend');
|
|
10
|
+
const Manifest = require('./manifest');
|
|
11
|
+
const Ref = require('./ref');
|
|
12
|
+
const Template = require('./template');
|
|
13
|
+
const Trace = require('./trace');
|
|
14
|
+
|
|
15
|
+
let Schemas;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const internals = {
|
|
19
|
+
types: {
|
|
20
|
+
alternatives: require('./types/alternatives'),
|
|
21
|
+
any: require('./types/any'),
|
|
22
|
+
array: require('./types/array'),
|
|
23
|
+
boolean: require('./types/boolean'),
|
|
24
|
+
date: require('./types/date'),
|
|
25
|
+
function: require('./types/function'),
|
|
26
|
+
link: require('./types/link'),
|
|
27
|
+
number: require('./types/number'),
|
|
28
|
+
object: require('./types/object'),
|
|
29
|
+
string: require('./types/string'),
|
|
30
|
+
symbol: require('./types/symbol')
|
|
31
|
+
},
|
|
32
|
+
aliases: {
|
|
33
|
+
alt: 'alternatives',
|
|
34
|
+
bool: 'boolean',
|
|
35
|
+
func: 'function'
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
if (Buffer) { // $lab:coverage:ignore$
|
|
41
|
+
internals.types.binary = require('./types/binary');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
internals.root = function () {
|
|
46
|
+
|
|
47
|
+
const root = {
|
|
48
|
+
_types: new Set(Object.keys(internals.types))
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Types
|
|
52
|
+
|
|
53
|
+
for (const type of root._types) {
|
|
54
|
+
root[type] = function (...args) {
|
|
55
|
+
|
|
56
|
+
assert(!args.length || ['alternatives', 'link', 'object'].includes(type), 'The', type, 'type does not allow arguments');
|
|
57
|
+
return internals.generate(this, internals.types[type], args);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Shortcuts
|
|
62
|
+
|
|
63
|
+
for (const method of ['allow', 'custom', 'disallow', 'equal', 'exist', 'forbidden', 'invalid', 'not', 'only', 'optional', 'options', 'prefs', 'preferences', 'required', 'strip', 'valid', 'when']) {
|
|
64
|
+
root[method] = function (...args) {
|
|
65
|
+
|
|
66
|
+
return this.any()[method](...args);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Methods
|
|
71
|
+
|
|
72
|
+
Object.assign(root, internals.methods);
|
|
73
|
+
|
|
74
|
+
// Aliases
|
|
75
|
+
|
|
76
|
+
for (const alias in internals.aliases) {
|
|
77
|
+
const target = internals.aliases[alias];
|
|
78
|
+
root[alias] = root[target];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
root.x = root.expression;
|
|
82
|
+
|
|
83
|
+
// Trace
|
|
84
|
+
|
|
85
|
+
if (Trace.setup) { // $lab:coverage:ignore$
|
|
86
|
+
Trace.setup(root);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return root;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
internals.methods = {
|
|
94
|
+
|
|
95
|
+
ValidationError: Errors.ValidationError,
|
|
96
|
+
version: Common.version,
|
|
97
|
+
cache: Cache.provider,
|
|
98
|
+
|
|
99
|
+
assert(value, schema, ...args /* [message], [options] */) {
|
|
100
|
+
|
|
101
|
+
internals.assert(value, schema, true, args);
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
attempt(value, schema, ...args /* [message], [options] */) {
|
|
105
|
+
|
|
106
|
+
return internals.assert(value, schema, false, args);
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
build(desc) {
|
|
110
|
+
|
|
111
|
+
assert(typeof Manifest.build === 'function', 'Manifest functionality disabled');
|
|
112
|
+
return Manifest.build(this, desc);
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
checkPreferences(prefs) {
|
|
116
|
+
|
|
117
|
+
Common.checkPreferences(prefs);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
compile(schema, options) {
|
|
121
|
+
|
|
122
|
+
return Compile.compile(this, schema, options);
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
defaults(modifier) {
|
|
126
|
+
|
|
127
|
+
assert(typeof modifier === 'function', 'modifier must be a function');
|
|
128
|
+
|
|
129
|
+
const joi = Object.assign({}, this);
|
|
130
|
+
for (const type of joi._types) {
|
|
131
|
+
const schema = modifier(joi[type]());
|
|
132
|
+
assert(Common.isSchema(schema), 'modifier must return a valid schema object');
|
|
133
|
+
|
|
134
|
+
joi[type] = function (...args) {
|
|
135
|
+
|
|
136
|
+
return internals.generate(this, schema, args);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return joi;
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
expression(...args) {
|
|
144
|
+
|
|
145
|
+
return new Template(...args);
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
extend(...extensions) {
|
|
149
|
+
|
|
150
|
+
Common.verifyFlat(extensions, 'extend');
|
|
151
|
+
|
|
152
|
+
Schemas = Schemas || require('./schemas');
|
|
153
|
+
|
|
154
|
+
assert(extensions.length, 'You need to provide at least one extension');
|
|
155
|
+
this.assert(extensions, Schemas.extensions);
|
|
156
|
+
|
|
157
|
+
const joi = Object.assign({}, this);
|
|
158
|
+
joi._types = new Set(joi._types);
|
|
159
|
+
|
|
160
|
+
for (let extension of extensions) {
|
|
161
|
+
if (typeof extension === 'function') {
|
|
162
|
+
extension = extension(joi);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.assert(extension, Schemas.extension);
|
|
166
|
+
|
|
167
|
+
const expanded = internals.expandExtension(extension, joi);
|
|
168
|
+
for (const item of expanded) {
|
|
169
|
+
assert(joi[item.type] === undefined || joi._types.has(item.type), 'Cannot override name', item.type);
|
|
170
|
+
|
|
171
|
+
const base = item.base || this.any();
|
|
172
|
+
const schema = Extend.type(base, item);
|
|
173
|
+
|
|
174
|
+
joi._types.add(item.type);
|
|
175
|
+
joi[item.type] = function (...args) {
|
|
176
|
+
|
|
177
|
+
return internals.generate(this, schema, args);
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return joi;
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
isError: Errors.ValidationError.isError,
|
|
186
|
+
isExpression: Template.isTemplate,
|
|
187
|
+
isRef: Ref.isRef,
|
|
188
|
+
isSchema: Common.isSchema,
|
|
189
|
+
|
|
190
|
+
in(...args) {
|
|
191
|
+
|
|
192
|
+
return Ref.in(...args);
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
override: Common.symbols.override,
|
|
196
|
+
|
|
197
|
+
ref(...args) {
|
|
198
|
+
|
|
199
|
+
return Ref.create(...args);
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
types() {
|
|
203
|
+
|
|
204
|
+
const types = {};
|
|
205
|
+
for (const type of this._types) {
|
|
206
|
+
types[type] = this[type]();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (const target in internals.aliases) {
|
|
210
|
+
types[target] = this[target]();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return types;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
// Helpers
|
|
219
|
+
|
|
220
|
+
internals.assert = function (value, schema, annotate, args /* [message], [options] */) {
|
|
221
|
+
|
|
222
|
+
const message = args[0] instanceof Error || typeof args[0] === 'string' ? args[0] : null;
|
|
223
|
+
const options = message !== null ? args[1] : args[0];
|
|
224
|
+
const result = schema.validate(value, Common.preferences({ errors: { stack: true } }, options || {}));
|
|
225
|
+
|
|
226
|
+
let error = result.error;
|
|
227
|
+
if (!error) {
|
|
228
|
+
return result.value;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (message instanceof Error) {
|
|
232
|
+
throw message;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const display = annotate && typeof error.annotate === 'function' ? error.annotate() : error.message;
|
|
236
|
+
|
|
237
|
+
if (error instanceof Errors.ValidationError === false) {
|
|
238
|
+
error = clone(error);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
error.message = message ? `${message} ${display}` : display;
|
|
242
|
+
throw error;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
internals.generate = function (root, schema, args) {
|
|
247
|
+
|
|
248
|
+
assert(root, 'Must be invoked on a Joi instance.');
|
|
249
|
+
|
|
250
|
+
schema.$_root = root;
|
|
251
|
+
|
|
252
|
+
if (!schema._definition.args ||
|
|
253
|
+
!args.length) {
|
|
254
|
+
|
|
255
|
+
return schema;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return schema._definition.args(schema, ...args);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
internals.expandExtension = function (extension, joi) {
|
|
263
|
+
|
|
264
|
+
if (typeof extension.type === 'string') {
|
|
265
|
+
return [extension];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const extended = [];
|
|
269
|
+
for (const type of joi._types) {
|
|
270
|
+
if (extension.type.test(type)) {
|
|
271
|
+
const item = Object.assign({}, extension);
|
|
272
|
+
item.type = type;
|
|
273
|
+
item.base = joi[type]();
|
|
274
|
+
extended.push(item);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return extended;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
module.exports = internals.root();
|