diff --git a/baloise.yml b/baloise.yml index 78a6d8f..d7ed456 100644 --- a/baloise.yml +++ b/baloise.yml @@ -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 diff --git a/doc/rules/must-use-additional-standard-http-status-codes.md b/doc/rules/must-use-additional-standard-http-status-codes.md index e97bd86..5411193 100644 --- a/doc/rules/must-use-additional-standard-http-status-codes.md +++ b/doc/rules/must-use-additional-standard-http-status-codes.md @@ -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. diff --git a/tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts b/tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts index 2bc886b..74e7987 100644 --- a/tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts +++ b/tests/150a-MUST-use-additional-standard-HTTP-status-codes.test.ts @@ -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`, + }), + ]), + ); + }); });