-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update x-sdk-operations schema and move it to a file
- Loading branch information
Barrett Schonefeld
committed
Apr 1, 2021
1 parent
6a49b38
commit 94bc958
Showing
6 changed files
with
175 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const AJV = require('ajv'); | ||
const ajv = new AJV({ allErrors: true, jsonPointers: true }); | ||
const { STATIC_ASSETS } = require('@stoplight/spectral/dist/assets'); | ||
|
||
// performs JSON Schema validation on the given object | ||
// expects `schemaKey` options with the key to retrieve the schema | ||
module.exports = function(obj, opts, paths) { | ||
const errors = []; | ||
const rootPath = paths.target !== void 0 ? paths.target : paths.given; | ||
const jsonSchema = JSON.parse(STATIC_ASSETS[opts.schemaKey]); | ||
const validate = ajv.compile(jsonSchema); | ||
if (!ajv.validate(jsonSchema, obj)) { | ||
return formatAJVErrors(validate.errors, rootPath); | ||
} | ||
return errors; | ||
}; | ||
|
||
function formatAJVErrors(errors, rootPath) { | ||
const errorList = []; | ||
errors.forEach(function(err) { | ||
errorList.push({ | ||
message: err.message, | ||
path: getErrPath(err, rootPath) | ||
}); | ||
}); | ||
return errorList; | ||
} | ||
|
||
function getErrPath(err, rootPath) { | ||
const relativePath = err.dataPath.split('/'); | ||
if (relativePath.length > 1) { | ||
const strippedPath = relativePath.splice( | ||
relativePath.indexOf('') + 1, | ||
relativePath.length | ||
); | ||
return [...rootPath, ...strippedPath]; | ||
} | ||
return rootPath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "IBM SDK Operations Extension", | ||
"description": "sdk operations extension schema", | ||
"type": "object", | ||
"properties": { | ||
"paths": { | ||
"$ref": "#/definitions/Paths" | ||
} | ||
}, | ||
"definitions": { | ||
"Paths": { | ||
"type": "object", | ||
"patternProperties": { | ||
".*": { | ||
"$ref": "#/definitions/PathItem" | ||
} | ||
} | ||
}, | ||
"PathItem": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^(get|put|post|delete|options|head|patch|trace)$": { | ||
"$ref": "#/definitions/Operation" | ||
} | ||
} | ||
}, | ||
"Operation": { | ||
"type": "object", | ||
"properties": { | ||
"x-sdk-operations": { | ||
"$ref": "#/definitions/SdkOperations" | ||
} | ||
} | ||
}, | ||
"SdkOperations": { | ||
"type": "object", | ||
"properties": { | ||
"request-examples": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/definitions/x-sdk-request-examples-array" | ||
} | ||
} | ||
}, | ||
"additionalProperties": false | ||
}, | ||
"x-sdk-request-examples-array": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/x-sdk-request-example" | ||
} | ||
}, | ||
"x-sdk-request-example": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name or title of the example. In documentation it should appear as a header above the example." | ||
}, | ||
"contentType": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"description": "The media type of the response in this example." | ||
} | ||
}, | ||
"example": { | ||
"type": "array", | ||
"description": "An array of code or text elements that make up the example", | ||
"items": { | ||
"$ref": "#/definitions/x-sdk-request-example-element" | ||
} | ||
}, | ||
"response": {}, | ||
"description": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"example" | ||
], | ||
"additionalProperties": false | ||
}, | ||
"x-sdk-request-example-element": { | ||
"type": "object", | ||
"description": "An element of the request example.", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name or title of the example element. In documentation it should appear as a header above the example element." | ||
}, | ||
"type": { | ||
"type": "string", | ||
"description": "The element type indicates the type of content in the element. `text` elements contain a textual description or explanation, possibly using markdown for rich text elements. `code` elements contain code appropriate for the language of the request example. `code` elements will be presented in a `<pre><code>` block in documentation and should contain no markup or escapes other than escapes for quote and backslash (required for JSON).", | ||
"enum": [ | ||
"text", | ||
"code" | ||
] | ||
}, | ||
"source": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "Content of the example element as an array of strings. The content is formed by simple concatenation of the array elements." | ||
} | ||
}, | ||
"required": [ | ||
"type", | ||
"source" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters