question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: custom rules with assertions syntax

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
jeremyfielcommented, Aug 22, 2022

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

1reaction
adamaltmancommented, Aug 22, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found