Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 35 additions & 0 deletions src/check_datapackage/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,40 @@ def _handle_S_resources_x_schema_fields_x(
return edits


def _handle_S_resources_x_schema_primary_key(
parent_error: SchemaError,
schema_errors: list[SchemaError],
) -> SchemaErrorEdits:
"""Only flag errors for the relevant type and simplify errors."""
PRIMARY_KEY_TYPES: tuple[type[Any], ...] = (list, str)
edits = SchemaErrorEdits(remove=[parent_error])
errors_in_group = _get_errors_in_group(schema_errors, parent_error)

key_type = type(parent_error.instance)
if key_type in PRIMARY_KEY_TYPES:
schema_for_type = f"primaryKey/oneOf/{PRIMARY_KEY_TYPES.index(key_type)}/"
edits.remove.extend(
_filter(
errors_in_group,
lambda error: schema_for_type not in error.schema_path,
)
)
return edits

edits.remove.extend(errors_in_group)
edits.add.append(
SchemaError(
message="The `primaryKey` property must be a string or an array.",
type="type",
jsonpath=parent_error.jsonpath,
schema_path=parent_error.schema_path,
instance=parent_error.instance,
)
)

return edits


def _handle_licenses(
parent_error: SchemaError,
schema_errors: list[SchemaError],
Expand Down Expand Up @@ -333,6 +367,7 @@ def _handle_licenses(
("resources/items/oneOf", _handle_S_resources_x),
("resources/items/properties/path/oneOf", _handle_S_resources_x_path),
("fields/items/oneOf", _handle_S_resources_x_schema_fields_x),
("primaryKey/oneOf", _handle_S_resources_x_schema_primary_key),
("licenses/items/anyOf", _handle_licenses),
]

Expand Down
43 changes: 43 additions & 0 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,49 @@ def test_fail_unknown_field_with_bad_property():
assert issues[0].jsonpath == "$.resources[0].schema.fields[0].type"


@mark.parametrize("primary_key", ["id", ["name", "address"]])
def test_pass_good_primary_key(primary_key):
properties = example_package_properties()
properties["resources"][0]["schema"]["primaryKey"] = primary_key

issues = check(properties)

assert issues == []


def test_fail_primary_key_of_bad_type():
properties = example_package_properties()
properties["resources"][0]["schema"]["primaryKey"] = 123

issues = check(properties)

assert len(issues) == 1
assert issues[0].type == "type"
assert issues[0].jsonpath == "$.resources[0].schema.primaryKey"


def test_fail_primary_key_with_bad_array():
properties = example_package_properties()
properties["resources"][0]["schema"]["primaryKey"] = []

issues = check(properties)

assert len(issues) == 1
assert issues[0].type == "minItems"
assert issues[0].jsonpath == "$.resources[0].schema.primaryKey"


def test_fail_primary_key_with_bad_array_item():
properties = example_package_properties()
properties["resources"][0]["schema"]["primaryKey"] = [123, "name"]

issues = check(properties)

assert len(issues) == 1
assert issues[0].type == "type"
assert issues[0].jsonpath == "$.resources[0].schema.primaryKey[0]"


def test_fail_package_license_with_no_name_or_path():
properties = example_package_properties()
del properties["licenses"][0]["name"]
Expand Down