json-2-joi 2.5.0 → 2.5.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/README.md CHANGED
@@ -90,7 +90,7 @@ const schema = Json2Joi.schema({
90
90
 
91
91
  const { error, value } = schema.validate({firstName: 'John', lastName: 'Doe', age: 45});
92
92
  ```
93
- schema.additionalProperties !== false
93
+
94
94
  ### joiOptions
95
95
 
96
96
  When this option is provided, a Joi instance will be created as `joiInstance = Joi.defaults((schema) => schema.options(joiOptions));` for building Joi Schema.
package/lib/resolver.js CHANGED
@@ -222,7 +222,7 @@ class SchemaResolver {
222
222
  return joiSchema;
223
223
  }
224
224
 
225
- resolve(schema, resolveOptions = {}, baseUri = (schema.$id || '')) {
225
+ resolve(schema, resolveOptions = {}, baseUri = (schema.$id || ''), isPropertyRequired = false) {
226
226
  if (this.joi.isSchema(schema)) {
227
227
  return schema;
228
228
  }
@@ -242,17 +242,17 @@ class SchemaResolver {
242
242
 
243
243
  if (typeof schema === 'string') {
244
244
  // If schema is itself a string, interpret it as a type
245
- resolvedSchema = this.resolveType({ type: schema }, resolveOptions, baseUri);
245
+ resolvedSchema = this.resolveType({ type: schema }, resolveOptions, baseUri, isPropertyRequired);
246
246
  } else if (schema.$ref) {
247
247
  if (schema.$ref.trim() === '#') {
248
248
  return baseUri ? this.joi.link(`#${normalizedId(baseUri)}`) : this.joi.link('/');
249
249
  }
250
250
 
251
- resolvedSchema = this.resolve(this.resolveReference(schema.$ref, baseUri), resolveOptions, baseUri);
251
+ resolvedSchema = this.resolve(this.resolveReference(schema.$ref, baseUri), resolveOptions, baseUri, isPropertyRequired);
252
252
  } else {
253
253
  const partialSchemas = [];
254
254
  if (schema.type) {
255
- partialSchemas.push(this.resolveType(schema, resolveOptions, baseUri));
255
+ partialSchemas.push(this.resolveType(schema, resolveOptions, baseUri, isPropertyRequired));
256
256
  } else if (schema.properties) {
257
257
  // if no type is specified, just properties
258
258
  partialSchemas.push(this.object(schema, resolveOptions, baseUri))
@@ -307,6 +307,15 @@ class SchemaResolver {
307
307
  this.joiSharedSchemas.set(`${baseUri}#${schema.$anchor}`, resolvedSchema);
308
308
  }
309
309
 
310
+ if (isPropertyRequired) {
311
+ resolvedSchema = resolvedSchema.required();
312
+ if (resolveOptions.strictRequired) {
313
+ resolvedSchema = resolvedSchema.invalid(null, '');
314
+ }
315
+ } else {
316
+ resolvedSchema = allowNullIfNeeded(resolvedSchema, resolveOptions);
317
+ }
318
+
310
319
  return resolvedSchema;
311
320
  }
312
321
 
@@ -358,7 +367,7 @@ class SchemaResolver {
358
367
  return refSchema;
359
368
  }
360
369
 
361
- resolveType(schema, resolveOptions, baseUri) {
370
+ resolveType(schema, resolveOptions, baseUri, isPropertyRequired = false) {
362
371
  let joischema;
363
372
 
364
373
  const joitype = (type, options, format) => {
@@ -370,7 +379,7 @@ class SchemaResolver {
370
379
 
371
380
  switch (type) {
372
381
  case 'array':
373
- joischema = this.array(schema, options, baseUri);
382
+ joischema = this.array(schema, options, baseUri, isPropertyRequired);
374
383
  break;
375
384
  case 'boolean':
376
385
  joischema = this.joi.boolean();
@@ -500,17 +509,9 @@ class SchemaResolver {
500
509
 
501
510
  Object.keys(jsonSchema.properties).forEach((key) => {
502
511
  const property = jsonSchema.properties[key];
512
+ const isPropertyRequired = Array.isArray(jsonSchema.required) && !!~jsonSchema.required.indexOf(key);
503
513
 
504
- let joischema = this.resolve(property, resolveOptions, baseUri);
505
-
506
- if (Array.isArray(jsonSchema.required) && !!~jsonSchema.required.indexOf(key)) {
507
- joischema = joischema.required();
508
- if (resolveOptions.strictRequired) {
509
- joischema = joischema.invalid(null, '');
510
- }
511
- } else {
512
- joischema = allowNullIfNeeded(joischema, resolveOptions);
513
- }
514
+ const joischema = this.resolve(property, resolveOptions, baseUri, isPropertyRequired);
514
515
 
515
516
  schemas[key] = joischema;
516
517
  });
@@ -533,26 +534,26 @@ class SchemaResolver {
533
534
  return joischema;
534
535
  }
535
536
 
536
- resolveAsArray(value, resolveOptions, baseUri) {
537
+ resolveAsArray(value, resolveOptions, baseUri, isPropertyRequired = false) {
537
538
  return [].concat(value).map((v) => {
538
539
  const vSchema = this.resolve(v, resolveOptions, baseUri);
539
- if (resolveOptions.strictArrayRequired) {
540
+ if (resolveOptions.strictArrayRequired && isPropertyRequired) {
540
541
  return vSchema.invalid(...resolveOptions.customizedNullValues);
541
542
  }
542
543
  return allowNullIfNeeded(vSchema, resolveOptions)
543
544
  });
544
545
  }
545
546
 
546
- array(schema, resolveOptions, baseUri) {
547
+ array(schema, resolveOptions, baseUri, isPropertyRequired = false) {
547
548
  let joischema = this.joi.array();
548
549
  let items;
549
550
 
550
551
  if (schema.items) {
551
- items = this.resolveAsArray(schema.items, resolveOptions, baseUri);
552
+ items = this.resolveAsArray(schema.items, resolveOptions, baseUri, isPropertyRequired);
552
553
 
553
554
  joischema = joischema.items(...items);
554
555
  } else if (schema.ordered) {
555
- items = this.resolveAsArray(schema.ordered, resolveOptions, baseUri);
556
+ items = this.resolveAsArray(schema.ordered, resolveOptions, baseUri, isPropertyRequired);
556
557
  joischema = joischema.ordered(...items);
557
558
  }
558
559
 
@@ -560,7 +561,7 @@ class SchemaResolver {
560
561
  joischema = joischema.max(items.length);
561
562
  }
562
563
 
563
- if (resolveOptions.strictArrayRequired && !schema.minItems) {
564
+ if (isPropertyRequired && resolveOptions.strictArrayRequired && !schema.minItems) {
564
565
  schema.minItems = 1;
565
566
  }
566
567
  isNumber(schema.minItems) && (joischema = joischema.min(schema.minItems));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-2-joi",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "license": "Apache 2.0",
5
5
  "description": "Converts json-schema to Joi schema. (Enhanced enjoi)",
6
6
  "main": "./index.js",