Validate the data inside a request against a schema.
(data, schema)
| 56 | |
| 57 | |
| 58 | def validate(data, schema): |
| 59 | """Validate the data inside a request against a schema.""" |
| 60 | validator = get_validator(schema) |
| 61 | errors = {} |
| 62 | for error in validator.iter_errors(data): |
| 63 | path = ".".join((str(c) for c in error.path)) |
| 64 | if path not in errors: |
| 65 | errors[path] = error.message |
| 66 | else: |
| 67 | errors[path] += "; " + error.message |
| 68 | log.info("ERROR [%s]: %s", path, error.message) |
| 69 | |
| 70 | if not len(errors): |
| 71 | return data |
| 72 | |
| 73 | resp = jsonify( |
| 74 | { |
| 75 | "status": "error", |
| 76 | "errors": errors, |
| 77 | "message": gettext("Error during data validation"), |
| 78 | }, |
| 79 | status=400, |
| 80 | ) |
| 81 | raise BadRequest(response=resp) |
| 82 | |
| 83 | |
| 84 | def clean_object(data): |