Skip to content

Commit a0621bb

Browse files
committed
feat: warning when 422 is used
1 parent 1b5cb29 commit a0621bb

3 files changed

+30
-1
lines changed

baloise.yml

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ rules:
9898
- '505'
9999
- '511'
100100
- default
101+
must-use-additional-standard-http-status-codes-422:
102+
message: 'Prefer 400 over 422 as response code'
103+
description: MUST use standard HTTP status codes [150a]
104+
documentationUrl: https://github.com/baloise-incubator/spectral-ruleset/blob/main/doc/rules/must-use-additional-standard-http-status-codes.md
105+
severity: warn
106+
given: $.paths.*.*.responses.422
107+
then:
108+
field: '422'
109+
function: defined
101110

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

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# MUST use standard http status codes [150a]
22

3-
We will also add 422 status code to allowed codes defined by [zalando](https://opensource.zalando.com/restful-api-guidelines/#150).
3+
We will also add 422 status code to allowed codes defined by [zalando](https://opensource.zalando.com/restful-api-guidelines/#150).
4+
But you should prefer 400 over 422.

tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,23 @@ describe('MUST use additional standard HTTP status codes [150a]', () => {
229229
]),
230230
);
231231
});
232+
233+
test('Detect 422 HTTP status codes for operations and warn', async () => {
234+
const openApi = await loadOpenApiSpec('base-openapi.yml');
235+
openApi.paths['/example'] = {
236+
post: {
237+
responses: {
238+
'422': {},
239+
},
240+
},
241+
};
242+
const result = await lint(openApi, 'baloise');
243+
expect(result).toEqual(
244+
expect.arrayContaining([
245+
expect.objectContaining({
246+
message: `Prefer 400 over 422 as response code`,
247+
}),
248+
]),
249+
);
250+
});
232251
});

0 commit comments

Comments
 (0)