joi 14.2.0 → 17.2.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 +10 -0
- package/README.md +9 -118
- package/dist/joi-browser.min.js +1 -0
- package/lib/annotate.js +175 -0
- package/lib/base.js +1068 -0
- package/lib/cache.js +143 -0
- package/lib/common.js +216 -0
- package/lib/compile.js +283 -0
- package/lib/errors.js +160 -269
- package/lib/extend.js +312 -0
- package/lib/index.d.ts +2200 -0
- package/lib/index.js +179 -347
- package/lib/manifest.js +476 -0
- package/lib/messages.js +178 -0
- package/lib/modify.js +267 -0
- package/lib/ref.js +386 -25
- package/lib/schemas.js +291 -15
- package/lib/state.js +152 -0
- package/lib/template.js +427 -0
- package/lib/trace.js +346 -0
- package/lib/types/alternatives.js +329 -0
- package/lib/types/any.js +174 -0
- package/lib/types/array.js +775 -0
- package/lib/types/binary.js +98 -0
- package/lib/types/boolean.js +150 -0
- package/lib/types/date.js +233 -0
- package/lib/types/function.js +93 -0
- package/lib/types/keys.js +1043 -0
- package/lib/types/link.js +168 -0
- package/lib/types/number.js +335 -0
- package/lib/types/object.js +22 -0
- package/lib/types/string.js +820 -0
- package/lib/types/symbol.js +102 -0
- package/lib/validator.js +650 -0
- package/lib/values.js +263 -0
- package/package.json +34 -29
- package/CHANGELOG.md +0 -3
- package/LICENSE +0 -25
- package/lib/cast.js +0 -64
- package/lib/language.js +0 -166
- package/lib/set.js +0 -191
- package/lib/types/alternatives/index.js +0 -218
- package/lib/types/any/index.js +0 -978
- package/lib/types/any/settings.js +0 -36
- package/lib/types/array/index.js +0 -707
- package/lib/types/binary/index.js +0 -100
- package/lib/types/boolean/index.js +0 -100
- package/lib/types/date/index.js +0 -182
- package/lib/types/func/index.js +0 -90
- package/lib/types/lazy/index.js +0 -82
- package/lib/types/number/index.js +0 -248
- package/lib/types/object/index.js +0 -957
- package/lib/types/state.js +0 -11
- package/lib/types/string/index.js +0 -703
- package/lib/types/string/ip.js +0 -54
- package/lib/types/string/rfc3986.js +0 -219
- package/lib/types/string/uri.js +0 -46
- package/lib/types/symbol/index.js +0 -93
- package/lib/types/symbols.js +0 -5
package/lib/template.js
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Assert = require('@hapi/hoek/lib/assert');
|
|
4
|
+
const Clone = require('@hapi/hoek/lib/clone');
|
|
5
|
+
const EscapeHtml = require('@hapi/hoek/lib/escapeHtml');
|
|
6
|
+
const Formula = require('@hapi/formula');
|
|
7
|
+
|
|
8
|
+
const Common = require('./common');
|
|
9
|
+
const Errors = require('./errors');
|
|
10
|
+
const Ref = require('./ref');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const internals = {
|
|
14
|
+
symbol: Symbol('template'),
|
|
15
|
+
|
|
16
|
+
opens: new Array(1000).join('\u0000'),
|
|
17
|
+
closes: new Array(1000).join('\u0001'),
|
|
18
|
+
|
|
19
|
+
dateFormat: {
|
|
20
|
+
date: Date.prototype.toDateString,
|
|
21
|
+
iso: Date.prototype.toISOString,
|
|
22
|
+
string: Date.prototype.toString,
|
|
23
|
+
time: Date.prototype.toTimeString,
|
|
24
|
+
utc: Date.prototype.toUTCString
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
module.exports = exports = internals.Template = class {
|
|
30
|
+
|
|
31
|
+
constructor(source, options) {
|
|
32
|
+
|
|
33
|
+
Assert(typeof source === 'string', 'Template source must be a string');
|
|
34
|
+
Assert(!source.includes('\u0000') && !source.includes('\u0001'), 'Template source cannot contain reserved control characters');
|
|
35
|
+
|
|
36
|
+
this.source = source;
|
|
37
|
+
this.rendered = source;
|
|
38
|
+
|
|
39
|
+
this._template = null;
|
|
40
|
+
this._settings = Clone(options);
|
|
41
|
+
|
|
42
|
+
this._parse();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_parse() {
|
|
46
|
+
|
|
47
|
+
// 'text {raw} {{ref}} \\{{ignore}} {{ignore\\}} {{ignore {{ignore}'
|
|
48
|
+
|
|
49
|
+
if (!this.source.includes('{')) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Encode escaped \\{{{{{
|
|
54
|
+
|
|
55
|
+
const encoded = internals.encode(this.source);
|
|
56
|
+
|
|
57
|
+
// Split on first { in each set
|
|
58
|
+
|
|
59
|
+
const parts = internals.split(encoded);
|
|
60
|
+
|
|
61
|
+
// Process parts
|
|
62
|
+
|
|
63
|
+
let refs = false;
|
|
64
|
+
const processed = [];
|
|
65
|
+
const head = parts.shift();
|
|
66
|
+
if (head) {
|
|
67
|
+
processed.push(head);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
const raw = part[0] !== '{';
|
|
72
|
+
const ender = raw ? '}' : '}}';
|
|
73
|
+
const end = part.indexOf(ender);
|
|
74
|
+
if (end === -1 || // Ignore non-matching closing
|
|
75
|
+
part[1] === '{') { // Ignore more than two {
|
|
76
|
+
|
|
77
|
+
processed.push(`{${internals.decode(part)}`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let variable = part.slice(raw ? 0 : 1, end);
|
|
82
|
+
const wrapped = variable[0] === ':';
|
|
83
|
+
if (wrapped) {
|
|
84
|
+
variable = variable.slice(1);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const dynamic = this._ref(internals.decode(variable), { raw, wrapped });
|
|
88
|
+
processed.push(dynamic);
|
|
89
|
+
if (typeof dynamic !== 'string') {
|
|
90
|
+
refs = true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const rest = part.slice(end + ender.length);
|
|
94
|
+
if (rest) {
|
|
95
|
+
processed.push(internals.decode(rest));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (!refs) {
|
|
100
|
+
this.rendered = processed.join('');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this._template = processed;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static date(date, prefs) {
|
|
108
|
+
|
|
109
|
+
return internals.dateFormat[prefs.dateFormat].call(date);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
describe(options = {}) {
|
|
113
|
+
|
|
114
|
+
if (!this._settings &&
|
|
115
|
+
options.compact) {
|
|
116
|
+
|
|
117
|
+
return this.source;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const desc = { template: this.source };
|
|
121
|
+
if (this._settings) {
|
|
122
|
+
desc.options = this._settings;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return desc;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static build(desc) {
|
|
129
|
+
|
|
130
|
+
return new internals.Template(desc.template, desc.options);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
isDynamic() {
|
|
134
|
+
|
|
135
|
+
return !!this._template;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static isTemplate(template) {
|
|
139
|
+
|
|
140
|
+
return template ? !!template[Common.symbols.template] : false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
refs() {
|
|
144
|
+
|
|
145
|
+
if (!this._template) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const refs = [];
|
|
150
|
+
for (const part of this._template) {
|
|
151
|
+
if (typeof part !== 'string') {
|
|
152
|
+
refs.push(...part.refs);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return refs;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
resolve(value, state, prefs, local) {
|
|
160
|
+
|
|
161
|
+
if (this._template &&
|
|
162
|
+
this._template.length === 1) {
|
|
163
|
+
|
|
164
|
+
return this._part(this._template[0], /* context -> [*/ value, state, prefs, local, {} /*] */);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return this.render(value, state, prefs, local);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
_part(part, ...args) {
|
|
171
|
+
|
|
172
|
+
if (part.ref) {
|
|
173
|
+
return part.ref.resolve(...args);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return part.formula.evaluate(args);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
render(value, state, prefs, local, options = {}) {
|
|
180
|
+
|
|
181
|
+
if (!this.isDynamic()) {
|
|
182
|
+
return this.rendered;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const parts = [];
|
|
186
|
+
for (const part of this._template) {
|
|
187
|
+
if (typeof part === 'string') {
|
|
188
|
+
parts.push(part);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const rendered = this._part(part, /* context -> [*/ value, state, prefs, local, options /*] */);
|
|
192
|
+
const string = internals.stringify(rendered, value, state, prefs, local, options);
|
|
193
|
+
if (string !== undefined) {
|
|
194
|
+
const result = part.raw || (options.errors && options.errors.escapeHtml) === false ? string : EscapeHtml(string);
|
|
195
|
+
parts.push(internals.wrap(result, part.wrapped && prefs.errors.wrap.label));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return parts.join('');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
_ref(content, { raw, wrapped }) {
|
|
204
|
+
|
|
205
|
+
const refs = [];
|
|
206
|
+
const reference = (variable) => {
|
|
207
|
+
|
|
208
|
+
const ref = Ref.create(variable, this._settings);
|
|
209
|
+
refs.push(ref);
|
|
210
|
+
return (context) => ref.resolve(...context);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
var formula = new Formula.Parser(content, { reference, functions: internals.functions, constants: internals.constants });
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
err.message = `Invalid template variable "${content}" fails due to: ${err.message}`;
|
|
218
|
+
throw err;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (formula.single) {
|
|
222
|
+
if (formula.single.type === 'reference') {
|
|
223
|
+
const ref = refs[0];
|
|
224
|
+
return { ref, raw, refs, wrapped: wrapped || ref.type === 'local' && ref.key === 'label' };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return internals.stringify(formula.single.value);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return { formula, raw, refs };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
toString() {
|
|
234
|
+
|
|
235
|
+
return this.source;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
internals.Template.prototype[Common.symbols.template] = true;
|
|
241
|
+
internals.Template.prototype.isImmutable = true; // Prevents Hoek from deep cloning schema objects
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
internals.encode = function (string) {
|
|
245
|
+
|
|
246
|
+
return string
|
|
247
|
+
.replace(/\\(\{+)/g, ($0, $1) => {
|
|
248
|
+
|
|
249
|
+
return internals.opens.slice(0, $1.length);
|
|
250
|
+
})
|
|
251
|
+
.replace(/\\(\}+)/g, ($0, $1) => {
|
|
252
|
+
|
|
253
|
+
return internals.closes.slice(0, $1.length);
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
internals.decode = function (string) {
|
|
259
|
+
|
|
260
|
+
return string
|
|
261
|
+
.replace(/\u0000/g, '{')
|
|
262
|
+
.replace(/\u0001/g, '}');
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
internals.split = function (string) {
|
|
267
|
+
|
|
268
|
+
const parts = [];
|
|
269
|
+
let current = '';
|
|
270
|
+
|
|
271
|
+
for (let i = 0; i < string.length; ++i) {
|
|
272
|
+
const char = string[i];
|
|
273
|
+
|
|
274
|
+
if (char === '{') {
|
|
275
|
+
let next = '';
|
|
276
|
+
while (i + 1 < string.length &&
|
|
277
|
+
string[i + 1] === '{') {
|
|
278
|
+
|
|
279
|
+
next += '{';
|
|
280
|
+
++i;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
parts.push(current);
|
|
284
|
+
current = next;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
current += char;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
parts.push(current);
|
|
292
|
+
return parts;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
internals.wrap = function (value, ends) {
|
|
297
|
+
|
|
298
|
+
if (!ends) {
|
|
299
|
+
return value;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (ends.length === 1) {
|
|
303
|
+
return `${ends}${value}${ends}`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return `${ends[0]}${value}${ends[1]}`;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
internals.stringify = function (value, original, state, prefs, local, options) {
|
|
311
|
+
|
|
312
|
+
const type = typeof value;
|
|
313
|
+
|
|
314
|
+
let skipWrap = false;
|
|
315
|
+
if (Ref.isRef(value) &&
|
|
316
|
+
value.render) {
|
|
317
|
+
|
|
318
|
+
skipWrap = value.in;
|
|
319
|
+
value = value.resolve(original, state, prefs, local, { in: value.in, ...options });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (value === null) {
|
|
323
|
+
return 'null';
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (type === 'string') {
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (type === 'number' ||
|
|
331
|
+
type === 'function' ||
|
|
332
|
+
type === 'symbol') {
|
|
333
|
+
|
|
334
|
+
return value.toString();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (type !== 'object') {
|
|
338
|
+
return JSON.stringify(value);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (value instanceof Date) {
|
|
342
|
+
return internals.Template.date(value, prefs);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (value instanceof Map) {
|
|
346
|
+
const pairs = [];
|
|
347
|
+
for (const [key, sym] of value.entries()) {
|
|
348
|
+
pairs.push(`${key.toString()} -> ${sym.toString()}`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
value = pairs;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (!Array.isArray(value)) {
|
|
355
|
+
return value.toString();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
let partial = '';
|
|
359
|
+
for (const item of value) {
|
|
360
|
+
partial = partial + (partial.length ? ', ' : '') + internals.stringify(item, original, state, prefs, local, options);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (skipWrap) {
|
|
364
|
+
return partial;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return internals.wrap(partial, prefs.errors.wrap.array);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
internals.constants = {
|
|
372
|
+
|
|
373
|
+
true: true,
|
|
374
|
+
false: false,
|
|
375
|
+
null: null,
|
|
376
|
+
|
|
377
|
+
second: 1000,
|
|
378
|
+
minute: 60 * 1000,
|
|
379
|
+
hour: 60 * 60 * 1000,
|
|
380
|
+
day: 24 * 60 * 60 * 1000
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
internals.functions = {
|
|
385
|
+
|
|
386
|
+
if(condition, then, otherwise) {
|
|
387
|
+
|
|
388
|
+
return condition ? then : otherwise;
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
msg(code) {
|
|
392
|
+
|
|
393
|
+
const [value, state, prefs, local, options] = this;
|
|
394
|
+
const messages = options.messages;
|
|
395
|
+
if (!messages) {
|
|
396
|
+
return '';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const template = Errors.template(value, messages[0], code, state, prefs) || Errors.template(value, messages[1], code, state, prefs);
|
|
400
|
+
if (!template) {
|
|
401
|
+
return '';
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return template.render(value, state, prefs, local, options);
|
|
405
|
+
},
|
|
406
|
+
|
|
407
|
+
number(value) {
|
|
408
|
+
|
|
409
|
+
if (typeof value === 'number') {
|
|
410
|
+
return value;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (typeof value === 'string') {
|
|
414
|
+
return parseFloat(value);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (typeof value === 'boolean') {
|
|
418
|
+
return value ? 1 : 0;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (value instanceof Date) {
|
|
422
|
+
return value.getTime();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
};
|