json-2-joi 2.0.1 → 2.0.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 2.0.2
2
+
3
+ - Remove `min` setting from JOI schema if `minLength` is not defined in JSON schema.
4
+ - Allow `null` value as Array's item if `allowNull` option is set to true.
5
+
1
6
  ### 2.0.1
2
7
 
3
8
  - Set correct examples data for Array type field.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Converts a JSON schema to a Joi schema for object validation.
6
6
 
7
- ## Change based on the forked version (Enjoi):
7
+ ## Change based on the forked version (enjoi):
8
8
 
9
9
  - Provided `refineDescription(schema)`, `allowNull` options
10
10
  - Change cycle reference resovling without self-generating id and rely on $id if defined in JSON schema
package/lib/resolver.js CHANGED
@@ -35,6 +35,13 @@ function isNotEmptyObject(value) {
35
35
  return isObject(value) && Object.keys(value).length > 0;
36
36
  }
37
37
 
38
+ function allowNullIfNeeded(schema, allowNull = false) {
39
+ if (allowNull) {
40
+ return schema.allow(null);
41
+ }
42
+ return schema;
43
+ }
44
+
38
45
  function normalizedId(id) {
39
46
  return id.replace(/[^a-zA-Z0-9]/g, '_');
40
47
  }
@@ -426,8 +433,8 @@ class SchemaResolver {
426
433
 
427
434
  if (Array.isArray(schema.required) && !!~schema.required.indexOf(key)) {
428
435
  joischema = joischema.required();
429
- } else if (this.allowNull) {
430
- joischema = joischema.allow(null);
436
+ } else {
437
+ joischema = allowNullIfNeeded(joischema, this.allowNull);
431
438
  }
432
439
 
433
440
  schemas[key] = joischema;
@@ -450,25 +457,25 @@ class SchemaResolver {
450
457
  return joischema;
451
458
  }
452
459
 
460
+ resolveAsArray(value, baseUri) {
461
+ if (Array.isArray(value)) {
462
+ // found an array, thus its _per type_
463
+ return value.map((v) => allowNullIfNeeded(this.resolve(v, baseUri), this.allowNull));
464
+ }
465
+ // it's a single entity, so just resolve it normally
466
+ return [allowNullIfNeeded(this.resolve(value, baseUri), this.allowNull)];
467
+ }
468
+
453
469
  array(schema, baseUri) {
454
470
  let joischema = this.joi.array();
455
471
  let items;
456
472
 
457
- const resolveAsArray = (value) => {
458
- if (Array.isArray(value)) {
459
- // found an array, thus its _per type_
460
- return value.map((v) => this.resolve(v, baseUri));
461
- }
462
- // it's a single entity, so just resolve it normally
463
- return [this.resolve(value, baseUri)];
464
- }
465
-
466
473
  if (schema.items) {
467
- items = resolveAsArray(schema.items);
474
+ items = this.resolveAsArray(schema.items, baseUri);
468
475
 
469
476
  joischema = joischema.items(...items);
470
477
  } else if (schema.ordered) {
471
- items = resolveAsArray(schema.ordered);
478
+ items = this.resolveAsArray(schema.ordered, baseUri);
472
479
  joischema = joischema.ordered(...items);
473
480
  }
474
481
 
@@ -554,11 +561,7 @@ class SchemaResolver {
554
561
  regularString(schema, joischema) {
555
562
  schema.pattern && (joischema = joischema.regex(new RegExp(schema.pattern)));
556
563
 
557
- if (typeof schema.minLength === 'undefined') {
558
- schema.minLength = 0;
559
- }
560
-
561
- if (schema.minLength === 0 && !schema.pattern && !schema.format) {
564
+ if ((typeof schema.minLength === 'undefined' || schema.minLength === 0) && !schema.pattern && !schema.format) {
562
565
  joischema = joischema.allow('');
563
566
  }
564
567
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "json-2-joi",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "license": "Apache 2.0",
5
- "description": "Converts json-schema to Joi schema.",
5
+ "description": "Converts json-schema to Joi schema. (Enhanced enjoi)",
6
6
  "main": "./index.js",
7
7
  "scripts": {
8
8
  "test": "tape test/*.js",