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 all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ The types of changes are:

## [Unreleased](https://github.com/ethyca/fideslang/compare/3.0.7...main)

### Changed

- Remove stipulation that sub fields (Field) of a Field object cannot have data categories assigned [#22](https://github.com/ethyca/fideslang/pull/22)


## [3.0.7](https://github.com/ethyca/fideslang/compare/3.0.6...3.0.7)

Expand Down
1 change: 0 additions & 1 deletion src/fideslang/default_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
2. Copy/Paste the JSON response below
"""


COUNTRY_CODES = [
{"name": "Afghanistan", "alpha3Code": "AFG"},
{"name": "Åland Islands", "alpha3Code": "ALA"},
Expand Down
4 changes: 2 additions & 2 deletions src/fideslang/default_taxonomy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from .data_uses import DEFAULT_DATA_USES
from .organizations import DEFAULT_ORGANIZATIONS

sort_data_types = (
lambda x: x.parent_key if hasattr(x, "parent_key") and x.parent_key else x.fides_key
sort_data_types = lambda x: (
Copy link
Author

Choose a reason for hiding this comment

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

autoformatter

x.parent_key if hasattr(x, "parent_key") and x.parent_key else x.fides_key
)

DEFAULT_TAXONOMY = Taxonomy(
Expand Down
1 change: 1 addition & 0 deletions src/fideslang/manifests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module handles anything related to working with raw manifest files."""

import glob
from functools import reduce
from typing import Dict, List, Set, Union
Expand Down
5 changes: 0 additions & 5 deletions src/fideslang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ def validate_object_fields(
) -> 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
Expand All @@ -494,10 +493,6 @@ def validate_object_fields(
f"The data type '{data_type}' on field '{field_name}' is not compatible with specified sub-fields. Convert to an 'object' field."
)

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


Expand Down
1 change: 1 addition & 0 deletions src/fideslang/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module handles everything related to parsing resources into Pydantic models,
either from local files or the server.
"""

from typing import Dict, List

from fideslang import FidesModel, Taxonomy, model_map
Expand Down
1 change: 1 addition & 0 deletions src/fideslang/validation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Contains all of the additional validation for the resource models.
"""

import re
from collections import Counter
from typing import Annotated, Dict, List, Optional, Pattern, Set, Tuple
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common fixtures to be used across tests."""

import os
from typing import Any, Dict

Expand Down
81 changes: 41 additions & 40 deletions tests/fideslang/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,59 +717,61 @@ 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"
)

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"
field = 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 field
assert field.data_categories == ["user"]
assert field.fides_meta.data_type == "object"

def test_data_categories_on_nested_fields(self):
DatasetField(
name="test_field",

field = DatasetField(
name="test_for_nest",
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", data_categories=["user"])],
fields=[
DatasetField(
name="nested_field",
data_categories=["user"],
fides_meta=FidesMeta(
references=None,
identify=None,
primary_key=False,
data_type="string",
length=None,
read_only=None,
data_categories=["user"],
),
)
],
)

assert field
assert field.data_categories == ["user"]
assert field.fields[0].data_categories == ["user"]
Copy link
Author

Choose a reason for hiding this comment

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

real test here, a nested field can in fact have a category



class TestCollectionMeta:
def test_invalid_collection_key(self):
Expand Down Expand Up @@ -806,7 +808,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