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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/errors.js +18 -13
  3. 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.1.1/API.md).
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 = this.details.length - i;
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 = '_$idx$_' + (i + 1) + '_$end$_';
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$_', ', ' + (i + 1) + '_$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 + '_$key$_' + pos + '_$end$_';
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$_', ', ' + pos + '_$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['_$miss$_' + seg + '|' + pos + '_$end$_'] = '__missing__';
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) => '" \u001b[31m[' + $1 + ']\u001b[0m')
300
- .replace(replacers.missing, ($0, $1, $2) => '\u001b[41m"' + $1 + '"\u001b[0m\u001b[31m [' + $2 + ']: -- missing --\u001b[0m')
301
- .replace(replacers.arrayIndex, ($0, $1, $2) => '\n' + $2 + ' \u001b[31m[' + $1 + ']\u001b[0m')
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 + '\n\u001b[31m';
308
+ message = `${message}\n${redFgEscape}`;
305
309
 
306
310
  for (let i = 0; i < this.details.length; ++i) {
307
- message = message + '\n[' + (i + 1) + '] ' + this.details[i].message;
311
+ const pos = i + 1;
312
+ message = `${message}\n[${pos}] ${this.details[i].message}`;
308
313
  }
309
314
 
310
- message = message + '\u001b[0m';
315
+ message = message + endColor;
311
316
 
312
317
  return message;
313
318
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "joi",
3
3
  "description": "Object schema validation",
4
- "version": "9.1.1",
4
+ "version": "9.2.0",
5
5
  "homepage": "https://github.com/hapijs/joi",
6
6
  "repository": "git://github.com/hapijs/joi",
7
7
  "main": "lib/index.js",