Skip to content

Commit

Permalink
Merge pull request #239 from acunniffe/fix/snake-case
Browse files Browse the repository at this point in the history
allow numeric components in snake case
  • Loading branch information
cmars authored Apr 19, 2022
2 parents 4bfc878 + a38f18e commit a8f903a
Show file tree
Hide file tree
Showing 3 changed files with 433 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rulesets/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const rules = {
propertyKey: ({ bodyProperties }: SnykApiCheckDsl) => {
bodyProperties.added.must("have snake case keys", ({ key }, context) => {
// TODO: did not find a doc link for this
const snakeCase = /^[a-z]+(?:_[a-z]+)*$/g;
const snakeCase = /^[a-z]+(?:_[a-z\d]+)*$/g;
if (!snakeCase.test(key))
expect.fail(
`expected ${getBodyPropertyName(context)} is be snake case`,
Expand Down
381 changes: 381 additions & 0 deletions src/rulesets/tests/__snapshots__/properties.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,387 @@ Object {
}
`;

exports[`body properties key fails when number in n=1 snake case component 1`] = `
Object {
"base": Object {
"info": Object {
"title": "OpenAPI",
"version": "0.0.0",
},
"openapi": "3.0.1",
"paths": Object {
"/example": Object {
"get": Object {
"responses": Object {},
},
},
},
},
"changelog": Array [
Object {
"added": Object {
"contentType": "application/json",
"flatSchema": Object {
"type": "object",
},
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json",
"kind": "body",
},
},
Object {
"added": Object {
"flatSchema": Object {
"type": "string",
},
"key": "30_days",
"required": false,
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"jsonSchemaTrail": Array [
"30_days",
],
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
"30_days",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/30_days",
"kind": "field",
},
},
Object {
"added": Object {
"description": "",
"statusCode": "200",
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"statusCode": "200",
},
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
],
"jsonPath": "/paths/~1example/get/responses/200",
"kind": "response",
},
},
],
"next": Object {
"info": Object {
"title": "OpenAPI",
"version": "0.0.0",
},
"openapi": "3.0.1",
"paths": Object {
"/example": Object {
"get": Object {
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"properties": Object {
"30_days": Object {
"type": "string",
},
},
"type": "object",
},
},
},
"description": "",
},
},
},
},
},
},
"results": Array [
Object {
"change": Object {
"added": Object {
"flatSchema": Object {
"type": "string",
},
"key": "30_days",
"required": false,
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"jsonSchemaTrail": Array [
"30_days",
],
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
"30_days",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/30_days",
"kind": "field",
},
},
"condition": "have snake case keys",
"docsLink": undefined,
"effectiveOnDate": undefined,
"error": "expected response property 30_days is be snake case",
"isMust": true,
"isShould": false,
"passed": false,
"where": "added field: 30_days",
},
],
}
`;

exports[`body properties key passes when snake case with a number as an n+1 component 1`] = `
Object {
"base": Object {
"info": Object {
"title": "OpenAPI",
"version": "0.0.0",
},
"openapi": "3.0.1",
"paths": Object {
"/example": Object {
"get": Object {
"responses": Object {},
},
},
},
},
"changelog": Array [
Object {
"added": Object {
"contentType": "application/json",
"flatSchema": Object {
"type": "object",
},
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json",
"kind": "body",
},
},
Object {
"added": Object {
"flatSchema": Object {
"type": "string",
},
"key": "is_allowed_after_30_days",
"required": false,
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"jsonSchemaTrail": Array [
"is_allowed_after_30_days",
],
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
"is_allowed_after_30_days",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/is_allowed_after_30_days",
"kind": "field",
},
},
Object {
"added": Object {
"description": "",
"statusCode": "200",
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"statusCode": "200",
},
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
],
"jsonPath": "/paths/~1example/get/responses/200",
"kind": "response",
},
},
],
"next": Object {
"info": Object {
"title": "OpenAPI",
"version": "0.0.0",
},
"openapi": "3.0.1",
"paths": Object {
"/example": Object {
"get": Object {
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"properties": Object {
"is_allowed_after_30_days": Object {
"type": "string",
},
},
"type": "object",
},
},
},
"description": "",
},
},
},
},
},
},
"results": Array [
Object {
"change": Object {
"added": Object {
"flatSchema": Object {
"type": "string",
},
"key": "is_allowed_after_30_days",
"required": false,
},
"changeType": "added",
"location": Object {
"conceptualLocation": Object {
"inResponse": Object {
"body": Object {
"contentType": "application/json",
},
"statusCode": "200",
},
"jsonSchemaTrail": Array [
"is_allowed_after_30_days",
],
"method": "get",
"path": "/example",
},
"conceptualPath": Array [
"operations",
"/example",
"get",
"responses",
"200",
"application/json",
"is_allowed_after_30_days",
],
"jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/is_allowed_after_30_days",
"kind": "field",
},
},
"condition": "have snake case keys",
"docsLink": undefined,
"effectiveOnDate": undefined,
"isMust": true,
"isShould": false,
"passed": true,
"where": "added field: is_allowed_after_30_days",
},
],
}
`;

exports[`body properties key passes when snake case with more than one component 1`] = `
Object {
"base": Object {
Expand Down
Loading

0 comments on commit a8f903a

Please sign in to comment.