I'm adding in some custom validation at the moment and I'm finding that if the field I'm validating has already failed one of the built in DRF validations then the value isn't included in the attrs dictionary passed in. For example, if "username" is required then my validate_user function wont be supplied a username value in attrs to validate. This is resulting in me writing lots of identical exception handling code to prevent possible errors before I can do my actual validation:
def validate_username(self, attrs, source):
try:
username = attrs[source]
except KeyError:
return attrs
# actual validation goes here
Maybe this kind of makes sense because a username isn't supplied but I'm also having to do similar with an email field if the email address supplied doesn't match the regex it is tested against. Would it make sense to either pass the value in or just not call the custom validator at all if the validation has already failed? This would save lots of identical code that doesn't actually do anything. If it's not possible to make this change then the documentation should probably be updated to at least show this.