-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 🚚 Rule to CustomCheck
#133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cff7796
refactor: :truck: `Rule` -> `CustomCheck`
signekb 23daa98
refactor: :truck: rename file from rule to custom_check; fix imports
signekb 8eb6656
docs: :pencil2: fix arg in docstring example`rules` -> `custom_checks`
signekb 19a0692
style: :art: format `qmd`
signekb b0c8091
chore: :arrow_up: update `uv.lock` with `just run-all`
signekb 3e59f17
test: :white_check_mark: `rule` -> `custom_check`
signekb 5058296
docs: :memo: `rules` -> `custom_checks` in docstring
signekb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Any, Callable | ||
|
|
||
| from check_datapackage.internals import ( | ||
| _filter, | ||
| _flat_map, | ||
| _get_fields_at_jsonpath, | ||
| _map, | ||
| ) | ||
| from check_datapackage.issue import Issue | ||
|
|
||
|
|
||
| @dataclass | ||
| class CustomCheck: | ||
| """A custom check to be done on a Data Package descriptor. | ||
|
|
||
| Attributes: | ||
| jsonpath (str): The location of the field or fields the custom check applies to, | ||
| expressed in [JSON path](https://jg-rp.github.io/python-jsonpath/syntax/) | ||
| notation (e.g., `$.resources[*].name`). | ||
| message (str): The message shown when the check is violated. | ||
| check (Callable[[Any], bool]): A function that expresses the custom check. | ||
| It takes the value at the `jsonpath` location as input and | ||
| returns true if the check is met, false if it isn't. | ||
| type (str): An identifier for the custom check. It will be shown in error | ||
| messages and can be used to exclude the check. Each custom check | ||
| should have a unique `type`. | ||
|
|
||
| Examples: | ||
| ```{python} | ||
| import check_datapackage as cdp | ||
|
|
||
| license_check = cdp.CustomCheck( | ||
| type="only-mit", | ||
| jsonpath="$.licenses[*].name", | ||
| message="Data Packages may only be licensed under MIT.", | ||
| check=lambda license_name: license_name == "mit", | ||
| ) | ||
| ``` | ||
| """ | ||
|
|
||
| jsonpath: str | ||
| message: str | ||
| check: Callable[[Any], bool] | ||
| type: str = "custom" | ||
|
|
||
|
|
||
| def apply_custom_checks( | ||
| custom_checks: list[CustomCheck], descriptor: dict[str, Any] | ||
| ) -> list[Issue]: | ||
| """Checks the descriptor for all custom checks and creates issues if any fail. | ||
|
|
||
| Args: | ||
| custom_checks: The custom checks to apply to the descriptor. | ||
| descriptor: The descriptor to check. | ||
|
|
||
| Returns: | ||
| A list of `Issue`s. | ||
| """ | ||
| return _flat_map( | ||
| custom_checks, | ||
| lambda custom_check: _apply_custom_check(custom_check, descriptor), | ||
| ) | ||
|
|
||
|
|
||
| def _apply_custom_check( | ||
| custom_check: CustomCheck, descriptor: dict[str, Any] | ||
| ) -> list[Issue]: | ||
| """Applies the custom check to the descriptor. | ||
|
|
||
| If any fields fail the custom check, this function creates a list of issues | ||
| for those fields. | ||
|
|
||
| Args: | ||
| custom_check: The custom check to apply to the descriptor. | ||
| descriptor: The descriptor to check. | ||
|
|
||
| Returns: | ||
| A list of `Issue`s. | ||
| """ | ||
| matching_fields = _get_fields_at_jsonpath(custom_check.jsonpath, descriptor) | ||
| failed_fields = _filter( | ||
| matching_fields, lambda field: not custom_check.check(field.value) | ||
| ) | ||
| return _map( | ||
| failed_fields, | ||
| lambda field: Issue( | ||
| jsonpath=field.jsonpath, | ||
| type=custom_check.type, | ||
| message=custom_check.message, | ||
| ), | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.