openapi-enforcer
Advanced tools
Comparing version 1.1.11 to 1.2.0
56
index.js
@@ -85,2 +85,25 @@ /** | ||
/** | ||
* Convert a non plain object to a plain object. | ||
* @param value The value to convert. | ||
* @param {object} [options] | ||
* @param {boolean} [options.allowInheritedProperties=false] | ||
* @param {array} [options.preserve=[]] | ||
* @returns {object} The plain object. | ||
*/ | ||
Enforcer.toPlainObject = function (value, options) { | ||
const map = new Map(); | ||
if (!options) options = {}; | ||
if (typeof options !== 'object') throw Error('Parameter "options" must be an object'); | ||
if (!options.hasOwnProperty('allowInheritedProperties')) options.allowInheritedProperties = false; | ||
if (!options.hasOwnProperty('preserve')) options.preserve = []; | ||
if (!Array.isArray(options.preserve)) throw Error('Option "preserve" must be an array'); | ||
options.preserve.push(Date); | ||
const result = toPlainObject(value, options, map); | ||
if (!result.set) throw Error('Unable to convert value to plain object'); | ||
return result.value; | ||
}; | ||
const v2_0 = Enforcer.v2_0 = {}; | ||
@@ -167,1 +190,34 @@ Object.defineProperty(v2_0, 'version', { value: '2.0' }); | ||
Enforcer.v3_0.Schema.defineDataTypeFormat('string', 'date-time', dataTypeFormats.dateTime); | ||
function toPlainObject (value, options, map) { | ||
if (value && value.constructor && options.preserve.indexOf(value.constructor) !== -1) { | ||
return { set: true, value }; | ||
} else if (Array.isArray(value)) { | ||
if (map.has(value)) return map.get(value); | ||
const result = []; | ||
map.set(value, result); | ||
value.forEach(v => { | ||
const r = toPlainObject(v, options, map); | ||
if (r.set) result.push(r.value); | ||
}); | ||
return { set: true, value: result }; | ||
} else if (value && typeof value === 'object') { | ||
if (map.has(value)) return map.get(value); | ||
const result = {}; | ||
map.set(value, result); | ||
for (let k in value) { | ||
if (options.allowInheritedProperties || value.hasOwnProperty(k)) { | ||
const r = toPlainObject(value[k], options, map); | ||
if (r.set) result[k] = r.value; | ||
} | ||
} | ||
return { set: true, value: result }; | ||
} else if (value instanceof Object) { | ||
return { set: false }; | ||
} else { | ||
return { set: true, value }; | ||
} | ||
} |
{ | ||
"name": "openapi-enforcer", | ||
"version": "1.1.11", | ||
"version": "1.2.0", | ||
"description": "Library for validating, parsing, and formatting data against open api schemas.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -380,3 +380,3 @@ /** | ||
type: 'string', | ||
errors: ({ exception, key, parent, refParser, result }) => { | ||
errors: ({ exception, parent, refParser, result }) => { | ||
if (refParser) { | ||
@@ -433,3 +433,3 @@ let schema; | ||
const types = [ def.type ]; | ||
if (def.nullable === true || def['x-nullable'] === true) types.push('null') | ||
if (def.nullable === true || def['x-nullable'] === true) types.push('null'); | ||
return types; | ||
@@ -451,3 +451,3 @@ }, | ||
type: 'string', | ||
errors: ({ exception, parent, warn }) => { | ||
errors: ({ parent, warn }) => { | ||
const format = parent.definition.format; | ||
@@ -632,3 +632,3 @@ if (format) { | ||
let offset = 0; | ||
while (match = rx.exec(value)) { | ||
while ((match = rx.exec(value))) { | ||
const property = match[1]; | ||
@@ -698,2 +698,2 @@ result += value.substring(offset, match.index) + (data[property] !== undefined ? data[property] : match[0]); | ||
}); | ||
} | ||
} |
35
temp.js
@@ -1,13 +0,24 @@ | ||
const Enforcer = require('./index') | ||
const path = require('path') | ||
const OpenAPI = require('./index').v3_0.OpenApi | ||
const openapi = OpenAPI({ | ||
openapi: '3.0.0', | ||
info: { title: '', version: '' }, | ||
paths: { | ||
'/{date}': { | ||
get: { | ||
parameters: [ | ||
{ | ||
name: 'date', | ||
in: 'path', | ||
required: true, | ||
schema: { | ||
type: 'string', | ||
format: 'date' | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}) | ||
Enforcer(path.resolve(__dirname, 'matthew.yml')) | ||
.catch(err => { | ||
console.error(err) | ||
// Error: One or more errors exist in the OpenApi definition | ||
// at: paths | ||
// at: /person/{id} | ||
// Path parameter definitions inconsistent | ||
// at: get | ||
// Definition missing path parameters: id | ||
}) | ||
console.log(openapi) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
1477913
161
16264