openapi-enforcer
Advanced tools
Comparing version 1.10.0 to 1.10.1
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.10.0", | ||
"version": "1.10.1", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,5 +18,5 @@ /** | ||
'use strict'; | ||
const Exception = require('../exception'); | ||
const util = require('../util'); | ||
const Value = require('./value'); | ||
const schemaUtil = require('./util'); | ||
const util = require('../util'); | ||
const Value = require('./value'); | ||
@@ -68,45 +68,4 @@ const rxTrue = /^\s*true\s*$/i; | ||
} else { | ||
const key = schema.anyOf ? 'anyOf' : 'oneOf'; | ||
const exceptions = []; | ||
const matches = []; | ||
schema[key].forEach(subSchema => { | ||
const childException = new Exception(''); | ||
const mapCopy = new Map(map); | ||
const result = runDeserialize(childException, mapCopy, subSchema, originalValue, options); | ||
if (childException.hasException) { | ||
exceptions.push(childException) | ||
} else { | ||
let score = 1; | ||
if (subSchema.type === 'object') { | ||
const properties = subSchema.properties || {}; | ||
const keys = Object.keys(value); | ||
const length = keys.length; | ||
for (let i = 0; i < length; i++) { | ||
const key = keys[i]; | ||
if (properties.hasOwnProperty(key)) { | ||
score++; | ||
} else if (properties.additionalProperties === false) { | ||
score = 0; | ||
break; | ||
} | ||
} | ||
} | ||
if (score > 0) matches.push({ score, result }) | ||
} | ||
}); | ||
if (matches.length > 1) { | ||
matches.sort((a, b) => a.score > b.score ? -1 : 1); | ||
const highScore = matches[0].score; | ||
const highs = matches.filter(match => match.score === highScore); | ||
if (highs.length > 1) { | ||
exception.message('Unable to determine deserialization schema because too many schemas match. Use of a discriminator or making your schemas more specific would help this problem.') | ||
} else { | ||
result = util.merge(value, highs[0].result); | ||
} | ||
} else if (matches.length === 0) { | ||
const child = exception.nest('No matching schemas'); | ||
exceptions.forEach(childException => child.push(childException)); | ||
} else { | ||
result = util.merge(value, matches[0].result); | ||
} | ||
result = schemaUtil.anyOneOf(schema, | ||
originalValue, exception, map, runDeserialize, false, options); | ||
} | ||
@@ -113,0 +72,0 @@ return result; |
@@ -18,5 +18,5 @@ /** | ||
'use strict'; | ||
const Exception = require('../exception'); | ||
const util = require('../util'); | ||
const Value = require('./value'); | ||
const schemaUtil = require('./util'); | ||
const util = require('../util'); | ||
const Value = require('./value'); | ||
@@ -57,45 +57,3 @@ module.exports = runSerialize; | ||
} else { | ||
const key = schema.anyOf ? 'anyOf' : 'oneOf'; | ||
const exceptions = []; | ||
const matches = []; | ||
schema[key].forEach(subSchema => { | ||
const childException = new Exception(''); | ||
const mapCopy = new Map(map); | ||
const result = runSerialize(childException, mapCopy, subSchema, originalValue); | ||
if (childException.hasException) { | ||
exceptions.push(childException) | ||
} else { | ||
let score = 1; | ||
if (subSchema.type === 'object') { | ||
const properties = subSchema.properties || {}; | ||
const keys = Object.keys(value); | ||
const length = keys.length; | ||
for (let i = 0; i < length; i++) { | ||
const key = keys[i]; | ||
if (properties.hasOwnProperty(key)) { | ||
score++; | ||
} else if (properties.additionalProperties === false) { | ||
score = 0; | ||
break; | ||
} | ||
} | ||
} | ||
if (score > 0) matches.push({ score, result }) | ||
} | ||
}); | ||
if (matches.length > 1) { | ||
matches.sort((a, b) => a.score > b.score ? -1 : 1); | ||
const highScore = matches[0].score; | ||
const highs = matches.filter(match => match.score === highScore); | ||
if (highs.length > 1) { | ||
exception.message('Unable to determine serialization schema because too many schemas match. Use of a discriminator or making your schemas more specific would help this problem.') | ||
} else { | ||
return util.merge(value, highs[0].result); | ||
} | ||
} else if (matches.length === 0) { | ||
const child = exception.nest('No matching schemas'); | ||
exceptions.forEach(childException => child.push(childException)); | ||
} else { | ||
return util.merge(value, matches[0].result); | ||
} | ||
return schemaUtil.anyOneOf(schema, originalValue, exception, map, runSerialize, true); | ||
} | ||
@@ -102,0 +60,0 @@ return value; |
@@ -19,4 +19,52 @@ const expect = require('chai').expect; | ||
expect(warning).to.equal(undefined) | ||
}); | ||
describe('issue 69 - oneOf additionalProperties = false', () => { | ||
const def = { | ||
oneOf: [ | ||
{ | ||
properties: { | ||
one: { type: "string" } | ||
}, | ||
required: [ "one" ], | ||
additionalProperties: false, | ||
type: "object" | ||
}, | ||
{ | ||
properties: { | ||
one: { type: "string" }, | ||
two: { type: "string" } | ||
}, | ||
required: [ "one", "two" ], | ||
additionalProperties: false, | ||
type: "object" | ||
} | ||
] | ||
}; | ||
let schema; | ||
before(() => { | ||
const result = new Enforcer.v3_0.Schema(def); | ||
schema = result.value; | ||
}); | ||
it('can identify schema with one value', () => { | ||
const [ value, err, warn ] = schema.deserialize({ one: "value" }); | ||
expect(err).to.equal(undefined); | ||
expect(value).to.deep.equal({ one: 'value' }); | ||
}); | ||
it('can identify schema with two values', () => { | ||
const [ value, err, warn ] = schema.deserialize({ one: 'one', two: 'two' }); | ||
expect(err).to.equal(undefined); | ||
expect(value).to.deep.equal({ one: 'one', two: 'two' }); | ||
}); | ||
it('can identify invalid input', () => { | ||
const [ value, err, warn ] = schema.deserialize({ one: 1 }); | ||
expect(err).to.match(/Expected a string/); | ||
}); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19758
1453437