Skip to content

Conversation

@simonLeary42
Copy link
Contributor

@simonLeary42 simonLeary42 commented Oct 31, 2025

copied from Project:

validator = AttributeValidator(self.value)
if expected_value_type == "Int":
validator.validate_int()
elif expected_value_type == "Float":
validator.validate_float()
elif expected_value_type == "Yes/No":
validator.validate_yes_no()
elif expected_value_type == "Date":
validator.validate_date()

I would have removed attribute validation boilerplate from Resource, too, but it's incompatible. See #865 and #864.

Comment on lines -61 to +64
AllocationAttributeFactory(
allocation=cls.allocation,
value=100,
allocation_attribute_type=AllocationAttributeTypeFactory(name="Storage Quota (TB)"),
)
attr_type = AAttributeTypeFactory(name="Int")
alloc_attr_type = AllocationAttributeTypeFactory(name="Storage Quota (TB)", attribute_type=attr_type)
AllocationAttributeFactory(allocation=cls.allocation, value=100, allocation_attribute_type=alloc_attr_type)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fragile. It depended on the fact that AAttributeTypeFactory has name="Int" by default.

@simonLeary42 simonLeary42 force-pushed the attribute-validator-boilerplate branch from 4b38576 to cd29a3c Compare October 31, 2025 19:38
cls.nonproject_user = UserFactory(is_staff=False, is_superuser=False)

attributetype = PAttributeTypeFactory(name="string")
attributetype = PAttributeTypeFactory(name="Text")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string is not a valid attribute type.

Comment on lines +15 to +36
def _raise_if_empty(self):
if self.value == "":
raise ValidationError(f'Invalid Value "{self.value}". Value cannot be empty.')

def validate_int(self):
self._raise_if_empty()
try:
validate = validators.Int()
validate.to_python(self.value)
except Exception:
raise ValidationError(f"Invalid Value {self.value}. Value must be an int.")

def validate_float(self):
self._raise_if_empty()
try:
validate = validators.Number()
validate.to_python(self.value)
except Exception:
raise ValidationError(f"Invalid Value {self.value}. Value must be an float.")

def validate_yes_no(self):
self._raise_if_empty()
Copy link
Contributor Author

@simonLeary42 simonLeary42 Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without _raise_if_empty(), validate_int(""), validate_float(""), and validate_yes_no("") all succeed.

@simonLeary42 simonLeary42 changed the title remove attribute validation boilerplate remove attribute validation boilerplate, add tests, improve tests Oct 31, 2025
@simonLeary42 simonLeary42 force-pushed the attribute-validator-boilerplate branch from cd29a3c to b4a7152 Compare November 6, 2025 00:35
@simonLeary42 simonLeary42 force-pushed the attribute-validator-boilerplate branch from b4a7152 to fd428c8 Compare November 6, 2025 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant