-
Notifications
You must be signed in to change notification settings - Fork 0
feat: validate plugin config schema #22
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| { | ||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||
| "$id": "https://github.com/joy7758/persona-object-protocol/plugin_config.schema.json", | ||
| "title": "POP Plugin Discovery Config", | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "$schema": { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| }, | ||
| "plugin_paths": { | ||
| "type": "array", | ||
| "items": { | ||
| "oneOf": [ | ||
| { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| }, | ||
| { | ||
| "type": "object", | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "path": { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| }, | ||
| "package_name": { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| } | ||
| }, | ||
| "required": [ | ||
| "path" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "plugin_packages": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| } | ||
| }, | ||
| "plugin_package_paths": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string", | ||
| "minLength": 1 | ||
| } | ||
| } | ||
| } | ||
| } |
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,59 @@ | ||
| #!/usr/bin/env python3 | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from jsonschema import Draft202012Validator | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
| DEFAULT_CONFIG_PATH = ROOT / "plugin_config.example.json" | ||
| DEFAULT_SCHEMA_PATH = ROOT / "plugin_config.schema.json" | ||
|
|
||
|
|
||
| def load_json(path: Path) -> Any: | ||
| return json.loads(path.read_text(encoding="utf-8")) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| description="Validate a plugin config file against the repository schema." | ||
| ) | ||
| parser.add_argument( | ||
| "config_path", | ||
| nargs="?", | ||
| type=Path, | ||
| default=DEFAULT_CONFIG_PATH, | ||
| help="Path to the plugin config file to validate.", | ||
| ) | ||
| parser.add_argument( | ||
| "--schema", | ||
| type=Path, | ||
| default=DEFAULT_SCHEMA_PATH, | ||
| help="Path to the plugin config schema file.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| config_path = args.config_path.resolve() | ||
| schema_path = args.schema.resolve() | ||
| schema = load_json(schema_path) | ||
| payload = load_json(config_path) | ||
|
|
||
| validator = Draft202012Validator(schema) | ||
| errors = sorted( | ||
| validator.iter_errors(payload), | ||
| key=lambda error: [str(part) for part in error.absolute_path], | ||
| ) | ||
| if errors: | ||
| first_error = errors[0] | ||
| path = ".".join(str(part) for part in first_error.absolute_path) or "<root>" | ||
| raise ValueError(f"{config_path}: invalid at {path}: {first_error.message}") | ||
|
|
||
| print(f"Validated {config_path.relative_to(ROOT)} against {schema_path.relative_to(ROOT)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
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.
This unconditional call introduces a runtime regression for plugin discovery: any user with a
plugin_config.json(orPOP_PLUGIN_CONFIG_FILE) now fails immediately ifjsonschemais not installed, even thoughjsonschemais only an optional extra dependency. In a default environment, this blocks all file-based plugin loading paths that previously worked, so discovery should either gracefully skip schema validation when the extra is missing or make the dependency mandatory for this workflow.Useful? React with 👍 / 👎.