Skip to content
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

Warning when 422 is used #57

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`,
}),
]),
);
});
});
Loading