joi 17.13.3 → 18.0.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.
- package/dist/joi-browser.min.js +1 -1
- package/lib/annotate.js +2 -2
- package/lib/base.js +151 -74
- package/lib/cache.js +3 -4
- package/lib/common.js +1 -2
- package/lib/compile.js +23 -23
- package/lib/extend.js +10 -11
- package/lib/index.d.ts +2627 -2333
- package/lib/index.js +9 -10
- package/lib/manifest.js +15 -16
- package/lib/messages.js +10 -11
- package/lib/modify.js +10 -10
- package/lib/ref.js +15 -17
- package/lib/state.js +4 -5
- package/lib/template.js +8 -10
- package/lib/trace.js +4 -4
- package/lib/types/alternatives.js +33 -10
- package/lib/types/any.js +5 -5
- package/lib/types/array.js +32 -10
- package/lib/types/binary.js +3 -3
- package/lib/types/boolean.js +3 -3
- package/lib/types/date.js +3 -3
- package/lib/types/function.js +4 -4
- package/lib/types/keys.js +39 -16
- package/lib/types/link.js +11 -11
- package/lib/types/number.js +4 -4
- package/lib/types/string.js +84 -52
- package/lib/types/symbol.js +5 -5
- package/lib/validator.js +26 -18
- package/lib/values.js +4 -5
- package/package.json +21 -12
package/lib/compile.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const { assert } = require('@hapi/hoek');
|
|
4
4
|
|
|
5
5
|
const Common = require('./common');
|
|
6
6
|
const Ref = require('./ref');
|
|
@@ -30,10 +30,10 @@ exports.schema = function (Joi, config, options = {}) {
|
|
|
30
30
|
|
|
31
31
|
internals.schema = function (Joi, config, options) {
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
assert(config !== undefined, 'Invalid undefined schema');
|
|
34
34
|
|
|
35
35
|
if (Array.isArray(config)) {
|
|
36
|
-
|
|
36
|
+
assert(config.length, 'Invalid empty array schema');
|
|
37
37
|
|
|
38
38
|
if (config.length === 1) {
|
|
39
39
|
config = config[0];
|
|
@@ -57,7 +57,7 @@ internals.schema = function (Joi, config, options) {
|
|
|
57
57
|
return Joi.custom(config);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
assert(typeof config === 'object', 'Invalid schema content:', typeof config);
|
|
61
61
|
|
|
62
62
|
if (Common.isResolvable(config)) {
|
|
63
63
|
return valid(Joi, config);
|
|
@@ -85,7 +85,7 @@ internals.schema = function (Joi, config, options) {
|
|
|
85
85
|
return valid(Joi.date(), config);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
assert(Object.getPrototypeOf(config) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
|
|
89
89
|
|
|
90
90
|
return Joi.object().keys(config);
|
|
91
91
|
};
|
|
@@ -105,7 +105,7 @@ exports.compile = function (root, schema, options = {}) {
|
|
|
105
105
|
|
|
106
106
|
const any = schema && schema[Common.symbols.any];
|
|
107
107
|
if (any) {
|
|
108
|
-
|
|
108
|
+
assert(options.legacy || any.version === Common.version, 'Cannot mix different versions of joi schemas:', any.version, Common.version);
|
|
109
109
|
return schema;
|
|
110
110
|
}
|
|
111
111
|
|
|
@@ -150,7 +150,7 @@ internals.walk = function (schema) {
|
|
|
150
150
|
return { root: schema[any.root], compile: any.compile };
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
assert(Object.getPrototypeOf(schema) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
|
|
154
154
|
|
|
155
155
|
for (const key in schema) {
|
|
156
156
|
const compiler = internals.walk(schema[key]);
|
|
@@ -172,7 +172,7 @@ internals.simple = function (value) {
|
|
|
172
172
|
exports.when = function (schema, condition, options) {
|
|
173
173
|
|
|
174
174
|
if (options === undefined) {
|
|
175
|
-
|
|
175
|
+
assert(condition && typeof condition === 'object', 'Missing options');
|
|
176
176
|
|
|
177
177
|
options = condition;
|
|
178
178
|
condition = Ref.create('.');
|
|
@@ -187,17 +187,17 @@ exports.when = function (schema, condition, options) {
|
|
|
187
187
|
// Schema condition
|
|
188
188
|
|
|
189
189
|
if (Common.isSchema(condition)) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
190
|
+
assert(options.is === undefined, '"is" can not be used with a schema condition');
|
|
191
|
+
assert(options.not === undefined, '"not" can not be used with a schema condition');
|
|
192
|
+
assert(options.switch === undefined, '"switch" can not be used with a schema condition');
|
|
193
193
|
|
|
194
194
|
return internals.condition(schema, { is: condition, then: options.then, otherwise: options.otherwise, break: options.break });
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
// Single condition
|
|
198
198
|
|
|
199
|
-
|
|
200
|
-
|
|
199
|
+
assert(Ref.isRef(condition) || typeof condition === 'string', 'Invalid condition:', condition);
|
|
200
|
+
assert(options.not === undefined || options.is === undefined, 'Cannot combine "is" with "not"');
|
|
201
201
|
|
|
202
202
|
if (options.switch === undefined) {
|
|
203
203
|
let rule = options;
|
|
@@ -206,8 +206,8 @@ exports.when = function (schema, condition, options) {
|
|
|
206
206
|
}
|
|
207
207
|
|
|
208
208
|
let is = rule.is !== undefined ? schema.$_compile(rule.is) : schema.$_root.invalid(null, false, 0, '').required();
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
assert(rule.then !== undefined || rule.otherwise !== undefined, 'options must have at least one of "then", "otherwise", or "switch"');
|
|
210
|
+
assert(rule.break === undefined || rule.then === undefined || rule.otherwise === undefined, 'Cannot specify then, otherwise, and break all together');
|
|
211
211
|
|
|
212
212
|
if (options.is !== undefined &&
|
|
213
213
|
!Ref.isRef(options.is) &&
|
|
@@ -221,10 +221,10 @@ exports.when = function (schema, condition, options) {
|
|
|
221
221
|
|
|
222
222
|
// Switch statement
|
|
223
223
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
224
|
+
assert(Array.isArray(options.switch), '"switch" must be an array');
|
|
225
|
+
assert(options.is === undefined, 'Cannot combine "switch" with "is"');
|
|
226
|
+
assert(options.not === undefined, 'Cannot combine "switch" with "not"');
|
|
227
|
+
assert(options.then === undefined, 'Cannot combine "switch" with "then"');
|
|
228
228
|
|
|
229
229
|
const rule = {
|
|
230
230
|
ref: exports.ref(condition),
|
|
@@ -238,8 +238,8 @@ exports.when = function (schema, condition, options) {
|
|
|
238
238
|
|
|
239
239
|
Common.assertOptions(test, last ? ['is', 'then', 'otherwise'] : ['is', 'then']);
|
|
240
240
|
|
|
241
|
-
|
|
242
|
-
|
|
241
|
+
assert(test.is !== undefined, 'Switch statement missing "is"');
|
|
242
|
+
assert(test.then !== undefined, 'Switch statement missing "then"');
|
|
243
243
|
|
|
244
244
|
const item = {
|
|
245
245
|
is: schema.$_compile(test.is),
|
|
@@ -253,10 +253,10 @@ exports.when = function (schema, condition, options) {
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
if (last) {
|
|
256
|
-
|
|
256
|
+
assert(options.otherwise === undefined || test.otherwise === undefined, 'Cannot specify "otherwise" inside and outside a "switch"');
|
|
257
257
|
const otherwise = options.otherwise !== undefined ? options.otherwise : test.otherwise;
|
|
258
258
|
if (otherwise !== undefined) {
|
|
259
|
-
|
|
259
|
+
assert(rule.break === undefined, 'Cannot specify both otherwise and break');
|
|
260
260
|
item.otherwise = schema.$_compile(otherwise);
|
|
261
261
|
}
|
|
262
262
|
}
|
package/lib/extend.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const Clone = require('@hapi/hoek/lib/clone');
|
|
3
|
+
const { assert, clone } = require('@hapi/hoek');
|
|
5
4
|
|
|
6
5
|
const Common = require('./common');
|
|
7
6
|
const Messages = require('./messages');
|
|
@@ -13,7 +12,7 @@ const internals = {};
|
|
|
13
12
|
exports.type = function (from, options) {
|
|
14
13
|
|
|
15
14
|
const base = Object.getPrototypeOf(from);
|
|
16
|
-
const prototype =
|
|
15
|
+
const prototype = clone(base);
|
|
17
16
|
const schema = from._assign(Object.create(prototype));
|
|
18
17
|
const def = Object.assign({}, options); // Shallow cloned
|
|
19
18
|
delete def.base;
|
|
@@ -38,7 +37,7 @@ exports.type = function (from, options) {
|
|
|
38
37
|
if (def.terms) {
|
|
39
38
|
for (const name in def.terms) { // Only apply own terms
|
|
40
39
|
const term = def.terms[name];
|
|
41
|
-
|
|
40
|
+
assert(schema.$_terms[name] === undefined, 'Invalid term override for', def.type, name);
|
|
42
41
|
schema.$_terms[name] = term.init;
|
|
43
42
|
terms[name] = term;
|
|
44
43
|
}
|
|
@@ -82,7 +81,7 @@ exports.type = function (from, options) {
|
|
|
82
81
|
if (def.rules) {
|
|
83
82
|
for (const name in def.rules) {
|
|
84
83
|
const rule = def.rules[name];
|
|
85
|
-
|
|
84
|
+
assert(typeof rule === 'object', 'Invalid rule definition for', def.type, name);
|
|
86
85
|
|
|
87
86
|
let method = rule.method;
|
|
88
87
|
if (method === undefined) {
|
|
@@ -93,11 +92,11 @@ exports.type = function (from, options) {
|
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
if (method) {
|
|
96
|
-
|
|
95
|
+
assert(!prototype[name], 'Rule conflict in', def.type, name);
|
|
97
96
|
prototype[name] = method;
|
|
98
97
|
}
|
|
99
98
|
|
|
100
|
-
|
|
99
|
+
assert(!rules[name], 'Rule conflict in', def.type, name);
|
|
101
100
|
rules[name] = rule;
|
|
102
101
|
|
|
103
102
|
if (rule.alias) {
|
|
@@ -115,7 +114,7 @@ exports.type = function (from, options) {
|
|
|
115
114
|
arg = { name: arg };
|
|
116
115
|
}
|
|
117
116
|
|
|
118
|
-
|
|
117
|
+
assert(!rule.argsByName.has(arg.name), 'Duplicated argument name', arg.name);
|
|
119
118
|
|
|
120
119
|
if (Common.isSchema(arg.assert)) {
|
|
121
120
|
arg.assert = arg.assert.strict().label(arg.name);
|
|
@@ -135,10 +134,10 @@ exports.type = function (from, options) {
|
|
|
135
134
|
const modifiers = Object.assign({}, parent.modifiers);
|
|
136
135
|
if (def.modifiers) {
|
|
137
136
|
for (const name in def.modifiers) {
|
|
138
|
-
|
|
137
|
+
assert(!prototype[name], 'Rule conflict in', def.type, name);
|
|
139
138
|
|
|
140
139
|
const modifier = def.modifiers[name];
|
|
141
|
-
|
|
140
|
+
assert(typeof modifier === 'function', 'Invalid modifier definition for', def.type, name);
|
|
142
141
|
|
|
143
142
|
const method = function (arg) {
|
|
144
143
|
|
|
@@ -158,7 +157,7 @@ exports.type = function (from, options) {
|
|
|
158
157
|
prototype._super = base;
|
|
159
158
|
schema.$_super = {}; // Backwards compatibility
|
|
160
159
|
for (const override in def.overrides) {
|
|
161
|
-
|
|
160
|
+
assert(base[override], 'Cannot override missing', override);
|
|
162
161
|
def.overrides[override][Common.symbols.parent] = base[override];
|
|
163
162
|
schema.$_super[override] = base[override].bind(schema); // Backwards compatibility
|
|
164
163
|
}
|