Skip to content

Commit 57580fc

Browse files
author
Johan van den Dorpe
committed
Update docs and tests for custom field validation
1 parent 92ab979 commit 57580fc

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

changes/205.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for validating custom fields by referencing them as cf_<custom_field_name> in ComplianceError exceptions

docs/user/app_data_compliance.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Any `DataComplianceRule` class can have a `name` defined to provide a friendly n
1818
>
1919
> For example, if a user fixes an object attribute that was incompliant with a built-in rule and then navigates to its `Data Compliance` tab, the object will still show as invalid for that built-in rule. This will remain the case until the job is ran again with the `Run built-in validation rules?` option checked.
2020
21+
!!! note
22+
When raising a ComplianceError, the attribute must exist on the object. To raise errors for custom fields, use cf_custom_field_name as the attribute name.
23+
2124
## How to Use
2225

2326
### Step 1. Create Data Compliance Rules

nautobot_data_validation_engine/tests/test_data_compliance_rules.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,34 @@ def test_validate_replaces_results(self):
7878
len(DataCompliance.objects.filter(compliance_class_name=TestFailedDataComplianceRule.__name__)),
7979
5,
8080
)
81+
82+
def test_custom_field_attribute_value(self):
83+
# Simulate a Location with a custom field value
84+
self.s.cf = {"foo": "bar"}
85+
# Patch DataComplianceRule.context to include our instance
86+
rule = TestPassedDataComplianceRule(self.s)
87+
rule.context = {"object": self.s}
88+
89+
# Call _create_data_compliance_object with a custom field attribute
90+
obj = rule._create_data_compliance_object(attribute="cf_foo", valid=True, message="msg")
91+
self.assertEqual(obj.validated_attribute, "cf_foo")
92+
self.assertEqual(obj.validated_attribute_value, "bar")
93+
94+
def test_custom_field_attribute_value_missing(self):
95+
# Simulate a Location with no custom field value
96+
self.s.cf = {}
97+
rule = TestPassedDataComplianceRule(self.s)
98+
rule.context = {"object": self.s}
99+
100+
obj = rule._create_data_compliance_object(attribute="cf_missing", valid=True, message="msg")
101+
self.assertEqual(obj.validated_attribute, "cf_missing")
102+
self.assertIsNone(obj.validated_attribute_value)
103+
104+
def test_regular_attribute_value(self):
105+
# Test with a regular attribute
106+
rule = TestPassedDataComplianceRule(self.s)
107+
rule.context = {"object": self.s}
108+
109+
obj = rule._create_data_compliance_object(attribute="name", valid=True, message="msg")
110+
self.assertEqual(obj.validated_attribute, "name")
111+
self.assertEqual(obj.validated_attribute_value, self.s.name)

0 commit comments

Comments
 (0)