Skip to content

Commit d328ffe

Browse files
committed
refactor: ♻️ refactor _keep_resources_with_no_issue_at_property
1 parent 5af52f0 commit d328ffe

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/check_datapackage/check.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class for more details, especially about the default values.
129129
_set_should_fields_to_required(schema)
130130

131131
issues = _check_object_against_json_schema(properties, schema)
132-
issues += _check_keys(properties, _map(issues, lambda issue: issue.jsonpath))
132+
issues += _check_keys(properties, issues)
133133
issues += apply_extensions(properties, config.extensions)
134134
issues = exclude(issues, config.exclusions, properties)
135135
issues = sorted(set(issues))
@@ -140,36 +140,42 @@ class for more details, especially about the default values.
140140
return issues
141141

142142

143-
def _check_keys(properties: dict[str, Any], issue_jsonpaths: list[str]) -> list[Issue]:
143+
def _check_keys(properties: dict[str, Any], issues: list[Issue]) -> list[Issue]:
144144
"""Check that primary and foreign keys exist."""
145145
# Primary keys
146-
issues = []
146+
key_issues = []
147147

148148
# Foreign keys
149149
resources_with_fk = _get_fields_at_jsonpath(
150150
"$.resources[?(length(@.schema.foreignKeys) > 0)]",
151151
properties,
152152
)
153-
resources_with_fk = _only_resources_with_correct_property_at(
154-
"schema.foreignKeys", resources_with_fk, issue_jsonpaths
153+
resources_with_fk = _keep_resources_with_no_issue_at_property(
154+
resources_with_fk, issues, "schema.foreignKeys"
155155
)
156-
issues += _flat_map(
156+
key_issues += _flat_map(
157157
resources_with_fk,
158158
lambda resource: _check_foreign_keys(resource, properties),
159159
)
160-
return issues
160+
return key_issues
161+
162+
163+
def _issues_at_property(
164+
resource: PropertyField, issues: list[Issue], jsonpath: str
165+
) -> list[Issue]:
166+
return _filter(
167+
issues,
168+
lambda issue: f"{resource.jsonpath}.{jsonpath}" in issue.jsonpath,
169+
)
161170

162171

163-
def _only_resources_with_correct_property_at(
164-
jsonpath: str, resources: list[PropertyField], issue_jsonpaths: list[str]
172+
def _keep_resources_with_no_issue_at_property(
173+
resources: list[PropertyField], issues: list[Issue], jsonpath: str
165174
) -> list[PropertyField]:
166-
"""Filter out resources that have an error at or under the given `jsonpath`."""
175+
"""Filter out resources that have an issue at or under the given `jsonpath`."""
167176
return _filter(
168177
resources,
169-
lambda resource: not _filter(
170-
issue_jsonpaths,
171-
lambda path: f"{resource.jsonpath}.{jsonpath}" in path,
172-
),
178+
lambda resource: not _issues_at_property(resource, issues, jsonpath),
173179
)
174180

175181

0 commit comments

Comments
 (0)