Skip to content
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
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_S_resources_x_schema_foreign_keys(
parent_error: SchemaError,
schema_errors: list[SchemaError],
Expand Down Expand Up @@ -386,6 +420,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),
("foreignKeys/items/oneOf", _handle_S_resources_x_schema_foreign_keys),
("licenses/items/anyOf", _handle_licenses),
]
Expand Down
87 changes: 65 additions & 22 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,28 +389,6 @@ def test_fail_foreign_keys_with_missing_fields(ref_fields):
assert issues[0].jsonpath == "$.resources[0].schema.foreignKeys[0].fields"


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

issues = check(properties)

assert len(issues) == 1
assert issues[0].type == "required"
assert issues[0].jsonpath == "$.licenses[0]"


def test_fail_resource_license_with_no_name_or_path():
properties = example_package_properties()
properties["resources"][0]["licenses"] = [{}]

issues = check(properties)

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


@mark.parametrize("ref_fields", ["purchase_id", ["purchase_id"], 123, []])
def test_fail_foreign_keys_with_bad_fields(ref_fields):
properties = example_package_properties()
Expand Down Expand Up @@ -513,6 +491,71 @@ def test_fail_foreign_keys_with_bad_array_item():
)


@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"]

issues = check(properties)

assert len(issues) == 1
assert issues[0].type == "required"
assert issues[0].jsonpath == "$.licenses[0]"


def test_fail_resource_license_with_no_name_or_path():
properties = example_package_properties()
properties["resources"][0]["licenses"] = [{}]

issues = check(properties)

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


def test_error_as_true():
properties = {
"name": 123,
Expand Down
Loading