-
Notifications
You must be signed in to change notification settings - Fork 512
feat(project): add pydantic validator for common_id field #5974
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -452,11 +452,15 @@ class App(models.CraftBaseModel): | |
| common_id: str | None = pydantic.Field( | ||
| default=None, | ||
| description="The identifier to a desktop ID within an external appstream file.", | ||
| examples=["org.canonical.foo"], | ||
| examples=["org.canonical.foo", "org.kde.ghostwriter", "io.github.user.project"], | ||
| ) | ||
| """The identifier to a desktop ID within an external appstream file. | ||
| """The unique identifier for a project. | ||
|
|
||
| See :ref:`how-to-configure-package-information` for details. | ||
| The common-id must be in reverse DNS format and consist only of alphanumeric characters and the | ||
| following special characters: ``., _, -``. | ||
|
|
||
| See :ref:`how-to-configure-package-information` and the `Appstream specification | ||
| <https://www.freedesktop.org/software/appstream/docs/sect-Metadata-Application.html#tag-id-desktopapp>`_ for more information. | ||
| """ | ||
|
|
||
| bus_name: str | None = pydantic.Field( | ||
|
|
@@ -1024,6 +1028,20 @@ def _validate_extensions(cls, extensions: list[str]) -> list[str]: | |
| ) | ||
| return extensions | ||
|
|
||
| @pydantic.field_validator("common_id") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lengau, please correct me here, but I believe the preferred way to add new regex validation is like this: |
||
| @classmethod | ||
| def _validate_common_id(cls, common_id: str) -> str: | ||
| common_id_re = re.compile(r"^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$") | ||
| if not common_id_re.match(common_id): | ||
| raise ValueError( | ||
| f"{common_id!r} is not a valid common id. Common IDs must be" | ||
| " reverse DNS ids. Checkout the freedesktop specs for more info" | ||
| " https://www.freedesktop.org/software/appstream/docs/" | ||
| "sect-Metadata-Application.html#tag-id-desktopapp" | ||
| ) | ||
|
|
||
| return common_id | ||
|
|
||
| @pydantic.field_validator("success_exit_status") | ||
| @classmethod | ||
| def _validate_success_exit_status( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1014,11 +1014,19 @@ def test_app_autostart(self, autostart, app_yaml_data): | |
| with pytest.raises(pydantic.ValidationError, match=error): | ||
| Project.unmarshal(data) | ||
|
|
||
| def test_app_common_id(self, app_yaml_data): | ||
| data = app_yaml_data(common_id="test-common-id") | ||
| def test_app_valid_common_id(self, app_yaml_data): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To exercise the regex, can you do a And can you add id's for the invalid ones to explain them? (for example, |
||
| data = app_yaml_data(common_id="org.test.app") | ||
| project = Project.unmarshal(data) | ||
| assert project.apps is not None | ||
| assert project.apps["app1"].common_id == "test-common-id" | ||
| assert project.apps["app1"].common_id == "org.test.app" | ||
|
|
||
| def test_app_invalid_common_id(self, app_yaml_data): | ||
| data = app_yaml_data(common_id="_invalid") | ||
| error = ( | ||
| "apps.app1.common_id\n Value error, '_invalid' is not a valid common id" | ||
| ) | ||
| with pytest.raises(pydantic.ValidationError, match=error): | ||
| Project.unmarshal(data) | ||
|
|
||
| def test_app_completer(self, app_yaml_data): | ||
| data = app_yaml_data(completer="test-completer") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.