I am using spectral to define linting rules for my openapi specifications. I have one…
- User
I am using spectral to define linting rules for my openapi specifications. I have one rule that checks whether schema names contains technical text like Dto, Request, Response. For this my rule looks like this: ... name and definition of rule ... 'given': '$.components.schemas.~', 'then': { 'function': pattern, 'functionOptions': { 'notMatch': '.(DTO|Dto|Request|Response).*', }, } First can you explain the regex under "given" to me, what does it do?
Assistant
Sure! Let's break down the
givenpart of your rule:json'given': '$.components.schemas.*~'This is a JSONPath expression. JSONPath is a query language for JSON, similar to XPath for XML. It allows you to navigate through JSON data structures to select specific elements.
Here's what each part of the expression means:
$: This is the root of the JSON document..components: This selects thecomponentsobject within the root of the JSON document..schemas: This selects theschemasobject within thecomponentsobject..*: This selects all properties (schemas) within theschemasobject.~: This is a special character in JSONPath that indicates the value of each property.
So,
$.components.schemas.*~essentially means "select the values of all properties within the object of the object in the root of the JSON document."- User
Perfect thank you, now I need to adapt this JSON path in the given part to make it possible to skip this rule by adding 'x-skip-avoid-technical-names-check': true to the value of a defined schema if it is a false positive. If this is added to a openapi specification, the rule should not flag it as a violation, e.g.: FalsePositiveRequest : { 'x-skip-avoid-technical-names-check': true, type: 'object', description: 'desc', properties: { name: { type: 'string', description: 'test' } }, } should not produce a linting error. Here is an example how another linting rule was adapted to make it possible to mark false positives for examples checks: 'oas3-valid-media-example': { 'message': '{{error}} (mark false positives with "x-skip-example-check: true" property) [20] {non-breaking}', 'severity': 'error', 'given': ['..content..[?(@ && @.schema && (@.example !== void 0 || @.examples) && !(@[\'x-skip-example-check\']))]', '..headers..[?(@ && @.schema && (@.example !== void 0 || @.examples) && !(@['x-skip-example-check']))]', '$..parameters..[?(@ && @.schema && (@.example !== void 0 || @.examples) && !(@['x-skip-example-check']))]'], 'then': { 'function': oasexample, 'functionOptions': { 'schemaField': 'schema', 'oasVersion': 3, 'type': 'media', }, }, } Can you help me adapt my rule to also provide this functionality?
Assistant
Certainly! To adapt your rule to allow skipping the check by adding
x-skip-avoid-technical-names-check: trueto the schema, you need to modify thegivenpart of your rule so that it excludes schemas that have this property set totrue.Here's how you can adapt your rule:
json'given': '$.components.schemas.*~[?(@ && !(@[\'x-skip-avoid-technical-names-check\']))]', 'then': { 'function': 'pattern', 'functionOptions': { 'notMatch': '.*(DTO|Dto|Request|Response).*' } }Let's break down the modified
givenpart:$.components.schemas.*~: This part remains the same and selects the values of all properties within the object of the object in the root of the JSON document.
- User
Can you explain the new filter expression part in detail to me?
Assistant
Certainly! Let's break down the filter expression part of the JSONPath in detail:
json[?(@ && !(@['x-skip-avoid-technical-names-check']))]This filter expression is used to apply a condition to each item selected by the preceding JSONPath expression. Here's what each part of the filter expression means:
- User
I adapted the given part of my rule and added the following test to test the new functionality: test('Ignore schema names flagged with x-skip-avoid-technical-names-check [13]', async () => { openApi.components.schemas.FalsePositiveRequest = { 'x-skip-avoid-technical-names-check': true, type: 'object', description: 'desc', properties: { name: { type: 'string', description: 'test' } }, }; const result = await lint(openApi);
codeexpect(result).toEqual([]);}); However the linting result still contains the error and is not empty, what am I doing wrong?
Assistant