diff --git a/ckanext/transmute/logic/action/get.py b/ckanext/transmute/logic/action/get.py index f077810..e312eba 100644 --- a/ckanext/transmute/logic/action/get.py +++ b/ckanext/transmute/logic/action/get.py @@ -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: diff --git a/ckanext/transmute/transmutators.py b/ckanext/transmute/transmutators.py index 59fd9b3..04b66c2 100644 --- a/ckanext/transmute/transmutators.py +++ b/ckanext/transmute/transmutators.py @@ -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