From 8a7261a797eb6b4bce65e79d7b0c7242246faaac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Mon, 7 Jun 2021 12:52:31 +0200 Subject: [PATCH] Validation: disable default by default, remove load, add doc --- README.md | 24 ++++++++++++++++++++++++ jsonschema_gentypes/cli.py | 16 ++++++++++------ jsonschema_gentypes/validate.py | 29 ++--------------------------- tests/test_validate.py | 16 +++++++--------- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 0e74b6fd..2440e152 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,30 @@ And just run: jsonschema-gentype ``` +# Validation + +This package also provide some validations features for YAML file based on `jsonschema`. + +Additional features: + +- Have the line and columns number in the errors, it the file is loaded with `ruamel.yaml`. +- Fill with the default provides in the JSON schema, disabled by default because it have some issue with `OneOf` and `AnyOf`. + +```python + import ruamel.yaml + import pkgutil + import jsonschema_gentypes.validate + + schema_data = pkgutil.get_data("jsonschema_gentypes", "schema.json") + with open(filename) as data_file: + yaml = ruamel.yaml.YAML() # type: ignore + data = yaml.load(data_file) + errors, data = jsonschema_gentypes.validate.validate(filename, data, schema, default) + if errors: + print("\n".join(errors)) + sys.exit(1) +``` + ## Limitations Requires Python 3.8 diff --git a/jsonschema_gentypes/cli.py b/jsonschema_gentypes/cli.py index 665c90a4..4179f6a3 100644 --- a/jsonschema_gentypes/cli.py +++ b/jsonschema_gentypes/cli.py @@ -11,9 +11,10 @@ import re import subprocess import sys -from typing import Dict, Set +from typing import Dict, Set, cast import requests +import ruamel.yaml from jsonschema import RefResolver import jsonschema_gentypes @@ -43,11 +44,14 @@ def main() -> None: else: schema_data = pkgutil.get_data("jsonschema_gentypes", "schema.json") assert schema_data - try: - config = validate.load(args.config, json.loads(schema_data)) - except validate.ValidationError as error: - LOG.error(error) - config = error.data + with open(args.config) as data_file: + yaml = ruamel.yaml.YAML() # type: ignore + data = yaml.load(data_file) + errors, data = validate.validate(args.config, data, json.loads(schema_data), True) + config = cast(configuration.Configuration, data) + + if errors: + LOG.error("The config file is invalid:\n%s", "\n".join(errors)) if not args.skip_config_errors: sys.exit(1) diff --git a/jsonschema_gentypes/validate.py b/jsonschema_gentypes/validate.py index d1fc6c58..80f0d7a9 100644 --- a/jsonschema_gentypes/validate.py +++ b/jsonschema_gentypes/validate.py @@ -7,7 +7,6 @@ from typing import Any, Dict, Iterator, List, Tuple import jsonschema -import ruamel.yaml import jsonschema_gentypes.jsonschema @@ -73,7 +72,7 @@ def set_defaults( def validate( - filename: str, data: Dict[str, Any], schema: Dict[str, Any], default: bool = True + filename: str, data: Dict[str, Any], schema: Dict[str, Any], default: bool = False ) -> Tuple[List[str], Dict[str, Any]]: """ Validate the YAML, with it's JSON schema. @@ -82,7 +81,7 @@ def validate( filename: Name used to generate the error messages data: The data structure to be validated (should be loaded with ruamel.yaml to have the lines numbers) schema: The loaded JSON schema - default: If true, fill the data with the defaults provided in the JSON schema + default: If true, fill the data with the defaults provided in the JSON schema, not working as expected with AnyOf and OneOf """ schema_ref = schema.get("$schema", "default") schema_match = re.match(r"https?\:\/\/json\-schema\.org\/(.*)\/schema", schema_ref) @@ -151,27 +150,3 @@ def __init__(self, message: str, data: Any) -> None: """ super().__init__(message) self.data = data - - -def load( - filename: str, - schema: Dict[str, Any], - default: bool = True, -) -> Any: - """ - Load and validate the YAML, with it's JSON schema. - - Arguments: - filename: Path of the file that should be loaded - schema: The JSON schema - default: Should fill the data with the default provided in the JSON schema - """ - with open(filename) as data_file: - yaml = ruamel.yaml.YAML() # type: ignore - data = yaml.load(data_file) - errors, data = validate(filename, data, schema, default) - - if errors: - raise ValidationError("The config file is invalid:\n{}".format("\n".join(errors)), data) - - return data diff --git a/tests/test_validate.py b/tests/test_validate.py index 3feacc11..a22ef287 100644 --- a/tests/test_validate.py +++ b/tests/test_validate.py @@ -4,7 +4,7 @@ import ruamel.yaml import yaml -from jsonschema_gentypes.validate import load, validate +from jsonschema_gentypes.validate import validate def test_validate_ruamel(): @@ -100,12 +100,10 @@ def test_validate_deep(): ] -def test_load_default(): - yaml_file = tempfile.NamedTemporaryFile(delete=False) - yaml_file.write("{}".encode()) - yaml_file.close() - data = load( - yaml_file.name, +def test_default(): + errors, data = validate( + "test.yaml", + {}, { "type": "object", "properties": { @@ -114,6 +112,6 @@ def test_load_default(): }, }, }, + default=True, ) - assert dict(data) == {"root": "abc"} - os.unlink(yaml_file.name) + assert data == {"root": "abc"}