joi 9.1.1 → 9.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/README.md +1 -1
- package/lib/errors.js +18 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ When validating a schema:
|
|
|
102
102
|
* Rules are defined in an additive fashion and evaluated in order after whitelist and blacklist checks.
|
|
103
103
|
|
|
104
104
|
# API
|
|
105
|
-
See the [API Reference](https://github.com/hapijs/joi/blob/v9.
|
|
105
|
+
See the [API Reference](https://github.com/hapijs/joi/blob/v9.2.0/API.md).
|
|
106
106
|
|
|
107
107
|
# Browsers
|
|
108
108
|
|
package/lib/errors.js
CHANGED
|
@@ -232,7 +232,11 @@ internals.serializer = function () {
|
|
|
232
232
|
};
|
|
233
233
|
|
|
234
234
|
|
|
235
|
-
internals.annotate = function () {
|
|
235
|
+
internals.annotate = function (stripColorCodes) {
|
|
236
|
+
|
|
237
|
+
const redFgEscape = stripColorCodes ? '' : '\u001b[31m';
|
|
238
|
+
const redBgEscape = stripColorCodes ? '' : '\u001b[41m';
|
|
239
|
+
const endColor = stripColorCodes ? '' : '\u001b[0m';
|
|
236
240
|
|
|
237
241
|
if (typeof this._object !== 'object') {
|
|
238
242
|
return this.details[0].message;
|
|
@@ -242,7 +246,7 @@ internals.annotate = function () {
|
|
|
242
246
|
|
|
243
247
|
const lookup = {};
|
|
244
248
|
for (let i = this.details.length - 1; i >= 0; --i) { // Reverse order to process deepest child first
|
|
245
|
-
const pos =
|
|
249
|
+
const pos = i + 1;
|
|
246
250
|
const error = this.details[i];
|
|
247
251
|
const path = error.path.split('.');
|
|
248
252
|
let ref = obj;
|
|
@@ -254,13 +258,13 @@ internals.annotate = function () {
|
|
|
254
258
|
else {
|
|
255
259
|
const value = ref[seg];
|
|
256
260
|
if (Array.isArray(ref)) {
|
|
257
|
-
const arrayLabel =
|
|
261
|
+
const arrayLabel = `_$idx$_${pos}_$end$_`;
|
|
258
262
|
if (!ref.placeholders) {
|
|
259
263
|
ref.placeholders = {};
|
|
260
264
|
}
|
|
261
265
|
|
|
262
266
|
if (ref.placeholders[seg]) {
|
|
263
|
-
ref.placeholders[seg] = ref.placeholders[seg].replace('_$end$_',
|
|
267
|
+
ref.placeholders[seg] = ref.placeholders[seg].replace('_$end$_', `, ${pos}_$end$_`);
|
|
264
268
|
}
|
|
265
269
|
else {
|
|
266
270
|
ref.placeholders[seg] = arrayLabel;
|
|
@@ -269,19 +273,19 @@ internals.annotate = function () {
|
|
|
269
273
|
else {
|
|
270
274
|
if (value !== undefined) {
|
|
271
275
|
delete ref[seg];
|
|
272
|
-
const objectLabel = seg
|
|
276
|
+
const objectLabel = `${seg}_$key$_${pos}_$end$_`;
|
|
273
277
|
ref[objectLabel] = value;
|
|
274
278
|
lookup[error.path] = objectLabel;
|
|
275
279
|
}
|
|
276
280
|
else if (lookup[error.path]) {
|
|
277
281
|
const replacement = lookup[error.path];
|
|
278
|
-
const appended = replacement.replace('_$end$_',
|
|
282
|
+
const appended = replacement.replace('_$end$_', `, ${pos}_$end$_`);
|
|
279
283
|
ref[appended] = ref[replacement];
|
|
280
284
|
lookup[error.path] = appended;
|
|
281
285
|
delete ref[replacement];
|
|
282
286
|
}
|
|
283
287
|
else {
|
|
284
|
-
ref[
|
|
288
|
+
ref[`_$miss$_${seg}|${pos}_$end$_`] = '__missing__';
|
|
285
289
|
}
|
|
286
290
|
}
|
|
287
291
|
}
|
|
@@ -296,18 +300,19 @@ internals.annotate = function () {
|
|
|
296
300
|
};
|
|
297
301
|
|
|
298
302
|
let message = internals.safeStringify(obj, 2)
|
|
299
|
-
.replace(replacers.key, ($0, $1) =>
|
|
300
|
-
.replace(replacers.missing, ($0, $1, $2) =>
|
|
301
|
-
.replace(replacers.arrayIndex, ($0, $1, $2) =>
|
|
303
|
+
.replace(replacers.key, ($0, $1) => `" ${redFgEscape}[${$1}]${endColor}`)
|
|
304
|
+
.replace(replacers.missing, ($0, $1, $2) => `${redBgEscape}"${$1}"${endColor}${redFgEscape} [${$2}]: -- missing --${endColor}`)
|
|
305
|
+
.replace(replacers.arrayIndex, ($0, $1, $2) => `\n${$2} ${redFgEscape}[${$1}]${endColor}`)
|
|
302
306
|
.replace(replacers.specials, ($0, $1) => $1);
|
|
303
307
|
|
|
304
|
-
message = message
|
|
308
|
+
message = `${message}\n${redFgEscape}`;
|
|
305
309
|
|
|
306
310
|
for (let i = 0; i < this.details.length; ++i) {
|
|
307
|
-
|
|
311
|
+
const pos = i + 1;
|
|
312
|
+
message = `${message}\n[${pos}] ${this.details[i].message}`;
|
|
308
313
|
}
|
|
309
314
|
|
|
310
|
-
message = message +
|
|
315
|
+
message = message + endColor;
|
|
311
316
|
|
|
312
317
|
return message;
|
|
313
318
|
};
|