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/lib/compile.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const Assert = require('@hapi/hoek/lib/assert');
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
- Assert(config !== undefined, 'Invalid undefined schema');
33
+ assert(config !== undefined, 'Invalid undefined schema');
34
34
 
35
35
  if (Array.isArray(config)) {
36
- Assert(config.length, 'Invalid empty array schema');
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
- Assert(typeof config === 'object', 'Invalid schema content:', typeof config);
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
- Assert(Object.getPrototypeOf(config) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
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
- Assert(options.legacy || any.version === Common.version, 'Cannot mix different versions of joi schemas:', any.version, Common.version);
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
- Assert(Object.getPrototypeOf(schema) === Object.getPrototypeOf({}), 'Schema can only contain plain objects');
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
- Assert(condition && typeof condition === 'object', 'Missing options');
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
- 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');
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
- Assert(Ref.isRef(condition) || typeof condition === 'string', 'Invalid condition:', condition);
200
- Assert(options.not === undefined || options.is === undefined, 'Cannot combine "is" with "not"');
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
- 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');
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
- 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"');
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
- Assert(test.is !== undefined, 'Switch statement missing "is"');
242
- Assert(test.then !== undefined, 'Switch statement missing "then"');
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
- Assert(options.otherwise === undefined || test.otherwise === undefined, 'Cannot specify "otherwise" inside and outside a "switch"');
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
- Assert(rule.break === undefined, 'Cannot specify both otherwise and break');
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 Assert = require('@hapi/hoek/lib/assert');
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 = Clone(base);
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
- Assert(schema.$_terms[name] === undefined, 'Invalid term override for', def.type, name);
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
- Assert(typeof rule === 'object', 'Invalid rule definition for', def.type, name);
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
- Assert(!prototype[name], 'Rule conflict in', def.type, name);
95
+ assert(!prototype[name], 'Rule conflict in', def.type, name);
97
96
  prototype[name] = method;
98
97
  }
99
98
 
100
- Assert(!rules[name], 'Rule conflict in', def.type, name);
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
- Assert(!rule.argsByName.has(arg.name), 'Duplicated argument name', arg.name);
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
- Assert(!prototype[name], 'Rule conflict in', def.type, name);
137
+ assert(!prototype[name], 'Rule conflict in', def.type, name);
139
138
 
140
139
  const modifier = def.modifiers[name];
141
- Assert(typeof modifier === 'function', 'Invalid modifier definition for', def.type, name);
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
- Assert(base[override], 'Cannot override missing', override);
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
  }