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
@@ -1,100 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Any = require('../any');
6
- const Hoek = require('hoek');
7
-
8
-
9
- // Declare internals
10
-
11
- const internals = {};
12
-
13
-
14
- internals.Binary = class extends Any {
15
-
16
- constructor() {
17
-
18
- super();
19
- this._type = 'binary';
20
- }
21
-
22
- _base(value, state, options) {
23
-
24
- const result = {
25
- value
26
- };
27
-
28
- if (typeof value === 'string' &&
29
- options.convert) {
30
-
31
- try {
32
- result.value = Buffer.from(value, this._flags.encoding);
33
- }
34
- catch (e) {
35
- }
36
- }
37
-
38
- result.errors = Buffer.isBuffer(result.value) ? null : this.createError('binary.base', null, state, options);
39
- return result;
40
- }
41
-
42
- encoding(encoding) {
43
-
44
- Hoek.assert(Buffer.isEncoding(encoding), 'Invalid encoding:', encoding);
45
-
46
- if (this._flags.encoding === encoding) {
47
- return this;
48
- }
49
-
50
- const obj = this.clone();
51
- obj._flags.encoding = encoding;
52
- return obj;
53
- }
54
-
55
- min(limit) {
56
-
57
- Hoek.assert(Number.isSafeInteger(limit) && limit >= 0, 'limit must be a positive integer');
58
-
59
- return this._test('min', limit, function (value, state, options) {
60
-
61
- if (value.length >= limit) {
62
- return value;
63
- }
64
-
65
- return this.createError('binary.min', { limit, value }, state, options);
66
- });
67
- }
68
-
69
- max(limit) {
70
-
71
- Hoek.assert(Number.isSafeInteger(limit) && limit >= 0, 'limit must be a positive integer');
72
-
73
- return this._test('max', limit, function (value, state, options) {
74
-
75
- if (value.length <= limit) {
76
- return value;
77
- }
78
-
79
- return this.createError('binary.max', { limit, value }, state, options);
80
- });
81
- }
82
-
83
- length(limit) {
84
-
85
- Hoek.assert(Number.isSafeInteger(limit) && limit >= 0, 'limit must be a positive integer');
86
-
87
- return this._test('length', limit, function (value, state, options) {
88
-
89
- if (value.length === limit) {
90
- return value;
91
- }
92
-
93
- return this.createError('binary.length', { limit, value }, state, options);
94
- });
95
- }
96
-
97
- };
98
-
99
-
100
- module.exports = new internals.Binary();
@@ -1,100 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Any = require('../any');
6
- const Hoek = require('hoek');
7
-
8
-
9
- // Declare internals
10
-
11
- const internals = {
12
- Set: require('../../set')
13
- };
14
-
15
-
16
- internals.Boolean = class extends Any {
17
- constructor() {
18
-
19
- super();
20
- this._type = 'boolean';
21
- this._flags.insensitive = true;
22
- this._inner.truthySet = new internals.Set();
23
- this._inner.falsySet = new internals.Set();
24
- }
25
-
26
- _base(value, state, options) {
27
-
28
- const result = {
29
- value
30
- };
31
-
32
- if (typeof value === 'string' &&
33
- options.convert) {
34
-
35
- const normalized = this._flags.insensitive ? value.toLowerCase() : value;
36
- result.value = (normalized === 'true' ? true
37
- : (normalized === 'false' ? false : value));
38
- }
39
-
40
- if (typeof result.value !== 'boolean') {
41
- result.value = (this._inner.truthySet.has(value, null, null, this._flags.insensitive) ? true
42
- : (this._inner.falsySet.has(value, null, null, this._flags.insensitive) ? false : value));
43
- }
44
-
45
- result.errors = (typeof result.value === 'boolean') ? null : this.createError('boolean.base', { value }, state, options);
46
- return result;
47
- }
48
-
49
- truthy(...values) {
50
-
51
- const obj = this.clone();
52
- values = Hoek.flatten(values);
53
- for (let i = 0; i < values.length; ++i) {
54
- const value = values[i];
55
-
56
- Hoek.assert(value !== undefined, 'Cannot call truthy with undefined');
57
- obj._inner.truthySet.add(value);
58
- }
59
-
60
- return obj;
61
- }
62
-
63
- falsy(...values) {
64
-
65
- const obj = this.clone();
66
- values = Hoek.flatten(values);
67
- for (let i = 0; i < values.length; ++i) {
68
- const value = values[i];
69
-
70
- Hoek.assert(value !== undefined, 'Cannot call falsy with undefined');
71
- obj._inner.falsySet.add(value);
72
- }
73
-
74
- return obj;
75
- }
76
-
77
- insensitive(enabled) {
78
-
79
- const insensitive = enabled === undefined ? true : !!enabled;
80
-
81
- if (this._flags.insensitive === insensitive) {
82
- return this;
83
- }
84
-
85
- const obj = this.clone();
86
- obj._flags.insensitive = insensitive;
87
- return obj;
88
- }
89
-
90
- describe() {
91
-
92
- const description = super.describe();
93
- description.truthy = [true, ...this._inner.truthySet.values()];
94
- description.falsy = [false, ...this._inner.falsySet.values()];
95
- return description;
96
- }
97
- };
98
-
99
-
100
- module.exports = new internals.Boolean();
@@ -1,182 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Any = require('../any');
6
- const Ref = require('../../ref');
7
- const Hoek = require('hoek');
8
-
9
-
10
- // Declare internals
11
-
12
- const internals = {};
13
-
14
- internals.isoDate = /^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/;
15
- internals.invalidDate = new Date('');
16
- internals.isIsoDate = (() => {
17
-
18
- const isoString = internals.isoDate.toString();
19
-
20
- return (date) => {
21
-
22
- return date && (date.toString() === isoString);
23
- };
24
- })();
25
-
26
- internals.Date = class extends Any {
27
-
28
- constructor() {
29
-
30
- super();
31
- this._type = 'date';
32
- }
33
-
34
- _base(value, state, options) {
35
-
36
- const result = {
37
- value: (options.convert && internals.Date.toDate(value, this._flags.format, this._flags.timestamp, this._flags.multiplier)) || value
38
- };
39
-
40
- if (result.value instanceof Date && !isNaN(result.value.getTime())) {
41
- result.errors = null;
42
- }
43
- else if (!options.convert) {
44
- result.errors = this.createError('date.strict', { value }, state, options);
45
- }
46
- else {
47
- let type;
48
- if (internals.isIsoDate(this._flags.format)) {
49
- type = 'isoDate';
50
- }
51
- else if (this._flags.timestamp) {
52
- type = `timestamp.${this._flags.timestamp}`;
53
- }
54
- else {
55
- type = 'base';
56
- }
57
-
58
- result.errors = this.createError(`date.${type}`, { value }, state, options);
59
- }
60
-
61
- return result;
62
- }
63
-
64
- static toDate(value, format, timestamp, multiplier) {
65
-
66
- if (value instanceof Date) {
67
- return value;
68
- }
69
-
70
- if (typeof value === 'string' ||
71
- (typeof value === 'number' && !isNaN(value) && isFinite(value))) {
72
-
73
- const isIsoDate = format && internals.isIsoDate(format);
74
- if (!isIsoDate &&
75
- typeof value === 'string' &&
76
- /^[+-]?\d+(\.\d+)?$/.test(value)) {
77
-
78
- value = parseFloat(value);
79
- }
80
-
81
- let date;
82
- if (isIsoDate) {
83
- date = format.test(value) ? new Date(value.toString()) : internals.invalidDate;
84
- }
85
- else if (timestamp) {
86
- date = /^\s*$/.test(value) ? internals.invalidDate : new Date(value * multiplier);
87
- }
88
- else {
89
- date = new Date(value);
90
- }
91
-
92
- if (!isNaN(date.getTime())) {
93
- return date;
94
- }
95
- }
96
-
97
- return null;
98
- }
99
-
100
- iso() {
101
-
102
- if (this._flags.format === internals.isoDate) {
103
- return this;
104
- }
105
-
106
- const obj = this.clone();
107
- obj._flags.format = internals.isoDate;
108
- return obj;
109
- }
110
-
111
- timestamp(type = 'javascript') {
112
-
113
- const allowed = ['javascript', 'unix'];
114
- Hoek.assert(allowed.includes(type), '"type" must be one of "' + allowed.join('", "') + '"');
115
-
116
- if (this._flags.timestamp === type) {
117
- return this;
118
- }
119
-
120
- const obj = this.clone();
121
- obj._flags.timestamp = type;
122
- obj._flags.multiplier = type === 'unix' ? 1000 : 1;
123
- return obj;
124
- }
125
-
126
- _isIsoDate(value) {
127
-
128
- return internals.isoDate.test(value);
129
- }
130
-
131
- };
132
-
133
- internals.compare = function (type, compare) {
134
-
135
- return function (date) {
136
-
137
- const isNow = date === 'now';
138
- const isRef = Ref.isRef(date);
139
-
140
- if (!isNow && !isRef) {
141
- date = internals.Date.toDate(date);
142
- }
143
-
144
- Hoek.assert(date, 'Invalid date format');
145
-
146
- return this._test(type, date, function (value, state, options) {
147
-
148
- let compareTo;
149
- if (isNow) {
150
- compareTo = Date.now();
151
- }
152
- else if (isRef) {
153
- const refValue = date(state.reference || state.parent, options);
154
- compareTo = internals.Date.toDate(refValue);
155
-
156
- if (!compareTo) {
157
- return this.createError('date.ref', { ref: date, value: refValue }, state, options);
158
- }
159
-
160
- compareTo = compareTo.getTime();
161
- }
162
- else {
163
- compareTo = date.getTime();
164
- }
165
-
166
- if (compare(value.getTime(), compareTo)) {
167
- return value;
168
- }
169
-
170
- return this.createError('date.' + type, { limit: new Date(compareTo), value }, state, options);
171
- });
172
- };
173
- };
174
-
175
-
176
- internals.Date.prototype.min = internals.compare('min', (value, date) => value >= date);
177
- internals.Date.prototype.max = internals.compare('max', (value, date) => value <= date);
178
- internals.Date.prototype.greater = internals.compare('greater', (value, date) => value > date);
179
- internals.Date.prototype.less = internals.compare('less', (value, date) => value < date);
180
-
181
-
182
- module.exports = new internals.Date();
@@ -1,90 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Hoek = require('hoek');
6
- const ObjectType = require('../object');
7
- const Ref = require('../../ref');
8
-
9
-
10
- // Declare internals
11
-
12
- const internals = {};
13
-
14
-
15
- internals.Func = class extends ObjectType.constructor {
16
-
17
- constructor() {
18
-
19
- super();
20
- this._flags.func = true;
21
- }
22
-
23
- arity(n) {
24
-
25
- Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
26
-
27
- return this._test('arity', n, function (value, state, options) {
28
-
29
- if (value.length === n) {
30
- return value;
31
- }
32
-
33
- return this.createError('function.arity', { n }, state, options);
34
- });
35
- }
36
-
37
- minArity(n) {
38
-
39
- Hoek.assert(Number.isSafeInteger(n) && n > 0, 'n must be a strict positive integer');
40
-
41
- return this._test('minArity', n, function (value, state, options) {
42
-
43
- if (value.length >= n) {
44
- return value;
45
- }
46
-
47
- return this.createError('function.minArity', { n }, state, options);
48
- });
49
- }
50
-
51
- maxArity(n) {
52
-
53
- Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
54
-
55
- return this._test('maxArity', n, function (value, state, options) {
56
-
57
- if (value.length <= n) {
58
- return value;
59
- }
60
-
61
- return this.createError('function.maxArity', { n }, state, options);
62
- });
63
- }
64
-
65
- ref() {
66
-
67
- return this._test('ref', null, function (value, state, options) {
68
-
69
- if (Ref.isRef(value)) {
70
- return value;
71
- }
72
-
73
- return this.createError('function.ref', { value }, state, options);
74
- });
75
- }
76
-
77
- class() {
78
-
79
- return this._test('class', null, function (value, state, options) {
80
-
81
- if ((/^\s*class\s/).test(value.toString())) {
82
- return value;
83
- }
84
-
85
- return this.createError('function.class', { value }, state, options);
86
- });
87
- }
88
- };
89
-
90
- module.exports = new internals.Func();
@@ -1,82 +0,0 @@
1
- 'use strict';
2
-
3
- // Load modules
4
-
5
- const Any = require('../any');
6
- const Hoek = require('hoek');
7
-
8
-
9
- // Declare internals
10
-
11
- const internals = {};
12
-
13
-
14
- internals.Lazy = class extends Any {
15
-
16
- constructor() {
17
-
18
- super();
19
- this._type = 'lazy';
20
- this._flags.once = true;
21
- this._cache = null;
22
- }
23
-
24
- _init(fn, options) {
25
-
26
- return this.set(fn, options);
27
- }
28
-
29
- _base(value, state, options) {
30
-
31
- let schema;
32
- if (this._cache) {
33
- schema = this._cache;
34
- }
35
- else {
36
- const result = { value };
37
- const lazy = this._flags.lazy;
38
-
39
- if (!lazy) {
40
- result.errors = this.createError('lazy.base', null, state, options);
41
- return result;
42
- }
43
-
44
- schema = lazy();
45
-
46
- if (!(schema instanceof Any)) {
47
- result.errors = this.createError('lazy.schema', { schema }, state, options);
48
- return result;
49
- }
50
-
51
- if (this._flags.once) {
52
- this._cache = schema;
53
- }
54
- }
55
-
56
- return schema._validate(value, state, options);
57
- }
58
-
59
- set(fn, options) {
60
-
61
- Hoek.assert(typeof fn === 'function', 'You must provide a function as first argument');
62
- Hoek.assert(options === undefined || (options && typeof options === 'object' && !Array.isArray(options)), `Options must be an object`);
63
-
64
- if (options) {
65
- const unknownOptions = Object.keys(options).filter((key) => !['once'].includes(key));
66
- Hoek.assert(unknownOptions.length === 0, `Options contain unknown keys: ${unknownOptions}`);
67
- Hoek.assert(options.once === undefined || typeof options.once === 'boolean', 'Option "once" must be a boolean');
68
- }
69
-
70
- const obj = this.clone();
71
- obj._flags.lazy = fn;
72
-
73
- if (options && options.once !== obj._flags.once) {
74
- obj._flags.once = options.once;
75
- }
76
-
77
- return obj;
78
- }
79
-
80
- };
81
-
82
- module.exports = new internals.Lazy();