Skip to content

Commit

Permalink
feat: add stop_on_empty transmutator
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Oct 20, 2024
1 parent 4b14b09 commit ea07550
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ckanext/transmute/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def _apply_validators(field: Field, validators: list[str | list[str]]):
field = get_transmutator(validator[0])(field, *validator[1:])
else:
field = get_transmutator(validator)(field)
except df.StopOnError:
return field.value
except df.Invalid as e:
raise ValidationError({f"{field.type}:{field.field_name}": [e.error]})
except TypeError as e:
Expand Down
16 changes: 16 additions & 0 deletions ckanext/transmute/transmutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ def to_string(field: Field) -> Field:
return field


@transmutator
def stop_on_empty(field: Field) -> Field:
"""Stop transmutation if field is empty
Args:
field (Field): Field object
Returns:
Field: the same Field
"""
if not field.value:
raise df.StopOnError

return field


@transmutator
def get_nested(field: Field, *path) -> Field:
"""Fetches a nested value from a field
Expand Down

0 comments on commit ea07550

Please sign in to comment.