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.
Files changed (59) hide show
  1. package/LICENSE.md +10 -0
  2. package/README.md +9 -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 -347
  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
package/lib/set.js DELETED
@@ -1,191 +0,0 @@
1
- 'use strict';
2
-
3
- const Ref = require('./ref');
4
-
5
-
6
- const internals = {};
7
-
8
-
9
- internals.extendedCheckForValue = function (value, insensitive) {
10
-
11
- const valueType = typeof value;
12
-
13
- if (valueType === 'object') {
14
- if (value instanceof Date) {
15
- return (item) => {
16
-
17
- return item instanceof Date && value.getTime() === item.getTime();
18
- };
19
- }
20
-
21
- if (Buffer.isBuffer(value)) {
22
- return (item) => {
23
-
24
- return Buffer.isBuffer(item) && value.length === item.length && value.toString('binary') === item.toString('binary');
25
- };
26
- }
27
- }
28
- else if (insensitive && valueType === 'string') {
29
- const lowercaseValue = value.toLowerCase();
30
- return (item) => {
31
-
32
- return typeof item === 'string' && lowercaseValue === item.toLowerCase();
33
- };
34
- }
35
-
36
- return null;
37
- };
38
-
39
-
40
- module.exports = class InternalSet {
41
-
42
- constructor(from) {
43
-
44
- this._set = new Set(from);
45
- this._hasRef = false;
46
- }
47
-
48
- add(value, refs) {
49
-
50
- const isRef = Ref.isRef(value);
51
- if (!isRef && this.has(value, null, null, false)) {
52
-
53
- return this;
54
- }
55
-
56
- if (refs !== undefined) { // If it's a merge, we don't have any refs
57
- Ref.push(refs, value);
58
- }
59
-
60
- this._set.add(value);
61
-
62
- this._hasRef |= isRef;
63
-
64
- return this;
65
- }
66
-
67
- merge(add, remove) {
68
-
69
- for (const item of add._set) {
70
- this.add(item);
71
- }
72
-
73
- for (const item of remove._set) {
74
- this.remove(item);
75
- }
76
-
77
- return this;
78
- }
79
-
80
- remove(value) {
81
-
82
- this._set.delete(value);
83
- return this;
84
- }
85
-
86
- has(value, state, options, insensitive) {
87
-
88
- return !!this.get(value, state, options, insensitive);
89
- }
90
-
91
- get(value, state, options, insensitive) {
92
-
93
- if (!this._set.size) {
94
- return false;
95
- }
96
-
97
- const hasValue = this._set.has(value);
98
- if (hasValue) {
99
- return { value };
100
- }
101
-
102
- const extendedCheck = internals.extendedCheckForValue(value, insensitive);
103
- if (!extendedCheck) {
104
- if (state && this._hasRef) {
105
- for (let item of this._set) {
106
- if (Ref.isRef(item)) {
107
- item = [].concat(item(state.reference || state.parent, options));
108
- const found = item.indexOf(value);
109
- if (found >= 0) {
110
- return { value: item[found] };
111
- }
112
- }
113
- }
114
- }
115
-
116
- return false;
117
- }
118
-
119
- return this._has(value, state, options, extendedCheck);
120
- }
121
-
122
- _has(value, state, options, check) {
123
-
124
- const checkRef = !!(state && this._hasRef);
125
-
126
- const isReallyEqual = function (item) {
127
-
128
- if (value === item) {
129
- return true;
130
- }
131
-
132
- return check(item);
133
- };
134
-
135
- for (let item of this._set) {
136
- if (checkRef && Ref.isRef(item)) { // Only resolve references if there is a state, otherwise it's a merge
137
- item = item(state.reference || state.parent, options);
138
-
139
- if (Array.isArray(item)) {
140
- const found = item.findIndex(isReallyEqual);
141
- if (found >= 0) {
142
- return {
143
- value: item[found]
144
- };
145
- }
146
-
147
- continue;
148
- }
149
- }
150
-
151
- if (isReallyEqual(item)) {
152
- return {
153
- value: item
154
- };
155
- }
156
- }
157
-
158
- return false;
159
- }
160
-
161
- values(options) {
162
-
163
- if (options && options.stripUndefined) {
164
- const values = [];
165
-
166
- for (const item of this._set) {
167
- if (item !== undefined) {
168
- values.push(item);
169
- }
170
- }
171
-
172
- return values;
173
- }
174
-
175
- return Array.from(this._set);
176
- }
177
-
178
- slice() {
179
-
180
- const set = new InternalSet(this._set);
181
- set._hasRef = this._hasRef;
182
- return set;
183
- }
184
-
185
- concat(source) {
186
-
187
- const set = new InternalSet([...this._set, ...source._set]);
188
- set._hasRef = !!(this._hasRef | source._hasRef);
189
- return set;
190
- }
191
- };
@@ -1,218 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Hoek = require('hoek');
6
- const Any = require('../any');
7
- const Cast = require('../../cast');
8
- const Ref = require('../../ref');
9
-
10
-
11
- // Declare internals
12
-
13
- const internals = {};
14
-
15
-
16
- internals.Alternatives = class extends Any {
17
-
18
- constructor() {
19
-
20
- super();
21
- this._type = 'alternatives';
22
- this._invalids.remove(null);
23
- this._inner.matches = [];
24
- }
25
-
26
- _init(...args) {
27
-
28
- return args.length ? this.try(...args) : this;
29
- }
30
-
31
- _base(value, state, options) {
32
-
33
- const errors = [];
34
- const il = this._inner.matches.length;
35
- const baseType = this._baseType;
36
-
37
- for (let i = 0; i < il; ++i) {
38
- const item = this._inner.matches[i];
39
- if (!item.schema) {
40
- const schema = item.peek || item.is;
41
- const input = item.is ? item.ref(state.reference || state.parent, options) : value;
42
- const failed = schema._validate(input, null, options, state.parent).errors;
43
-
44
- if (failed) {
45
- if (item.otherwise) {
46
- return item.otherwise._validate(value, state, options);
47
- }
48
- }
49
- else if (item.then) {
50
- return item.then._validate(value, state, options);
51
- }
52
-
53
- if (i === (il - 1) && baseType) {
54
- return baseType._validate(value, state, options);
55
- }
56
-
57
- continue;
58
- }
59
-
60
- const result = item.schema._validate(value, state, options);
61
- if (!result.errors) { // Found a valid match
62
- return result;
63
- }
64
-
65
- errors.push(...result.errors);
66
- }
67
-
68
- if (errors.length) {
69
- return { errors: this.createError('alternatives.child', { reason: errors }, state, options) };
70
- }
71
-
72
- return { errors: this.createError('alternatives.base', null, state, options) };
73
- }
74
-
75
- try(...schemas) {
76
-
77
- schemas = Hoek.flatten(schemas);
78
- Hoek.assert(schemas.length, 'Cannot add other alternatives without at least one schema');
79
-
80
- const obj = this.clone();
81
-
82
- for (let i = 0; i < schemas.length; ++i) {
83
- const cast = Cast.schema(this._currentJoi, schemas[i]);
84
- if (cast._refs.length) {
85
- obj._refs.push(...cast._refs);
86
- }
87
-
88
- obj._inner.matches.push({ schema: cast });
89
- }
90
-
91
- return obj;
92
- }
93
-
94
- when(condition, options) {
95
-
96
- let schemaCondition = false;
97
- Hoek.assert(Ref.isRef(condition) || typeof condition === 'string' || (schemaCondition = condition instanceof Any), 'Invalid condition:', condition);
98
- Hoek.assert(options, 'Missing options');
99
- Hoek.assert(typeof options === 'object', 'Invalid options');
100
- if (schemaCondition) {
101
- Hoek.assert(!options.hasOwnProperty('is'), '"is" can not be used with a schema condition');
102
- }
103
- else {
104
- Hoek.assert(options.hasOwnProperty('is'), 'Missing "is" directive');
105
- }
106
-
107
- Hoek.assert(options.then !== undefined || options.otherwise !== undefined, 'options must have at least one of "then" or "otherwise"');
108
-
109
- const obj = this.clone();
110
- let is;
111
- if (!schemaCondition) {
112
- is = Cast.schema(this._currentJoi, options.is);
113
-
114
- if (options.is === null || !(Ref.isRef(options.is) || options.is instanceof Any)) {
115
-
116
- // Only apply required if this wasn't already a schema or a ref, we'll suppose people know what they're doing
117
- is = is.required();
118
- }
119
- }
120
-
121
- const item = {
122
- ref: schemaCondition ? null : Cast.ref(condition),
123
- peek: schemaCondition ? condition : null,
124
- is,
125
- then: options.then !== undefined ? Cast.schema(this._currentJoi, options.then) : undefined,
126
- otherwise: options.otherwise !== undefined ? Cast.schema(this._currentJoi, options.otherwise) : undefined
127
- };
128
-
129
- if (obj._baseType) {
130
-
131
- item.then = item.then && obj._baseType.concat(item.then);
132
- item.otherwise = item.otherwise && obj._baseType.concat(item.otherwise);
133
- }
134
-
135
- if (!schemaCondition) {
136
- Ref.push(obj._refs, item.ref);
137
- obj._refs.push(...item.is._refs);
138
- }
139
-
140
- if (item.then && item.then._refs.length) {
141
- obj._refs.push(...item.then._refs);
142
- }
143
-
144
- if (item.otherwise && item.otherwise._refs.length) {
145
- obj._refs.push(...item.otherwise._refs);
146
- }
147
-
148
- obj._inner.matches.push(item);
149
-
150
- return obj;
151
- }
152
-
153
- label(name) {
154
-
155
- const obj = super.label(name);
156
- obj._inner.matches = obj._inner.matches.map((match) => {
157
-
158
- if (match.schema) {
159
- return { schema: match.schema.label(name) };
160
- }
161
-
162
- match = Object.assign({}, match);
163
- if (match.then) {
164
- match.then = match.then.label(name);
165
- }
166
-
167
- if (match.otherwise) {
168
- match.otherwise = match.otherwise.label(name);
169
- }
170
-
171
- return match;
172
- });
173
- return obj;
174
- }
175
-
176
- describe() {
177
-
178
- const description = super.describe();
179
- const alternatives = [];
180
- for (let i = 0; i < this._inner.matches.length; ++i) {
181
- const item = this._inner.matches[i];
182
- if (item.schema) {
183
-
184
- // try()
185
-
186
- alternatives.push(item.schema.describe());
187
- }
188
- else {
189
-
190
- // when()
191
-
192
- const when = item.is ? {
193
- ref: item.ref.toString(),
194
- is: item.is.describe()
195
- } : {
196
- peek: item.peek.describe()
197
- };
198
-
199
- if (item.then) {
200
- when.then = item.then.describe();
201
- }
202
-
203
- if (item.otherwise) {
204
- when.otherwise = item.otherwise.describe();
205
- }
206
-
207
- alternatives.push(when);
208
- }
209
- }
210
-
211
- description.alternatives = alternatives;
212
- return description;
213
- }
214
-
215
- };
216
-
217
-
218
- module.exports = new internals.Alternatives();