Skip to content

Commit f20e211

Browse files
author
Shogun
committed
feat: Updated docs.
1 parent d1d38ec commit f20e211

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Register as a plugin, optional providing any of the following options:
2323

2424
- `hideUnhandledErrors`: If to hide unhandled server errors or returning to the client including stack information. Default is to hide errors when `NODE_ENV` environment variable is `production`.
2525
- `convertValidationErrors`: Convert validation errors to a structured human readable object. Default is `true`.
26+
- `convertResponsesValidationErrors`: Convert response validation errors to a structured human readable object. Default is to enable when `NODE_ENV` environment variable is different from `production`.
2627

2728
Once registered, the server will use the plugin handlers for all errors (basically, both `setErrorHandler` and `setNotFoundHandler` are called).
2829

@@ -126,6 +127,38 @@ When hitting `/invalid` it will return the following:
126127
}
127128
```
128129

130+
## Validation and response validation errors
131+
132+
If enabled, response will have status of 400 or 500 (depending on whether the request or response validation failed) and the the body will have the `failedValidations` property.
133+
134+
Example of a client validation error:
135+
136+
```json
137+
{
138+
"statusCode": 400,
139+
"error": "Bad Request",
140+
"message": "One or more validations failed trying to process your request.",
141+
"failedValidations": { "query": { "val": "must match pattern \"ab{2}c\"", "val2": "is not a valid property" } }
142+
}
143+
```
144+
145+
Example of a response validation error:
146+
147+
```json
148+
{
149+
"error": "Internal Server Error",
150+
"message": "The response returned from the endpoint violates its specification for the HTTP status 200.",
151+
"statusCode": 500,
152+
"failedValidations": {
153+
"response": {
154+
"a": "must be a string",
155+
"b": "must be present",
156+
"c": "is not a valid property"
157+
}
158+
}
159+
}
160+
```
161+
129162
## Contributing to fastify-errors-properties
130163

131164
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

src/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function addResponseValidation(this: FastifyDecoratedInstance, route: Rou
217217
const statusCode = reply.res.statusCode
218218

219219
// Never validate error 500
220-
if (statusCode === 500) {
220+
if (statusCode === INTERNAL_SERVER_ERROR) {
221221
return payload
222222
}
223223

test/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('Plugin', function(): void {
328328

329329
expect(response).toHaveHTTPStatus(BAD_REQUEST)
330330
expect(JSON.parse(response.payload)).toEqual({
331-
statusCode: 400,
331+
statusCode: BAD_REQUEST,
332332
error: 'Bad Request',
333333
message: 'One or more validations failed trying to process your request.',
334334
failedValidations: { params: { id: 'must be a valid number' } }
@@ -345,7 +345,7 @@ describe('Plugin', function(): void {
345345

346346
expect(response).toHaveHTTPStatus(BAD_REQUEST)
347347
expect(JSON.parse(response.payload)).toEqual({
348-
statusCode: 400,
348+
statusCode: BAD_REQUEST,
349349
error: 'Bad Request',
350350
message: 'One or more validations failed trying to process your request.',
351351
failedValidations: { query: { val: 'must match pattern "ab{2}c"', val2: 'is not a valid property' } }
@@ -357,7 +357,7 @@ describe('Plugin', function(): void {
357357

358358
expect(response).toHaveHTTPStatus(BAD_REQUEST)
359359
expect(JSON.parse(response.payload)).toEqual({
360-
statusCode: 400,
360+
statusCode: BAD_REQUEST,
361361
error: 'Bad Request',
362362
message: 'One or more validations failed trying to process your request.',
363363
failedValidations: { headers: { 'x-header': 'must be present' } }
@@ -369,7 +369,7 @@ describe('Plugin', function(): void {
369369

370370
expect(response).toHaveHTTPStatus(BAD_REQUEST)
371371
expect(JSON.parse(response.payload)).toEqual({
372-
statusCode: 400,
372+
statusCode: BAD_REQUEST,
373373
error: 'Bad Request',
374374
message: 'One or more validations failed trying to process your request.',
375375
failedValidations: { body: { $root: 'must be an array' } }

0 commit comments

Comments
 (0)