Question: custom rules with assertions syntax
See original GitHub issueI’m writing a custom rule based on the example response-contains-property
I’m using OAS 3.0.3, my response is using a $ref
to an external JSON Schema Draft-04. In order for this rule to work, do I need to dereference the definition prior to linting? It seems to be returning from the schema.type !== 'object'
condition and doesn’t report any error when using the lint
command.
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"$ref": "./schemas/associate-activity-logs-response-schema_v01.json"
},
/** @type { import('@redocly/cli').oasRule} */
const nextGenResponseModel = options => {
const mustExist = options.mustExist || []
return {
Response: {
skip: (_response, key) => {
return ![200, 201, 202].includes(key.toString())
},
MediaType: {
skip: (_media, key) => {
return key !== 'application/json'
},
Schema(schema, { report, location }) {
if (schema.type !== 'object') return
for (let element of mustExist) {
if (!schema.properties[element]) {
report ({
message: `Next-Gen Response must have top-level "${element}"`,
location: location.child('properties')
})
}
}
}
}
}
}
}
module.exports = nextGenResponseModel
custom-id/nextGenResponseModel:
description: Ensure the Response model follows the API Style Guide
message: {{ error }}
severity: error
mustExist:
- _confirmMessage
- _meta
plugins:
- './plugins/index.js'
Issue Analytics
- State:
- Created a year ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
Custom rules and assertions - Redocly
A custom rule is a rule that starts with a assert/ prefix followed by a unique rule name. Assertion names display in the...
Read more >Assertions in Java - GeeksforGeeks
An assertion allows testing the correctness of any assumptions that have been made in the program. An assertion is achieved using the assert...
Read more >Best practice for using assert? - python - Stack Overflow
Asserts self-document code assumptions for what is true at the current execution time. It's an assumption comment, which gets checked. ...
Read more >Define custom assertions | Developer Guide - Nightwatch.js
Define custom assertions. Overview. Nightwatch allows you to even define your own assertions, extending the available .assert and .verify namespaces.
Read more >Assertion Styles - Chai
This section of the guide introduces you to the three different assertion styles that you may use in your testing environment.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
if i have a list of required properties, can I expect the error to output which property was not found?
message: "_confirmMessage" was not found
Side note: we have a built in rule: https://redocly.com/docs/cli/rules/response-contains-property/
Having template placeholders to available to use in messages would be good.