Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HJ-20 save data categories nested fields #22

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/fideslang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,35 +471,6 @@ def valid_meta(cls, meta_values: Optional[FidesMeta]) -> Optional[FidesMeta]:
)
return meta_values

@model_validator(mode="after")
def validate_object_fields(
self,
_: ValidationInfo,
) -> DatasetField:
"""Two validation checks for object fields:
- If there are sub-fields specified, type should be either empty or 'object'
- Additionally object fields cannot have data_categories.
"""
fields = self.fields
declared_data_type = None
field_name: str = self.name

if self.fides_meta:
declared_data_type = self.fides_meta.data_type

if fields and declared_data_type:
data_type, _ = parse_data_type_string(declared_data_type)
if data_type != "object":
raise ValueError(
f"The data type '{data_type}' on field '{field_name}' is not compatible with specified sub-fields. Convert to an 'object' field."
)
thingscouldbeworse marked this conversation as resolved.
Show resolved Hide resolved

if (fields or declared_data_type == "object") and self.data_categories:
raise ValueError(
f"Object field '{field_name}' cannot have specified data_categories. Specify category on sub-field instead"
)
return self


# this is required for the recursive reference in the pydantic model:
DatasetField.model_rebuild()
Expand Down
63 changes: 28 additions & 35 deletions tests/fideslang/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,43 +717,37 @@ def test_return_all_elements_on_array_field(self):
)

def test_data_categories_at_object_level(self):
with pytest.raises(ValidationError) as exc:
DatasetField(
name="test_field",
data_categories=["user"],
fides_meta=FidesMeta(
references=None,
identify=None,
primary_key=False,
data_type="object",
length=None,
return_all_elements=None,
read_only=None,
),
fields=[DatasetField(name="nested_field")],
)
assert_error_message_includes(
exc, "Object field 'test_field' cannot have specified data_categories"
# Data categories at the object level ARE allowed now
DatasetField(
thingscouldbeworse marked this conversation as resolved.
Show resolved Hide resolved
name="test_field",
data_categories=["user"],
fides_meta=FidesMeta(
references=None,
identity=None,
primary_key=False,
data_type="object",
length=None,
return_all_elements=None,
read_only=None,
),
fields=[DatasetField(name="nested_field")],
)

def test_object_field_conflicting_types(self):
with pytest.raises(ValidationError) as exc:
DatasetField(
name="test_field",
data_categories=["user"],
fides_meta=FidesMeta(
references=None,
identify=None,
primary_key=False,
data_type="string",
length=None,
return_all_elements=None,
read_only=None,
),
fields=[DatasetField(name="nested_field")],
)
assert_error_message_includes(
Copy link
Author

Choose a reason for hiding this comment

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

If we want users to add data categories to the "non-terminal" nested fields (middle fields, or top level) then this validation needs to be removed and the reverse of this test is now the case

exc, "The data type 'string' on field 'test_field' is not compatible with"

DatasetField(
name="test_field",
data_categories=["user"],
fides_meta=FidesMeta(
references=None,
identify=None,
primary_key=False,
data_type="string",
length=None,
return_all_elements=None,
read_only=None,
),
fields=[DatasetField(name="nested_field")],
)

def test_data_categories_on_nested_fields(self):
Expand Down Expand Up @@ -806,7 +800,6 @@ def test_erase_after(self):
assert meta.erase_after == [FidesCollectionKey("test_dataset.test_collection")]



class TestAnyUrlString:
def test_valid_url(self):
assert AnyUrlString("https://www.example.com/")
Expand Down
Loading