Skip to content

Commit

Permalink
Clean up draft validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-Jess committed Mar 24, 2024
1 parent 8e0d48d commit d79127c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def validate_publish(self, validated_data, instance=None):
"expires_at",
]

def field_bad(field, validated_data, instance):
if field in validated_data:
if not validated_data[field]:
return True
else:
if not instance or not hasattr(instance, field) or not getattr(instance, field):
return True
return False

if bad_fields := [field for field in fields if field_bad(field, validated_data, instance)]:
newest_fields = [
(field, validated_data[field])
if field in validated_data
else (field, getattr(instance, field))
if instance and hasattr(instance, field)
else (field, None)
for field in fields
]

if bad_fields := [field[0] for field in newest_fields if not field[1]]:
raise serializers.ValidationError(
f"fields: {', '.join(bad_fields)} are required to publish sublet."
)
Expand Down

0 comments on commit d79127c

Please sign in to comment.