Skip to content

Commit

Permalink
Ensure validation methods do not clash with parameters (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Dec 14, 2022
1 parent 5e029fa commit 82b545c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
20 changes: 11 additions & 9 deletions lumen/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _valid_keys_(cls) -> List[str] | None:
return cls._valid_keys

@classmethod
def _validate_keys(cls, spec: Dict[str, Any]):
def _validate_keys_(cls, spec: Dict[str, Any]):
valid_keys = cls._valid_keys_
for key in spec:
if valid_keys is None or key in valid_keys:
Expand All @@ -148,7 +148,7 @@ def _validate_keys(cls, spec: Dict[str, Any]):
raise ValidationError(msg, spec, key)

@classmethod
def _validate_required(
def _validate_required_(
cls, spec: Dict[str, Any], required: List[str | Tuple[str, ...]] | None = None
):
if required is None:
Expand Down Expand Up @@ -263,6 +263,8 @@ def _validate_ref(
def _validate_param(cls, key: str, value: Any, spec: Dict[str, Any]):
pobj = cls.param[key]
try:
if isinstance(pobj, param.Selector) and pobj.names and value in pobj.names:
return
pobj._validate(value)
except Exception as e:
msg = f"{cls.__name__} component {key!r} value failed validation: {str(e)}"
Expand Down Expand Up @@ -291,7 +293,7 @@ def _is_list_component_key(cls, key: str) -> bool:
)

@classmethod
def _validate_spec(
def _validate_spec_(
cls, spec: Dict[str, Any], context: Dict[str, Any] | None = None
) -> Dict[str, Any]:
validated: Dict[str, Any] = {}
Expand Down Expand Up @@ -407,9 +409,9 @@ def validate(
if isinstance(spec, str):
return spec
context = {} if context is None else context
cls._validate_keys(spec)
cls._validate_required(spec)
return cls._validate_spec(spec, context)
cls._validate_keys_(spec)
cls._validate_required_(spec)
return cls._validate_spec_(spec, context)


class MultiTypeComponent(Component):
Expand Down Expand Up @@ -552,6 +554,6 @@ def validate(
msg, attr = reverse_match_suggestion('type', list(spec), msg)
raise ValidationError(msg, spec, attr)
component_cls = cls._get_type(spec['type'], spec)
component_cls._validate_keys(spec)
component_cls._validate_required(spec)
return component_cls._validate_spec(spec, context)
component_cls._validate_keys_(spec)
component_cls._validate_required_(spec)
return component_cls._validate_spec_(spec, context)
8 changes: 4 additions & 4 deletions lumen/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _validate_template(
elif '.' not in template:
raise ValidationError(
f'Config template {template!r} not found. Template must be one '
'of {list(_TEMPLATES)} or an absolute import path.', spec, template
f'of {list(_TEMPLATES)} or an absolute import path.', spec, template
)
*paths, name = template.split('.')
path = '.'.join(paths)
Expand Down Expand Up @@ -854,9 +854,9 @@ def validate(
if 'targets' in spec:
spec['layouts'] = spec.pop('targets')

cls._validate_keys(spec)
cls._validate_required(spec)
return cls._validate_spec(spec)
cls._validate_keys_(spec)
cls._validate_required_(spec)
return cls._validate_spec_(spec)

def layout(self):
"""
Expand Down

0 comments on commit 82b545c

Please sign in to comment.