Skip to content

Commit

Permalink
fix: make sps-ref-property-name more specific (#81)
Browse files Browse the repository at this point in the history
We specifically look to see if there is a "type" field under the ref
before we check format
  • Loading branch information
brandonsahadeo authored Mar 14, 2024
1 parent 9cda42d commit 0982e0b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
23 changes: 11 additions & 12 deletions rulesets/src/naming.ruleset.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
rules:

rules:
# domain references (URN-like values)
sps-ref-property-name:
description: Property with the name 'ref' MUST be of type 'sps-ref' following URN-like reference formats.
severity: error
formats: [oas3]
given: '$..properties..[?((@property=== "ref" || @property === "Ref") && @.$ref == null && @.allOf == null && @.oneOf == null)]'
given: '$..properties..[?((@property=== "ref" || @property === "Ref") && @.$ref == null && @.allOf == null && @.oneOf == null && @.type != null)]'
resolved: false
then:
- field: "format"
function: truthy
- field: "format"
function: pattern
functionOptions:
match: 'sps-ref'
match: "sps-ref"

sps-ref-schema:
description: Properties following 'sps-ref' format MUST use the standardized schema - maxLength (255), minLength(7), pattern (includes 'sps'), type (string).
message: '{{property}} is not provided or not following required schema values.'
message: "{{property}} is not provided or not following required schema values."
severity: error
formats: [oas3]
given: '$..[?(@property=== "format" && @ == "sps-ref")]^'
then:
- function: schema
functionOptions:
functionOptions:
schema:
type: object
required:
Expand All @@ -47,23 +46,23 @@ rules:
- field: pattern
function: pattern
functionOptions:
match: 'sps'
match: "sps"
- field: type
function: pattern
functionOptions:
match: '^string$'
match: "^string$"

# fingerprint property usage and naming
sps-fingerprint-naming:
description: Rather than property names refering to the implementation for 'hash' or 'hashkey', you MUST use the property name 'fingerprint'.
message: '{{property}} is not using property name fingerprint.'
message: "{{property}} is not using property name fingerprint."
severity: error
formats: [oas3]
given: '$.components.schemas..properties.*~'
given: "$.components.schemas..properties.*~"
then:
function: pattern
functionOptions:
notMatch: '^hashkey|hashKey|hash$'
notMatch: "^hashkey|hashKey|hash$"

sps-fingerprint-type:
description: Fingerprint values MUST use a data type of `string`.
Expand All @@ -73,4 +72,4 @@ rules:
then:
function: pattern
functionOptions:
match: '^string$'
match: "^string$"
37 changes: 36 additions & 1 deletion rulesets/test/naming/sps-ref-property-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("sps-ref-property-name", () => {
ref: myrefvalue
name: hello
`;

await spectral.validateSuccess(spec, ruleName);
});

Expand Down Expand Up @@ -65,4 +65,39 @@ describe("sps-ref-property-name", () => {
`;
await spectral.validateFailure(spec, ruleName, "Error");
});

test("Ref property refers to readOnly value, and should pass", async () => {
const spec = `
openapi: 3.0.1
paths:
/v1/test:
post:
summary: Create a new Rule
requestBody:
required: true
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Rule'
- properties:
ref:
readOnly: true
priority:
properties:
id:
readOnly: false
name:
readOnly: true
type:
properties:
id:
readOnly: false
name:
readOnly: true
ref:
readOnly: true
`;
await spectral.validateSuccess(spec, ruleName);
});
});

0 comments on commit 0982e0b

Please sign in to comment.