openapi-enforcer
Advanced tools
Comparing version 1.21.0 to 1.21.1
@@ -7,2 +7,18 @@ # Change Log | ||
## 1.21.1 | ||
### Fixed | ||
- **Default Values Can Be Specified with allOf and anyOf Schemas** | ||
Schemas that use the `allOf` or `anyOf` property can now specify a default value at the shared schema level. For example: | ||
```yaml | ||
MySchema: | ||
default: false | ||
anyOf: | ||
- type: boolean | ||
- type: number | ||
``` | ||
## 1.21.0 | ||
@@ -9,0 +25,0 @@ |
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.21.0", | ||
"version": "1.21.1", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,3 @@ /** | ||
}, | ||
determineSchemaFromSchemas, | ||
edgeSlashes, | ||
@@ -124,2 +125,33 @@ findMediaMatch, | ||
// given multiple possible schemas (anyOf or oneOf) find the correct schema based on the provided value | ||
function determineSchemaFromSchemas(schemas, value) { | ||
const type = typeof value; | ||
const length = schemas.length; | ||
for (let i = 0; i < length; i++) { | ||
const schema = schemas[i]; | ||
if (schema.type === 'array' && Array.isArray(value)) return schema; | ||
if (schema.type === 'boolean' && type === 'boolean') return schema; | ||
if (schema.type === 'integer' && type === 'number' && /^\d+$/.test(String(value))) return schema; | ||
if (schema.type === 'number' && type === 'number') return schema; | ||
if (schema.type === 'string' && type === 'string') return schema; | ||
if (schema.type === 'object' && type === 'object' && value !== null) return schema; | ||
if (value === null && (schema.nullable || schema['x-nullable'])) return schema; | ||
if (schema.anyOf) { | ||
const schema = determineSchemaFromSchemas(schema.anyOf, value) | ||
if (schema !== null) return schema; | ||
} else if (schema.oneOf) { | ||
const schema = determineSchemaFromSchemas(schema.oneOf, value) | ||
if (schema !== null) return schema; | ||
} else if (schema.allOf) { | ||
const length = schema.allOf.length; | ||
for (let j = 0; j < length; j++) { | ||
const schema = schema.allOf[j]; | ||
if (schema.type !== undefined) return schema; | ||
if (schema.anyOf || schema.oneOf) return determineSchemaFromSchemas(schema.anyOf ?? schema.oneOf, value); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
function edgeSlashes (value, start, end) { | ||
@@ -126,0 +158,0 @@ value = value.replace(/^\//, '').replace(/\/$/, ''); |
@@ -277,2 +277,12 @@ const expect = require('chai').expect; | ||
describe('issue-145 - default with multiple types', () => { | ||
it('can have a default at the top and anyof subtypes', async () => { | ||
const [ value, err, warn ] = await Enforcer(path.resolve(resourcesPath, 'issue-145/openapi.yml'), { | ||
fullResult: true, | ||
componentOptions: {} | ||
}); | ||
expect(err).to.equal(undefined); | ||
}) | ||
}) | ||
}); |
Sorry, the diff of this file is too big to display
1047346
20626