Repository navigation
feat(flagd-core): validate flag configs against the flagd JSON schema #471
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
Open
SpandanM110
wants to merge
2
commits into
open-feature:main
Choose a base branch
from
SpandanM110:feat/flagd-core-schema-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
54 changes: 54 additions & 0 deletions
54
tools/openfeature-flagd-core/src/openfeature/contrib/tools/flagd/core/schema.py
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,54 @@ | ||
| """Validation of flag configurations against the bundled flagd JSON schema. | ||
|
|
||
| The schema files in ``schemas/`` are copies of ``json/flags.json`` and | ||
| ``json/targeting.json`` from https://github.com/open-feature/schemas, which the | ||
| flagd provider already checks out as a git submodule. Run ``poe sync-schemas`` | ||
| to refresh them after updating that submodule. | ||
| """ | ||
|
|
||
| import json | ||
| import typing | ||
| from collections.abc import Mapping | ||
| from functools import cache | ||
| from importlib.resources import files | ||
|
|
||
| from jsonschema import Draft7Validator | ||
| from referencing import Registry, Resource | ||
|
|
||
| _SCHEMA_FILES = ("flags.json", "targeting.json") | ||
| _FLAGS_SCHEMA_ID = "https://flagd.dev/schema/v0/flags.json" | ||
|
|
||
|
|
||
| @cache | ||
| def _validator() -> Draft7Validator: | ||
| schema_dir = files(__package__).joinpath("schemas") | ||
| schemas = [ | ||
| json.loads(schema_dir.joinpath(name).read_text(encoding="utf-8")) | ||
| for name in _SCHEMA_FILES | ||
| ] | ||
| # flags.json references targeting.json by a relative "$ref", which resolves | ||
| # against the "$id" of flags.json. flags.json also has a top-level "$ref", | ||
| # and draft-07 ignores keywords next to "$ref", including "$id". So each | ||
| # schema is registered under the "$id" read from the file, and validation | ||
| # starts from a "$ref" to that URI, giving relative references a base. | ||
| registry: Registry[typing.Any] = Registry().with_resources( | ||
| (schema["$id"], Resource.from_contents(schema)) for schema in schemas | ||
| ) | ||
| return Draft7Validator({"$ref": _FLAGS_SCHEMA_ID}, registry=registry) | ||
|
|
||
|
|
||
| def validate_flag_configuration(configuration: Mapping[str, typing.Any]) -> list[str]: | ||
| """Validate a flag configuration against the flagd JSON schema. | ||
|
|
||
| Returns a description of every schema violation, or an empty list if the | ||
| configuration is valid. Each description names the location and the failed | ||
| schema keyword only: ``jsonschema`` messages embed the offending instance, | ||
| which could leak variant values or targeting literals into logs. | ||
| """ | ||
| errors = sorted( | ||
| _validator().iter_errors(configuration), key=lambda error: error.json_path | ||
| ) | ||
| return [ | ||
| f"{error.json_path}: does not match the schema ('{error.validator}')" | ||
| for error in errors | ||
| ] |
Oops, something went wrong.
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.