Skip to content

Commit b980189

Browse files
committed
à faire valider.
test cases in test_routes.py expect a validation error format thats need to be match fastjsonschema provides {key} is a required property for missing required fields Additionnal properties are not allowed for extra properties Modifications : - Catch fastjsonjsonschema exceptions and extract the field name - transform the error message to match the format expected by tests - uses string manipulations to handle both mising required fields and additionnal properties cases pytest : 40/40 passed
1 parent e747d0e commit b980189

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/opengeodeweb_back/utils_functions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,19 @@ def validate_request(request, schema):
8585
validate = fastjsonschema.compile(schema)
8686
validate(json_data)
8787
except fastjsonschema.JsonSchemaException as e:
88-
flask.abort(400, f"Validation error: {str(e)}")
88+
error_msg = str(e)
89+
90+
# Transforme les messages d'erreur pour les rendre plus lisibles
91+
if "data must contain" in error_msg:
92+
# Estraire le nom du champ depuis jsonschema erreurs
93+
field = error_msg.split("data must contain ['")[1].split("']")[0]
94+
error_msg = f"'{field}' is a required property"
95+
elif "data must not contain" in error_msg:
96+
# Extrait le nom en plus du champ
97+
field = error_msg.split("data must not contain {'")[1].split("'")[0]
98+
error_msg = f"Additional properties are not allowed ('{field}' was unexpected)"
99+
100+
flask.abort(400, f"Validation error: {error_msg}")
89101

90102

91103
def set_interval(func, sec, args=None):

0 commit comments

Comments
 (0)