Skip to content

Commit 2318c0e

Browse files
author
Rodrigo Martins de Oliveira
authored
Merge pull request flasgger#144 from allrod5/document-custom-validation-error-handling
Document Validation Error Handler
2 parents dbb053a + 6d17aa4 commit 2318c0e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Diff for: README.md

+55
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,61 @@ from flasgger import validate
471471
request.json, 'Pet', 'defs.yml', validation_function=my_function)
472472
```
473473

474+
### Validation Error handling
475+
476+
By default Flasgger will handle validation errors by aborting the
477+
request with a 400 BAD REQUEST response with the error message.
478+
479+
A custom validation error handling function can be provided to
480+
supersede default behavior as long as it meets the requirements:
481+
- take three, and only three, positional arguments:
482+
- the error raised as the first;
483+
- the data which failed validation as the second; and
484+
- the schema used in to validate as the third argument
485+
486+
487+
Providing the function to the Swagger instance will make it the default:
488+
489+
```python
490+
from flasgger import Swagger
491+
492+
swagger = Swagger(app, validation_error_handler=my_handler)
493+
```
494+
495+
Providing the function as parameter of `swag_from` or `swagger.validate`
496+
annotations or directly to the `validate` function will force it's use
497+
over the default validation function for Swagger:
498+
499+
```python
500+
from flasgger import swag_from
501+
502+
@swag_from(
503+
'spec.yml', validation=True, validation_error_handler=my_handler)
504+
...
505+
```
506+
507+
```python
508+
from flasgger import Swagger
509+
510+
swagger = Swagger(app)
511+
512+
@swagger.validate('Pet', validation_error_handler=my_handler)
513+
...
514+
```
515+
516+
```python
517+
from flasgger import validate
518+
519+
...
520+
521+
validate(
522+
request.json, 'Pet', 'defs.yml',
523+
validation_error_handler=my_handler)
524+
```
525+
526+
Examples of use of a custom validation error handler function can be
527+
found at [example validation_error_handler.py](examples/validation_error_handler.py)
528+
474529
# Get defined schemas as python dictionaries
475530

476531
You may wish to use schemas you defined in your Swagger specs as dictionaries

0 commit comments

Comments
 (0)