Comparing version 9.0.0 to 9.0.1
@@ -0,2 +1,5 @@ | ||
### 9.0.1 | ||
* #112 Use joi.link to only resolve strictly recursive self-references | ||
### 9.0.0 | ||
@@ -3,0 +6,0 @@ |
@@ -57,7 +57,7 @@ /* global WeakMap */ | ||
resolve(schema = this.root) { | ||
resolve(schema = this.root, ancestors = []) { | ||
let resolvedSchema; | ||
let generatedId = this.walkedSchemas.get(schema); | ||
if (generatedId) { | ||
if (generatedId && ancestors.lastIndexOf(generatedId) > -1) { | ||
// resolve cyclic schema by using joi reference via generated unique ids | ||
@@ -74,10 +74,10 @@ return this.resolveLink(schema) | ||
} else if (schema.$ref) { | ||
resolvedSchema = this.resolve(this.resolveReference(schema.$ref)); | ||
resolvedSchema = this.resolve(this.resolveReference(schema.$ref), ancestors.concat(generatedId)); | ||
} else { | ||
const partialSchemas = []; | ||
if (schema.type) { | ||
partialSchemas.push(this.resolveType(schema)); | ||
partialSchemas.push(this.resolveType(schema, ancestors.concat(generatedId))); | ||
} else if (schema.properties) { | ||
// if no type is specified, just properties | ||
partialSchemas.push(this.object(schema)) | ||
partialSchemas.push(this.object(schema, ancestors.concat(generatedId))) | ||
} else if (schema.format) { | ||
@@ -91,12 +91,12 @@ // if no type is specified, just format | ||
if (schema.anyOf) { | ||
partialSchemas.push(this.resolveAnyOf(schema)); | ||
partialSchemas.push(this.resolveAnyOf(schema, ancestors.concat(generatedId))); | ||
} | ||
if (schema.allOf) { | ||
partialSchemas.push(this.resolveAllOf(schema)); | ||
partialSchemas.push(this.resolveAllOf(schema, ancestors.concat(generatedId))); | ||
} | ||
if (schema.oneOf) { | ||
partialSchemas.push(this.resolveOneOf(schema)); | ||
partialSchemas.push(this.resolveOneOf(schema, ancestors.concat(generatedId))); | ||
} | ||
if (schema.not) { | ||
partialSchemas.push(this.resolveNot(schema)); | ||
partialSchemas.push(this.resolveNot(schema, ancestors.concat(generatedId))); | ||
} | ||
@@ -122,3 +122,3 @@ if (partialSchemas.length === 0) { | ||
if (this.useDefaults && schema.default) { | ||
if (this.useDefaults && schema.default !== undefined) { | ||
resolvedSchema = resolvedSchema.default(schema.default) | ||
@@ -155,3 +155,3 @@ } | ||
resolveType(schema) { | ||
resolveType(schema, ancestors) { | ||
let joischema; | ||
@@ -174,3 +174,3 @@ | ||
case 'array': | ||
joischema = this.array(schema); | ||
joischema = this.array(schema, ancestors); | ||
break; | ||
@@ -185,3 +185,3 @@ case 'boolean': | ||
case 'object': | ||
joischema = this.object(schema); | ||
joischema = this.object(schema, ancestors); | ||
break; | ||
@@ -225,21 +225,21 @@ case 'string': | ||
resolveOneOf(schema) { | ||
resolveOneOf(schema, ancestors) { | ||
Hoek.assert(Util.isArray(schema.oneOf), 'Expected oneOf to be an array.'); | ||
return this.joi.alternatives(schema.oneOf.map(schema => this.resolve(schema))).match('one'); | ||
return this.joi.alternatives(schema.oneOf.map(schema => this.resolve(schema, ancestors))).match('one'); | ||
} | ||
resolveAnyOf(schema) { | ||
resolveAnyOf(schema, ancestors) { | ||
Hoek.assert(Util.isArray(schema.anyOf), 'Expected anyOf to be an array.'); | ||
return this.joi.alternatives(schema.anyOf.map(schema => this.resolve(schema))).match('any'); | ||
return this.joi.alternatives(schema.anyOf.map(schema => this.resolve(schema, ancestors))).match('any'); | ||
} | ||
resolveAllOf(schema) { | ||
resolveAllOf(schema, ancestors) { | ||
Hoek.assert(Util.isArray(schema.allOf), 'Expected allOf to be an array.'); | ||
return this.joi.alternatives(schema.allOf.map(schema => this.resolve(schema))).match('all'); | ||
return this.joi.alternatives(schema.allOf.map(schema => this.resolve(schema, ancestors))).match('all'); | ||
} | ||
resolveNot(schema) { | ||
resolveNot(schema, ancestors) { | ||
Hoek.assert(Util.isObject(schema.not), 'Expected Not to be an object.'); | ||
@@ -250,3 +250,3 @@ | ||
{ | ||
not: this.resolve(schema.not), | ||
not: this.resolve(schema.not, ancestors), | ||
then: this.joi.any(), | ||
@@ -262,3 +262,3 @@ otherwise: this.joi.any().forbidden() | ||
object(schema) { | ||
object(schema, ancestors) { | ||
@@ -275,3 +275,3 @@ const resolveproperties = () => { | ||
let joischema = this.resolve(property); | ||
let joischema = this.resolve(property, ancestors); | ||
@@ -291,3 +291,3 @@ if (schema.required && !!~schema.required.indexOf(key)) { | ||
if (Util.isObject(schema.additionalProperties)) { | ||
joischema = joischema.pattern(/^/, this.resolve(schema.additionalProperties)); | ||
joischema = joischema.pattern(/^/, this.resolve(schema.additionalProperties, ancestors)); | ||
} else { | ||
@@ -303,3 +303,3 @@ joischema = joischema.unknown(schema.additionalProperties !== false); | ||
array(schema) { | ||
array(schema, ancestors) { | ||
let joischema = this.joi.array(); | ||
@@ -311,6 +311,6 @@ let items; | ||
// found an array, thus its _per type_ | ||
return value.map((v) => this.resolve(v)); | ||
return value.map((v) => this.resolve(v, ancestors)); | ||
} | ||
// it's a single entity, so just resolve it normally | ||
return [this.resolve(value)]; | ||
return [this.resolve(value, ancestors)]; | ||
} | ||
@@ -317,0 +317,0 @@ |
{ | ||
"name": "enjoi", | ||
"version": "9.0.0", | ||
"version": "9.0.1", | ||
"license": "Apache 2.0", | ||
@@ -5,0 +5,0 @@ "description": "Converts json-schema to Joi schema.", |
78807