-
Notifications
You must be signed in to change notification settings - Fork 14
feat(ipa): new IPA rule xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
4436ed7
e72e2d5
fcf03ca
2851d73
0bc337f
68f718b
6c473ea
e048cf3
d23d11c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import testRule from './__helpers__/testRule.js'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
testRule('xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', [ | ||
{ | ||
name: 'valid schemas', | ||
document: { | ||
components: { | ||
schemas: { | ||
SchemaOneOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
oneOf: [{}], | ||
}, | ||
SchemaAnuOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
anyOf: [{}], | ||
}, | ||
SchemaAllOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
allOf: [{}], | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid schemas', | ||
document: { | ||
components: { | ||
schemas: { | ||
Schema: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
properties: { | ||
type: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
NestedSchema: { | ||
properties: { | ||
name: { | ||
type: 'object', | ||
discriminator: { | ||
propertyName: 'first', | ||
}, | ||
properties: { | ||
first: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
address: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', | ||
message: "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property.", | ||
path: ['components', 'schemas', 'Schema', 'discriminator'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', | ||
message: "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property.", | ||
path: ['components', 'schemas', 'NestedSchema', 'properties', 'name', 'discriminator'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'invalid schemas with exceptions', | ||
document: { | ||
components: { | ||
schemas: { | ||
Schema: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
properties: { | ||
type: { | ||
type: 'string', | ||
}, | ||
}, | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf': 'reason', | ||
}, | ||
}, | ||
NestedSchema: { | ||
properties: { | ||
name: { | ||
type: 'object', | ||
discriminator: { | ||
propertyName: 'first', | ||
}, | ||
properties: { | ||
first: { | ||
type: 'string', | ||
}, | ||
}, | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf': 'reason', | ||
}, | ||
}, | ||
address: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js'; | ||
import { resolveObject } from './utils/componentUtils.js'; | ||
|
||
const ERROR_MESSAGE = "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property."; | ||
|
||
export default (input, _, { path, documentInventory, rule }) => { | ||
const siblings = resolveObject(documentInventory.resolved, path.slice(0, path.length - 1)); | ||
|
||
const errors = checkViolationsAndReturnErrors(input, path, Object.keys(siblings)); | ||
return evaluateAndCollectAdoptionStatus(errors, rule.name, siblings, path); | ||
}; | ||
|
||
function checkViolationsAndReturnErrors(input, path, siblingKeys) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which Swagger component annotation level are we able to define exceptions? Is it Schema? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the schema. The exceptions are siblings to the discriminator since discriminators cannot have extensions. Now the metric path is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
if (!siblingKeys.includes('oneOf') && !siblingKeys.includes('anyOf') && !siblingKeys.includes('allOf')) { | ||
return [ | ||
{ | ||
path, | ||
message: ERROR_MESSAGE, | ||
}, | ||
]; | ||
} | ||
return []; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] SchemaAnyOf