json-2-joi 2.3.1 → 2.3.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,7 @@
1
+ ### 2.3.2
2
+
3
+ - Enhance `null` value support when `type` is array and contains `null`.
4
+
1
5
  ### 2.3.1
2
6
 
3
7
  - Fix `noDefaults` options validation issue.
package/lib/resolver.js CHANGED
@@ -357,10 +357,6 @@ class SchemaResolver {
357
357
  resolveType(schema, resolveOptions, baseUri) {
358
358
  let joischema;
359
359
 
360
- const typeDefinitionMap = {
361
- title: 'label'
362
- };
363
-
364
360
  const joitype = (type, options, format) => {
365
361
  let joischema;
366
362
 
@@ -397,6 +393,19 @@ class SchemaResolver {
397
393
  return joischema;
398
394
  }
399
395
 
396
+ let nullIdx;
397
+ if (Array.isArray(schema.type)) {
398
+ nullIdx = schema.type.findIndex((type) => type === 'null');
399
+ if (nullIdx > -1) {
400
+ schema.type.splice(nullIdx, 1);
401
+ }
402
+ if (schema.type.length === 1) {
403
+ schema.type = schema.type[0];
404
+ } else if (schema.type.length === 0) {
405
+ schema.type = 'null';
406
+ }
407
+ }
408
+
400
409
  if (Array.isArray(schema.type)) {
401
410
  const schemas = [];
402
411
 
@@ -408,7 +417,13 @@ class SchemaResolver {
408
417
  } else {
409
418
  joischema = joitype(schema.type, resolveOptions, schema.format);
410
419
  }
420
+ if (nullIdx > -1) {
421
+ joischema = joischema.allow(null);
422
+ }
411
423
 
424
+ const typeDefinitionMap = {
425
+ title: 'label'
426
+ };
412
427
  Object.keys(typeDefinitionMap).forEach(function (key) {
413
428
  if (schema[key] !== undefined) {
414
429
  joischema = joischema[typeDefinitionMap[key]](schema[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-2-joi",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "license": "Apache 2.0",
5
5
  "description": "Converts json-schema to Joi schema. (Enhanced enjoi)",
6
6
  "main": "./index.js",