Skip to content

New validator: DiscardValidator #92

@binaryDiv

Description

@binaryDiv

Implement a DiscardValidator that accepts any kind of input, but discards it and returns a (predefined) value instead.

There are no validation errors for this validator, the input is just completely ignored. (For a validator that rejects any kind of input, there already is the RejectValidator.)

The implementation could look somewhat like this (copied from a project):

class DiscardValidator(Validator):
    """
    Meta validator that simply discards any input and always returns the same predefined value.

    Set the `output_value` parameter to any value to specify the value returned by the validator. If not set, the value
    defaults to `None`.
    """

    # Value that is returned by the validator
    output_value: Any

    def __init__(self, *, output_value: Any = None):
        self.return_value = output_value

    def validate(self, input_data: Any, **kwargs) -> Any:
        # Ignore input completely and just return the predefined output value
        return self.output_value

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions