Skip to content

Commit

Permalink
Validation: disable default by default, remove load, add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jun 8, 2021
1 parent ba2e1a9 commit 8a7261a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
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"}

0 comments on commit 8a7261a

Please sign in to comment.