Skip to content

Commit 11b887a

Browse files
committed
feat(validator): include missing data elements from JsonSchemaValueException
1 parent 505891d commit 11b887a

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

aws_lambda_powertools/utilities/validation/base.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def validate_data_against_schema(data: Union[Dict, str], schema: Dict, formats:
3232
fastjsonschema.validate(definition=schema, data=data, formats=formats)
3333
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
3434
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")
35-
except fastjsonschema.JsonSchemaException as e:
36-
message = f"Failed schema validation. Error: {e.message}, Path: {e.path}, Data: {e.value}" # noqa: B306, E501
37-
raise SchemaValidationError(message)
35+
except fastjsonschema.JsonSchemaValueException as e:
36+
message = f"Failed schema validation. Error: {e.message}, Path: {e.path}, Data: {e.value}"
37+
raise SchemaValidationError(
38+
message,
39+
validation_message=e.message,
40+
name=e.name,
41+
path=e.path,
42+
value=e.value,
43+
definition=e.definition,
44+
rule=e.rule,
45+
rule_definition=e.rule_definition,
46+
)

aws_lambda_powertools/utilities/validation/exceptions.py

+46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1+
from typing import Any, List, Optional
2+
13
from ...exceptions import InvalidEnvelopeExpressionError
24

35

46
class SchemaValidationError(Exception):
57
"""When serialization fail schema validation"""
68

9+
def __init__(
10+
self,
11+
message: str,
12+
validation_message: Optional[str] = None,
13+
name: Optional[str] = None,
14+
path: Optional[List] = None,
15+
value: Optional[Any] = None,
16+
definition: Optional[Any] = None,
17+
rule: Optional[str] = None,
18+
rule_definition: Optional[Any] = None,
19+
):
20+
"""
21+
22+
Parameters
23+
----------
24+
message : str
25+
Powertools formatted error message
26+
validation_message : str, optional
27+
Containing human-readable information what is wrong (e.g. ``data.property[index] must be smaller than or
28+
equal to 42``)
29+
name : str, optional
30+
name of a path in the data structure (e.g. ``data.property[index]``)
31+
path: List, optional
32+
``path`` as an array in the data structure (e.g. ``['data', 'property', 'index']``),
33+
value : Any, optional
34+
the invalid value
35+
definition : Any, optional
36+
The full rule ``definition`` (e.g. ``42``)
37+
rule : str, optional
38+
`rule`` which the ``data`` is breaking (e.g. ``maximum``)
39+
rule_definition : Any, optional
40+
The full rule ``definition`` (e.g. ``42``)
41+
"""
42+
super().__init__(message)
43+
self.message = message
44+
45+
self.validation_message = validation_message
46+
self.name = name
47+
self.path = path
48+
self.value = value
49+
self.definition = definition
50+
self.rule = rule
51+
self.rule_definition = rule_definition
52+
753

854
class InvalidSchemaFormatError(Exception):
955
"""When JSON Schema is in invalid format"""

0 commit comments

Comments
 (0)