Skip to content

Commit 72cf65f

Browse files
committed
Add validate() to validate user password and username api fields
1 parent 3974fe2 commit 72cf65f

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

manager/src/manager/serializers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,31 @@ class UserSerializer(NonNullSerializer):
823823
uid = serializers.CharField(source="pk", read_only=True)
824824
acls = PubSubACLSerializer(many=True, required=False)
825825

826+
def validate(self, attrs):
827+
is_update = self.instance is not None
828+
829+
if not is_update:
830+
username = attrs.get('username', None)
831+
password = attrs.get('password', None)
832+
833+
if not username or not username.strip():
834+
raise serializers.ValidationError({'username': ['This field is required.']})
835+
if not password:
836+
raise serializers.ValidationError({'password': ['This field is required.']})
837+
838+
else:
839+
if 'username' in self.initial_data:
840+
username = attrs.get('username', '')
841+
if not username or not username.strip():
842+
raise serializers.ValidationError({'username': ['This field may not be blank.']})
843+
844+
if 'password' in self.initial_data:
845+
password = attrs.get('password', '')
846+
if not password:
847+
raise serializers.ValidationError({'password': ['This field may not be blank.']})
848+
849+
return super().validate(attrs)
850+
826851
def create_update(self, validated_data, instance=None):
827852
is_update = instance is not None
828853

0 commit comments

Comments
 (0)