joi 14.3.0 → 17.2.1

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 (59) hide show
  1. package/LICENSE.md +10 -0
  2. package/README.md +8 -118
  3. package/dist/joi-browser.min.js +1 -0
  4. package/lib/annotate.js +175 -0
  5. package/lib/base.js +1068 -0
  6. package/lib/cache.js +143 -0
  7. package/lib/common.js +216 -0
  8. package/lib/compile.js +283 -0
  9. package/lib/errors.js +160 -269
  10. package/lib/extend.js +312 -0
  11. package/lib/index.d.ts +2200 -0
  12. package/lib/index.js +179 -364
  13. package/lib/manifest.js +476 -0
  14. package/lib/messages.js +178 -0
  15. package/lib/modify.js +267 -0
  16. package/lib/ref.js +386 -25
  17. package/lib/schemas.js +291 -15
  18. package/lib/state.js +152 -0
  19. package/lib/template.js +427 -0
  20. package/lib/trace.js +346 -0
  21. package/lib/types/alternatives.js +329 -0
  22. package/lib/types/any.js +174 -0
  23. package/lib/types/array.js +775 -0
  24. package/lib/types/binary.js +98 -0
  25. package/lib/types/boolean.js +150 -0
  26. package/lib/types/date.js +233 -0
  27. package/lib/types/function.js +93 -0
  28. package/lib/types/keys.js +1043 -0
  29. package/lib/types/link.js +168 -0
  30. package/lib/types/number.js +335 -0
  31. package/lib/types/object.js +22 -0
  32. package/lib/types/string.js +820 -0
  33. package/lib/types/symbol.js +102 -0
  34. package/lib/validator.js +650 -0
  35. package/lib/values.js +263 -0
  36. package/package.json +34 -29
  37. package/CHANGELOG.md +0 -3
  38. package/LICENSE +0 -25
  39. package/lib/cast.js +0 -64
  40. package/lib/language.js +0 -166
  41. package/lib/set.js +0 -191
  42. package/lib/types/alternatives/index.js +0 -218
  43. package/lib/types/any/index.js +0 -978
  44. package/lib/types/any/settings.js +0 -36
  45. package/lib/types/array/index.js +0 -707
  46. package/lib/types/binary/index.js +0 -100
  47. package/lib/types/boolean/index.js +0 -100
  48. package/lib/types/date/index.js +0 -182
  49. package/lib/types/func/index.js +0 -90
  50. package/lib/types/lazy/index.js +0 -82
  51. package/lib/types/number/index.js +0 -248
  52. package/lib/types/object/index.js +0 -957
  53. package/lib/types/state.js +0 -11
  54. package/lib/types/string/index.js +0 -703
  55. package/lib/types/string/ip.js +0 -54
  56. package/lib/types/string/rfc3986.js +0 -219
  57. package/lib/types/string/uri.js +0 -46
  58. package/lib/types/symbol/index.js +0 -93
  59. package/lib/types/symbols.js +0 -5
@@ -0,0 +1,175 @@
1
+ 'use strict';
2
+
3
+ const Clone = require('@hapi/hoek/lib/clone');
4
+
5
+ const Common = require('./common');
6
+
7
+
8
+ const internals = {
9
+ annotations: Symbol('annotations')
10
+ };
11
+
12
+
13
+ exports.error = function (stripColorCodes) {
14
+
15
+ if (!this._original ||
16
+ typeof this._original !== 'object') {
17
+
18
+ return this.details[0].message;
19
+ }
20
+
21
+ const redFgEscape = stripColorCodes ? '' : '\u001b[31m';
22
+ const redBgEscape = stripColorCodes ? '' : '\u001b[41m';
23
+ const endColor = stripColorCodes ? '' : '\u001b[0m';
24
+
25
+ const obj = Clone(this._original);
26
+
27
+ for (let i = this.details.length - 1; i >= 0; --i) { // Reverse order to process deepest child first
28
+ const pos = i + 1;
29
+ const error = this.details[i];
30
+ const path = error.path;
31
+ let node = obj;
32
+ for (let j = 0; ; ++j) {
33
+ const seg = path[j];
34
+
35
+ if (Common.isSchema(node)) {
36
+ node = node.clone(); // joi schemas are not cloned by hoek, we have to take this extra step
37
+ }
38
+
39
+ if (j + 1 < path.length &&
40
+ typeof node[seg] !== 'string') {
41
+
42
+ node = node[seg];
43
+ }
44
+ else {
45
+ const refAnnotations = node[internals.annotations] || { errors: {}, missing: {} };
46
+ node[internals.annotations] = refAnnotations;
47
+
48
+ const cacheKey = seg || error.context.key;
49
+
50
+ if (node[seg] !== undefined) {
51
+ refAnnotations.errors[cacheKey] = refAnnotations.errors[cacheKey] || [];
52
+ refAnnotations.errors[cacheKey].push(pos);
53
+ }
54
+ else {
55
+ refAnnotations.missing[cacheKey] = pos;
56
+ }
57
+
58
+ break;
59
+ }
60
+ }
61
+ }
62
+
63
+ const replacers = {
64
+ key: /_\$key\$_([, \d]+)_\$end\$_"/g,
65
+ missing: /"_\$miss\$_([^|]+)\|(\d+)_\$end\$_": "__missing__"/g,
66
+ arrayIndex: /\s*"_\$idx\$_([, \d]+)_\$end\$_",?\n(.*)/g,
67
+ specials: /"\[(NaN|Symbol.*|-?Infinity|function.*|\(.*)]"/g
68
+ };
69
+
70
+ let message = internals.safeStringify(obj, 2)
71
+ .replace(replacers.key, ($0, $1) => `" ${redFgEscape}[${$1}]${endColor}`)
72
+ .replace(replacers.missing, ($0, $1, $2) => `${redBgEscape}"${$1}"${endColor}${redFgEscape} [${$2}]: -- missing --${endColor}`)
73
+ .replace(replacers.arrayIndex, ($0, $1, $2) => `\n${$2} ${redFgEscape}[${$1}]${endColor}`)
74
+ .replace(replacers.specials, ($0, $1) => $1);
75
+
76
+ message = `${message}\n${redFgEscape}`;
77
+
78
+ for (let i = 0; i < this.details.length; ++i) {
79
+ const pos = i + 1;
80
+ message = `${message}\n[${pos}] ${this.details[i].message}`;
81
+ }
82
+
83
+ message = message + endColor;
84
+
85
+ return message;
86
+ };
87
+
88
+
89
+ // Inspired by json-stringify-safe
90
+
91
+ internals.safeStringify = function (obj, spaces) {
92
+
93
+ return JSON.stringify(obj, internals.serializer(), spaces);
94
+ };
95
+
96
+
97
+ internals.serializer = function () {
98
+
99
+ const keys = [];
100
+ const stack = [];
101
+
102
+ const cycleReplacer = (key, value) => {
103
+
104
+ if (stack[0] === value) {
105
+ return '[Circular ~]';
106
+ }
107
+
108
+ return '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
109
+ };
110
+
111
+ return function (key, value) {
112
+
113
+ if (stack.length > 0) {
114
+ const thisPos = stack.indexOf(this);
115
+ if (~thisPos) {
116
+ stack.length = thisPos + 1;
117
+ keys.length = thisPos + 1;
118
+ keys[thisPos] = key;
119
+ }
120
+ else {
121
+ stack.push(this);
122
+ keys.push(key);
123
+ }
124
+
125
+ if (~stack.indexOf(value)) {
126
+ value = cycleReplacer.call(this, key, value);
127
+ }
128
+ }
129
+ else {
130
+ stack.push(value);
131
+ }
132
+
133
+ if (value) {
134
+ const annotations = value[internals.annotations];
135
+ if (annotations) {
136
+ if (Array.isArray(value)) {
137
+ const annotated = [];
138
+
139
+ for (let i = 0; i < value.length; ++i) {
140
+ if (annotations.errors[i]) {
141
+ annotated.push(`_$idx$_${annotations.errors[i].sort().join(', ')}_$end$_`);
142
+ }
143
+
144
+ annotated.push(value[i]);
145
+ }
146
+
147
+ value = annotated;
148
+ }
149
+ else {
150
+ for (const errorKey in annotations.errors) {
151
+ value[`${errorKey}_$key$_${annotations.errors[errorKey].sort().join(', ')}_$end$_`] = value[errorKey];
152
+ value[errorKey] = undefined;
153
+ }
154
+
155
+ for (const missingKey in annotations.missing) {
156
+ value[`_$miss$_${missingKey}|${annotations.missing[missingKey]}_$end$_`] = '__missing__';
157
+ }
158
+ }
159
+
160
+ return value;
161
+ }
162
+ }
163
+
164
+ if (value === Infinity ||
165
+ value === -Infinity ||
166
+ Number.isNaN(value) ||
167
+ typeof value === 'function' ||
168
+ typeof value === 'symbol') {
169
+
170
+ return '[' + value.toString() + ']';
171
+ }
172
+
173
+ return value;
174
+ };
175
+ };