Skip to content

Commit

Permalink
feat(rulesets): use aliases in ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Jan 6, 2022
1 parent bba4b41 commit a2019f1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ testRule('oas2-parameter-description', [
],
},
},
parameters: {
limit: {
description: 'This is how it works.',
},
},
},
errors: [],
},
Expand Down
9 changes: 9 additions & 0 deletions packages/rulesets/src/oas/functions/oasExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ function* getMediaValidationItems(
}
}

function isSubschema(givenPath: JsonPath) {
// todo: add more
return givenPath.length > 1 && !['properties', 'items', 'contains'].includes(String(givenPath[givenPath.length - 1]));
}

function* getSchemaValidationItems(
fields: string[],
targetVal: Record<string, unknown>,
Expand All @@ -103,6 +108,10 @@ function* getSchemaValidationItems(
continue;
}

if (!isSubschema(givenPath)) {
continue;
}

yield {
value: targetVal[field],
path: [...givenPath, field],
Expand Down
90 changes: 62 additions & 28 deletions packages/rulesets/src/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,60 @@ const ruleset = {
formats: [oas2, oas3, oas3_0, oas3_1],
aliases: {
PathItem: '$.paths[*]',
ResponsesObject: {
targets: [
{ formats: [oas2], given: ['$.responses', '#OperationObject.responses'] },
{ formats: [oas3], given: ['$.components.responses', '#OperationObject.responses'] },
],
},
ResponseObject: '#ResponsesObject[*]', // [(@ && !@.$ref)]
ParametersDefinitionsObject: {
targets: [
{ formats: [oas2], given: ['$.parameters'] },
{ formats: [oas3], given: ['$.components.parameters'] },
],
},
ParametersObject: {
targets: [
{ formats: [oas2], given: ['#PathItem.parameters', '#OperationObject.parameters'] },
{
formats: [oas3],
given: ['#PathItem.parameters', '#OperationObject.parameters'],
},
],
},
ParameterObject: {
targets: [
{ formats: [oas2], given: ['#ParametersDefinitionsObject[*]', '#ParametersObject[*]'] }, // ?(@ && !@.$ref)
{
formats: [oas3],
given: ['#ParametersDefinitionsObject[*]', '#ParametersObject[*]'], // [?(@ && !@.$ref)]
},
],
},
EncodingObject: '#MediaTypeObject.encoding',
MediaTypeObject: ['#RequestBodyObject.content', '#ParameterObject.content', '#ResponseObject.content'],
HeadersObject: {
targets: [
{ formats: [oas2], given: ['#ResponseObject.headers'] },
{
formats: [oas3],
given: ['$.components.headers', '#EncodingObject.headers', '#ResponseObject.headers'],
},
],
},
HeaderObject: {
targets: [
{ formats: [oas2], given: ['#ResponseObject.headers[*]'] },
{
formats: [oas3],
given: ['#HeadersObject[*]'], // ?(@ && !$.ref)
},
],
},
RequestBodyObject: ['#OperationObject.requestBody', '$.components.requestBodies[*]'],
OperationObject: '#PathItem[get,put,post,delete,options,head,patch,trace]',
SchemaObject: ['#MediaTypeObject.schema', '#ParameterObject.schema', '$.components.schemas[*]'],
},
rules: {
'operation-success-response': {
Expand Down Expand Up @@ -441,7 +494,7 @@ const ruleset = {
description: 'Parameter objects must have "description".',
recommended: false,
formats: [oas2],
given: '$..parameters[?(@ && @.in)]',
given: '#ParameterObject',
type: 'style',
then: {
field: 'description',
Expand All @@ -468,11 +521,7 @@ const ruleset = {
formats: [oas2],
severity: 0,
type: 'validation',
given: [
"$..definitions..[?(@property !== 'properties' && @ && (@.example !== void 0 || @['x-example'] !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && @ && (@.example !== void 0 || @['x-example'] !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..responses..[?(@property !== 'properties' && @ && (@.example !== void 0 || @['x-example'] !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
],
given: ['#SchemaObject..[?(@ && @.example)]'],
then: {
function: oasExample,
functionOptions: {
Expand All @@ -489,7 +538,7 @@ const ruleset = {
formats: [oas2],
severity: 0,
type: 'validation',
given: '$..responses..[?(@ && @.schema && @.examples)]',
given: '#MediaTypeObject',
then: {
function: oasExample,
functionOptions: {
Expand Down Expand Up @@ -575,11 +624,9 @@ const ruleset = {
type: 'style',
given: [
'$.components.examples[*]',
'$.paths[*][*]..content[*].examples[*]',
'$.paths[*][*]..parameters[*].examples[*]',
'$.components.parameters[*].examples[*]',
'$.paths[*][*]..headers[*].examples[*]',
'$.components.headers[*].examples[*]',
'#MediaTypeObject.examples[*]',
'#ParameterObject.examples[*]',
'#HeaderObject.examples[*]',
],
then: {
function: xor,
Expand Down Expand Up @@ -607,11 +654,7 @@ const ruleset = {
recommended: false,
formats: [oas3],
type: 'style',
given: [
'#PathItem.parameters[?(@ && @.in)]',
'#OperationObject.parameters[?(@ && @.in)]',
'$.components.parameters[?(@ && @.in)]',
],
given: '#ParameterObject',
then: {
field: 'description',
function: truthy,
Expand Down Expand Up @@ -650,11 +693,7 @@ const ruleset = {
severity: 0,
formats: [oas3],
type: 'validation',
given: [
'$..content..[?(@ && @.schema && (@.example !== void 0 || @.examples))]',
'$..headers..[?(@ && @.schema && (@.example !== void 0 || @.examples))]',
'$..parameters..[?(@ && @.schema && (@.example !== void 0 || @.examples))]',
],
given: '#MediaTypeObject',
then: {
function: oasExample,
functionOptions: {
Expand All @@ -671,12 +710,7 @@ const ruleset = {
formats: [oas3],
recommended: true,
type: 'validation',
given: [
"$.components.schemas..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..content..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..headers..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
"$..parameters..[?(@property !== 'properties' && @ && (@ && @.example !== void 0 || @.default !== void 0) && (@.enum || @.type || @.format || @.$ref || @.properties || @.items))]",
],
given: ['#SchemaObject', '#SchemaObject..[?(@ && @.example)]'],
then: {
function: oasExample,
functionOptions: {
Expand Down

0 comments on commit a2019f1

Please sign in to comment.