Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2016, Mermade Software
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+37
View File
@@ -0,0 +1,37 @@
# OAS-Schema-Walker
## Functions
<dl>
<dt><a href="#getDefaultState">getDefaultState()</a> ⇒</dt>
<dd><p>obtains the default starting state for the <code>state</code> object used
by walkSchema</p>
</dd>
<dt><a href="#walkSchema">walkSchema(parent, state, callback,)</a> ⇒</dt>
<dd><p>begins the walk of a schema object, or the <code>state</code> object used
by walkSchema</p>
</dd>
</dl>
<a name="getDefaultState"></a>
## getDefaultState() ⇒
obtains the default starting state for the `state` object used
by walkSchema
**Kind**: global function
**Returns**: the state object suitable for use in walkSchema
<a name="walkSchema"></a>
## walkSchema(parent, state, callback,) ⇒
begins the walk of a schema object, or the `state` object used
by walkSchema
**Kind**: global function
**Returns**: the schema object
| Param | Description |
| --- | --- |
| parent | the parent schema, if any. Use empty object if none |
| state | the initial starting state of the walker, usually obtained from `getDefaultState` |
| callback, | a function taking a schema, parent and state to be called on this and all subschemas |
+125
View File
@@ -0,0 +1,125 @@
'use strict';
/**
* functions to walk an OpenAPI schema object and traverse all subschemas
* calling a callback function on each one
*/
/**
* obtains the default starting state for the `state` object used
* by walkSchema
* @return the state object suitable for use in walkSchema
*/
function getDefaultState() {
return { depth: 0, seen: new WeakMap(), top: true, combine: false, allowRefSiblings: false };
}
/**
* begins the walk of a schema object
* @param schema the schema object to walk
* @param parent the parent schema, if any. Use empty object if none
* @param state the initial starting state of the walker, usually obtained from `getDefaultState`. Use empty object to auto-initialise
* @param callback a function taking a schema, parent and state to be called on this and all subschemas
* @return the schema object
*/
function walkSchema(schema, parent, state, callback) {
if (typeof state.depth === 'undefined') state = getDefaultState();
if ((schema === null) || (typeof schema === 'undefined')) return schema;
if (typeof schema.$ref !== 'undefined') {
let temp = {$ref:schema.$ref};
if (state.allowRefSiblings && schema.description) {
temp.description = schema.description;
}
callback(temp,parent,state);
return temp; // all other properties SHALL be ignored
}
if (state.combine) {
if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length === 1) {
schema = Object.assign({},schema.allOf[0],schema);
delete schema.allOf;
}
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length === 1) {
schema = Object.assign({},schema.anyOf[0],schema);
delete schema.anyOf;
}
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length === 1) {
schema = Object.assign({},schema.oneOf[0],schema);
delete schema.oneOf;
}
}
callback(schema,parent,state);
if (state.seen.has(schema)) {
return schema;
}
//else
if ((typeof schema === 'object') && (schema !== null)) state.seen.set(schema,true);
state.top = false;
state.depth++;
if (typeof schema.items !== 'undefined') {
state.property = 'items';
walkSchema(schema.items,schema,state,callback);
}
if (schema.additionalItems) {
if (typeof schema.additionalItems === 'object') {
state.property = 'additionalItems';
walkSchema(schema.additionalItems,schema,state,callback);
}
}
if (schema.additionalProperties) {
if (typeof schema.additionalProperties === 'object') {
state.property = 'additionalProperties';
walkSchema(schema.additionalProperties,schema,state,callback);
}
}
if (schema.properties) {
for (let prop in schema.properties) {
let subSchema = schema.properties[prop];
state.property = 'properties/'+prop;
walkSchema(subSchema,schema,state,callback);
}
}
if (schema.patternProperties) {
for (let prop in schema.patternProperties) {
let subSchema = schema.patternProperties[prop];
state.property = 'patternProperties/'+prop;
walkSchema(subSchema,schema,state,callback);
}
}
if (schema.allOf) {
for (let index in schema.allOf) {
let subSchema = schema.allOf[index];
state.property = 'allOf/'+index;
walkSchema(subSchema,schema,state,callback);
}
}
if (schema.anyOf) {
for (let index in schema.anyOf) {
let subSchema = schema.anyOf[index];
state.property = 'anyOf/'+index;
walkSchema(subSchema,schema,state,callback);
}
}
if (schema.oneOf) {
for (let index in schema.oneOf) {
let subSchema = schema.oneOf[index];
state.property = 'oneOf/'+index;
walkSchema(subSchema,schema,state,callback);
}
}
if (schema.not) {
state.property = 'not';
walkSchema(schema.not,schema,state,callback);
}
state.depth--;
return schema;
}
module.exports = {
getDefaultState: getDefaultState,
walkSchema: walkSchema
};
+27
View File
@@ -0,0 +1,27 @@
{
"name": "oas-schema-walker",
"version": "1.1.5",
"description": "Library to walk OAS 3 schema objects and call a callback",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"funding": "https://github.com/Mermade/oas-kit?sponsor=1",
"keywords": [
"openapi",
"swagger",
"oas",
"schema",
"json-schema"
],
"author": "Mike Ralphson",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "https://github.com/Mermade/oas-kit.git"
},
"bugs": {
"url": "https://github.com/mermade/oas-kit/issues"
},
"gitHead": "dc53888b6184e0896ac878a39fec92f835fd44a3"
}