Skip to content

Commit

Permalink
Adding strict feature for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBurchLog committed Dec 3, 2024
1 parent e580283 commit 8161380
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion brewtils/schema_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import six # type: ignore
from box import Box # type: ignore
from marshmallow import ValidationError

import brewtils.models
import brewtils.schemas
Expand Down Expand Up @@ -527,6 +528,7 @@ def parse(
data, # type: Optional[Union[str, Dict[str, Any]]]
model_class, # type: Any
from_string=False, # type: bool
strict=True, # type: bool
**kwargs # type: Any
): # type: (...) -> Union[str, Dict[str, Any]]
"""Convert a JSON string or dictionary into a model object
Expand All @@ -535,6 +537,7 @@ def parse(
data: The raw input
model_class: Class object of the desired model type
from_string: True if input is a JSON string, False if a dictionary
strict: False if parsing should return back valid sections
**kwargs: Additional parameters to be passed to the Schema (e.g. many=True)
Returns:
Expand All @@ -561,7 +564,13 @@ def parse(

schema.context["models"] = cls._models

return schema.loads(data) if from_string else schema.load(data)
try:
return schema.loads(data) if from_string else schema.load(data)
except ValidationError as err:
if strict:
raise err
cls.logger.error(err.messages)
return err.valid_data

# Serialization methods
@classmethod
Expand Down

0 comments on commit 8161380

Please sign in to comment.