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

[Backport master] Validation: disable default by default, remove load, add doc #35

Merged
merged 1 commit into from
Jun 9, 2021
Merged
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions jsonschema_gentypes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
29 changes: 2 additions & 27 deletions jsonschema_gentypes/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Any, Dict, Iterator, List, Tuple

import jsonschema
import ruamel.yaml

import jsonschema_gentypes.jsonschema

Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
16 changes: 7 additions & 9 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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": {
Expand All @@ -114,6 +112,6 @@ def test_load_default():
},
},
},
default=True,
)
assert dict(data) == {"root": "abc"}
os.unlink(yaml_file.name)
assert data == {"root": "abc"}