2

According to Python Eve's documentation on schema definition, it currently comprehends the following types: string, boolean, integer, float, number (integer and float values allowed), datetime, dict, list and media. This means that there is no native support to the date type.

Creating schemas using the datetime type and changing the DATE_FORMAT config value to something such as %Y-%m-%d may provide an effective (even though superficial) solution, but since several other internal features in the same application rely on storing and manipulating these values in ISO 8601 format, this is not possible. Adding hooks to process response payloads and manually format specific fields is also an effective (and even more supperrficial) approach, but does not scale and may generate inconsistencies.

Any suggestions to solve this issue?

1 Answers1

1

You can create a custom data type to validate any format, see the docs here (https://docs.python-eve.org/en/stable/validation.html#custom-data-types).

As an example, a custom validator class for date as %d/%m/%Y:

class CustomValidators(Validator):
    def _validate_type_datestr(self, value):
        """
        Enables validation of a date field in the format %d/%m/%Y.
        """
        try:
            datetime.strptime(value, '%d/%m/%Y').date()
        except (ValueError, TypeError):
            return False
        return True

You can pass this class in your application init as validator and use datestr as a type in your schemas.

app = Eve(validator=CustomValidators)
gcw
  • 1,639
  • 1
  • 18
  • 37