Skip to content

Commit

Permalink
fix: tweak Context.validate
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Nov 29, 2022
1 parent d6de473 commit 285d89c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions beet/toolchain/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,28 @@ def validate(
options: Optional[JsonDict] = None,
) -> T:
"""Validate options."""
if options is None:
options = self.meta.get(key)
if options is None and isinstance(value := self.meta.get(key), dict):
options = value

if options is None:
head, *tail = key.split(".")
options = self.meta.get(head)

for part in tail:
if isinstance(options, dict):
options = options.get(part) # type: ignore
else:
options = None
break
if isinstance(value := self.meta.get(head), dict):
options = value

for part in tail:
if isinstance(value := options.get(part), dict):
options = value
else:
break

if options is None:
options = {}

try:
return validator(**(options or {}))
if isinstance(validator, type) and issubclass(validator, BaseModel):
return validator.parse_obj(options) # type: ignore
return validator(**options)
except BubbleException:
raise
except ValidationError as exc:
Expand Down

0 comments on commit 285d89c

Please sign in to comment.