Skip to content
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

Use fastjsonschema to validate user configuration #6110

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions aiida/manage/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ def _backup(cls, filepath):
@staticmethod
def validate(config: dict, filepath: Optional[str] = None):
"""Validate a configuration dictionary."""
import jsonschema
from .schema.fastjsonschema_validate_v9 import JsonSchemaValueException, validate

try:
jsonschema.validate(instance=config, schema=config_schema())
except jsonschema.ValidationError as error:
raise ConfigValidationError(
message=error.message, keypath=error.path, schema=error.schema, filepath=filepath
)
validate(data=config)
except JsonSchemaValueException as error:
raise ConfigValidationError(message=error.message, keypath=error.path, schema=None, filepath=filepath)

def __init__(self, filepath: str, config: dict, validate: bool = True):
"""Instantiate a configuration object from a configuration dictionary and its filepath.
Expand Down
8 changes: 4 additions & 4 deletions aiida/manage/configuration/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def validate(self, value: Any, cast: bool = True) -> Any:

"""
# pylint: disable=too-many-branches
import jsonschema
import fastjsonschema

from aiida.manage.caching import _validate_identifier_pattern

Expand Down Expand Up @@ -90,9 +90,9 @@ def validate(self, value: Any, cast: bool = True) -> Any:
pass

try:
jsonschema.validate(instance=value, schema=self.schema)
except jsonschema.ValidationError as exc:
raise ConfigValidationError(message=exc.message, keypath=[self.name, *(exc.path or [])], schema=exc.schema)
fastjsonschema.validate(data=value, definition=self.schema)
except fastjsonschema.JsonSchemaException as exc:
raise ConfigValidationError(message=exc.message, keypath=[self.name, *(exc.path or [])], schema=self.schema) # pylint: disable=no-member

# special caching validation
if self.name in ('caching.enabled_for', 'caching.disabled_for'):
Expand Down
4,502 changes: 4,502 additions & 0 deletions aiida/manage/configuration/schema/fastjsonschema_validate_v9.py

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions docs/source/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ py:class CircusClient
py:class pgsu.PGSU
py:meth pgsu.PGSU.__init__

py:class jsonschema.exceptions._Error

py:class Session
py:class Query
py:class importlib_metadata.EntryPoint
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ dependencies:
- click~=8.1
- disk-objectstore~=0.6.0
- docstring_parser
- python-fastjsonschema~=2.17
- get-annotations~=0.1
- python-graphviz~=0.19
- ipython>=7
- jinja2~=3.0
- jsonschema~=3.0
- kiwipy[rmq]~=0.7.7
- importlib-metadata~=6.0
- numpy~=1.21
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ dependencies = [
"click~=8.1",
"disk-objectstore~=0.6.0",
"docstring-parser",
"fastjsonschema~=2.17",
"get-annotations~=0.1;python_version<'3.10'",
"graphviz~=0.19",
"ipython>=7",
"jinja2~=3.0",
"jsonschema~=3.0",
"kiwipy[rmq]~=0.7.7",
"importlib-metadata~=6.0",
"numpy~=1.21",
Expand Down Expand Up @@ -410,13 +410,13 @@ module = [
'click_config_file.*',
'click_spinner.*',
'docutils.*',
'fastjsonschema.*',
'flask_cors.*',
'flask_restful.*',
'get_annotations.*',
'graphviz.*',
'importlib._bootstrap.*',
'IPython.*',
'jsonschema.*',
'kiwipy.*',
'matplotlib.*',
'mayavi.*',
Expand Down Expand Up @@ -452,6 +452,10 @@ ignore_missing_imports = true
module = 'plumpy'
ignore_errors = true

[[tool.mypy.overrides]]
module = 'aiida.manage.configuration.schema.fastjsonschema_validate_v9'
ignore_errors = true

[tool.tox]
legacy_tox_ini = """
[tox]
Expand Down
1 change: 0 additions & 1 deletion requirements/requirements-py-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ ipywidgets==8.0.6
itsdangerous==2.1.2
jedi==0.18.2
jinja2==3.1.2
jsonschema[format-nongpl]==3.2.0
jupyter==1.0.0
jupyter-cache==0.6.1
jupyter-client==8.2.0
Expand Down
1 change: 0 additions & 1 deletion requirements/requirements-py-3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ ipywidgets==8.0.6
itsdangerous==2.1.2
jedi==0.18.2
jinja2==3.1.2
jsonschema[format-nongpl]==3.2.0
jupyter==1.0.0
jupyter-cache==0.6.1
jupyter-client==8.2.0
Expand Down
1 change: 0 additions & 1 deletion requirements/requirements-py-3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ ipywidgets==8.0.6
itsdangerous==2.1.2
jedi==0.18.2
jinja2==3.1.2
jsonschema[format-nongpl]==3.2.0
jupyter==1.0.0
jupyter-cache==0.6.1
jupyter-client==8.2.0
Expand Down
1 change: 1 addition & 0 deletions utils/dependency_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ROOT = Path(__file__).resolve().parent.parent # repository root

SETUPTOOLS_CONDA_MAPPINGS = {
'fastjsonschema': 'python-fastjsonschema',
'graphviz': 'python-graphviz',
'docstring-parser': 'docstring_parser',
}
Expand Down