Comparing version 3.2.4 to 3.2.5
@@ -0,1 +1,5 @@ | ||
### v3.2.5 | ||
* Fixed allOf support (#53) | ||
### v3.2.4 | ||
@@ -2,0 +6,0 @@ |
'use strict'; | ||
const Joi = require('joi'); | ||
const _Joi = require('joi'); | ||
const Util = require('util'); | ||
const Hoek = require('hoek'); | ||
const Joi = _Joi.extend({ | ||
name: 'any', | ||
rules: [{ | ||
name: 'allOf', | ||
params: { | ||
items: _Joi.array().items(_Joi.object()).required() | ||
}, | ||
validate(params, value, state, options) { | ||
let error; | ||
for (const schema of params.items) { | ||
_Joi.validate(value, schema, { allowUnknown: true }, (err) => { | ||
error = err; | ||
}); | ||
if (error) { | ||
break; | ||
} | ||
} | ||
if (error) { | ||
const details = error.details[0]; | ||
return this.createError(details.type, details.context, { path : details.path }, options); | ||
} | ||
return value; | ||
} | ||
}] | ||
}); | ||
const schemaSchema = Joi.alternatives(Joi.object().unknown(true), Joi.string()).required(); | ||
@@ -38,3 +64,3 @@ | ||
if (current.allOf) { | ||
return resolveAllOf(current); | ||
return resolveAllOf(current.allOf); | ||
} | ||
@@ -66,3 +92,3 @@ | ||
//eslint-disable-next-line no-console | ||
console.warn('WARNING: schema missing a \'type\' or \'$ref\' or \'enum\': %s', JSON.stringify(current)); | ||
console.warn('WARNING: schema missing a \'type\' or \'$ref\' or \'enum\': \n%s', JSON.stringify(current, null, 2)); | ||
return Joi.any(); | ||
@@ -183,18 +209,12 @@ } | ||
function resolveAllOf(current) { | ||
Hoek.assert(Util.isArray(current.allOf), 'Expected allOf to be an array.'); | ||
function resolveAllOf(items) { | ||
Hoek.assert(Util.isArray(items), 'Expected allOf to be an array.'); | ||
const type = getSchemaType(current.allOf[0]); | ||
current.allOf.map(function (schema) { | ||
const _type = getSchemaType(schema); | ||
Hoek.assert((_type === 'array' || _type === 'object') && type === _type, | ||
'Expected allOf item to be an array or object.'); | ||
}) | ||
const allOf = []; | ||
if (type === 'object') { | ||
return resolve(mergeObject(current.allOf)); | ||
for (const item of items) { | ||
allOf.push(resolve(item)); | ||
} | ||
else { | ||
return resolve(mergeArray(current.allOf)); | ||
} | ||
return Joi.any().allOf(allOf); | ||
} | ||
@@ -368,47 +388,2 @@ | ||
function getSchemaType(current) { | ||
if (current.type) { | ||
return current.type; | ||
} | ||
if (current.$ref) { | ||
return resolveref(current.$ref).type; | ||
} | ||
} | ||
function mergeArray(current) { | ||
const result = {type: 'array'}; | ||
let items = []; | ||
let ordered = []; | ||
for (let item of current) { | ||
if (item.$ref) { | ||
item = resolveref(item.$ref); | ||
} | ||
if (item.items instanceof Array) { | ||
items = items.concat(item.items); | ||
ordered = ordered.concat(item.ordered || []); | ||
} else { | ||
items = items.concat([item.items]); | ||
} | ||
} | ||
result.items = items; | ||
result.ordered = ordered.length ? ordered : undefined; | ||
return result; | ||
} | ||
function mergeObject(current) { | ||
const result = {type: 'object', properties: {}}; | ||
const properties = result.properties; | ||
let required = []; | ||
for (let item of current) { | ||
if (item.$ref) { | ||
item = resolveref(item.$ref); | ||
} | ||
Object.assign(properties, item.properties); | ||
required = required.concat(item.required || []); | ||
} | ||
result.required = required.length ? required : undefined; | ||
return result; | ||
} | ||
return resolve(schema); | ||
@@ -415,0 +390,0 @@ }; |
{ | ||
"name": "enjoi", | ||
"version": "3.2.4", | ||
"version": "3.2.5", | ||
"license": "Apache 2.0", | ||
@@ -28,3 +28,3 @@ "description": "Converts json-schema to Joi schema for validation.", | ||
], | ||
"author": "Trevor Livingston", | ||
"author": "Trevor Livingston <tlivings@gmail.com>", | ||
"bugs": { | ||
@@ -31,0 +31,0 @@ "url": "https://github.com/tlivings/enjoi/issues" |
@@ -13,3 +13,3 @@ [![Build Status](https://travis-ci.org/tlivings/enjoi.png)](https://travis-ci.org/tlivings/enjoi) [![NPM version](https://badge.fury.io/js/enjoi.png)](http://badge.fury.io/js/enjoi) | ||
- `object:patternProperties` - Joi limitation. | ||
- `object:patternProperties` - TBD. | ||
@@ -16,0 +16,0 @@ ### API |
28945
305