Skip to content

Commit

Permalink
feat: warning when 422 is used
Browse files Browse the repository at this point in the history
  • Loading branch information
dweber019 committed May 27, 2024
1 parent 9754fff commit 5b2386f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions baloise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ rules:
- '505'
- '511'
- default
must-use-additional-standard-http-status-codes-422:
message: 'Prefer 400 over 422 as response code'
description: MUST use standard HTTP status codes [150a]
documentationUrl: https://github.com/baloise-incubator/spectral-ruleset/blob/main/doc/rules/must-use-additional-standard-http-status-codes.md
severity: warn
given: $.paths.*.*.responses.422
then:
field: '422'
function: defined

should-use-well-understood-http-status-codes: off

Expand Down
3 changes: 2 additions & 1 deletion doc/rules/must-use-additional-standard-http-status-codes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# MUST use standard http status codes [150a]

We will also add 422 status code to allowed codes defined by [zalando](https://opensource.zalando.com/restful-api-guidelines/#150).
We will also add 422 status code to allowed codes defined by [zalando](https://opensource.zalando.com/restful-api-guidelines/#150).
But you should prefer 400 over 422.
19 changes: 19 additions & 0 deletions tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,23 @@ describe('MUST use additional standard HTTP status codes [150a]', () => {
]),
);
});

test('Detect 422 HTTP status codes for operations and warn', async () => {
const openApi = await loadOpenApiSpec('base-openapi.yml');
openApi.paths['/example'] = {
post: {
responses: {
'422': {},
},
},
};
const result = await lint(openApi, 'baloise');
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: `Prefer 400 over 422 as response code`,
}),
]),
);
});
});

0 comments on commit 5b2386f

Please sign in to comment.