Skip to content

Commit

Permalink
Add option to raise errors (#68)
Browse files Browse the repository at this point in the history
* rename validate_once to validate

* add option to raise error when validation fails
  • Loading branch information
opethe1st authored Mar 7, 2020
1 parent 9a38632 commit 8f493db
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 65 deletions.
4 changes: 2 additions & 2 deletions performance/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import pytest

from performance.large_schema import SCHEMA
from pyjschema.draft_2019_09 import construct_validator, validate_once
from pyjschema.draft_2019_09 import construct_validator, validate

INSTANCE = [9, "hello", [1, "a", True], {"a": "a", "b": "b", "d": "d"}, 42, 3]


@pytest.mark.benchmark(group="compile+run", min_time=0.1, max_time=0.5, min_rounds=5)
def test_pyjschema(benchmark):
def validate_pyjschema():
validate_once(SCHEMA, INSTANCE)
validate(SCHEMA, INSTANCE)

benchmark(validate_pyjschema)

Expand Down
4 changes: 2 additions & 2 deletions src/pyjschema/draft_2019_09/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .validator_construction import (
Validator,
build_validator,
validate_once,
validate,
construct_validator,
)

__all__ = ["validate_once", "build_validator", "Validator", "construct_validator"]
__all__ = ["validate", "build_validator", "Validator", "construct_validator"]
4 changes: 2 additions & 2 deletions src/pyjschema/draft_2019_09/boolean_applicators_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class Test(unittest.TestCase):
def test(self):
res = validate_once(schema={"not": True}, instance="123")
res = validate(schema={"not": True}, instance="123")
self.assertFalse(res)
4 changes: 2 additions & 2 deletions src/pyjschema/draft_2019_09/types/array_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class Test(unittest.TestCase):
Expand All @@ -25,4 +25,4 @@ def test(self):
"items": [{"$ref": "#/$defs/item"},],
}

self.assertEqual(bool(validate_once(schema=schema, instance=instance)), False)
self.assertEqual(bool(validate(schema=schema, instance=instance)), False)
18 changes: 11 additions & 7 deletions src/pyjschema/draft_2019_09/validator_construction.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import json
import os
import typing

from pyjschema.exceptions import SchemaError
from pyjschema.exceptions import SchemaError, ValidationError

from .context import BUILD_VALIDATOR, VOCABULARIES
from .referencing import resolve_references
from .types import AcceptAll, RejectAll
from .validator import Validator
from .vocabularies import METASCHEMA_VALIDATORS, get_vocabularies

__all__ = ["validate_once", "Validator", "construct_validator"]
__all__ = ["validate", "Validator", "construct_validator"]


def construct_validator(schema, check_schema=False):
if check_schema:
schema_validator = meta_schema_validator(schema=schema)
# Need to wrap schema errors here and reraisr as SchemaErrors
if not schema_validator(instance=schema):
raise SchemaError("Schema is invalid according to the meta-schema")
raise SchemaError(message="Schema is invalid according to the meta-schema")
else:
validator = build_validator_and_resolve_references(
schema=schema,
Expand All @@ -28,16 +27,21 @@ def construct_validator(schema, check_schema=False):

def validate(instance):
# USE_SHORTCIRCUITING.set(True)
return bool(validator(instance=instance, location="/"))
return validator(instance=instance, location="/")

return validate


def validate_once(
schema: typing.Union[dict, bool], instance: dict, check_schema=False
def validate(
schema: typing.Union[dict, bool],
instance: dict,
check_schema=False,
raise_exceptions=False,
) -> bool:
validate = construct_validator(schema=schema, check_schema=check_schema)
res = validate(instance=instance)
if not res and raise_exceptions:
raise ValidationError(message="This instance doesnt conform to this schema")
return res


Expand Down
6 changes: 3 additions & 3 deletions src/pyjschema/draft_2019_09/validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pyjschema.exceptions import SchemaError

from .validator import Validator
from .validator_construction import validate_once
from .validator_construction import validate


class TestValidator(unittest.TestCase):
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_recursive_ref(self, instance, result):
}

self.assertEqual(
result, bool(validate_once(schema=schema, instance=instance)),
result, bool(validate(schema=schema, instance=instance)),
)

@parameterized.parameterized.expand(
Expand Down Expand Up @@ -76,5 +76,5 @@ def test_recursive_ref(self, instance, result):
}

self.assertEqual(
result, bool(validate_once(schema=schema, instance=instance)),
result, bool(validate(schema=schema, instance=instance)),
)
17 changes: 14 additions & 3 deletions src/pyjschema/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from typing import Optional


class SchemaError(Exception):
pass
def __init__(self, message, details: Optional[dict] = None):
super().__init__()
details = details if details else {}
self.message = message
self.details = details


class InstanceError(Exception):
pass
class ValidationError(Exception):
def __init__(self, message, details: Optional[dict] = None):
super().__init__()
details = details if details else {}
self.message = message
self.details = details


class ProgrammerError(Exception):
Expand Down
6 changes: 3 additions & 3 deletions tests/draft_2019_09/accept_all_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestTrue(unittest.TestCase):
Expand All @@ -17,7 +17,7 @@ class TestTrue(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
self.assertTrue(validate_once(schema=schema, instance=instance))
self.assertTrue(validate(schema=schema, instance=instance))

@parameterized.parameterized.expand(
[
Expand All @@ -38,4 +38,4 @@ def test_true(self, name, schema, instance):
]
)
def test_empty_schema(self, name, schema, instance):
self.assertTrue(validate_once(schema=schema, instance=instance))
self.assertTrue(validate(schema=schema, instance=instance))
6 changes: 3 additions & 3 deletions tests/draft_2019_09/boolean_applicators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestBooleanLogic(unittest.TestCase):
Expand Down Expand Up @@ -31,7 +31,7 @@ class TestBooleanLogic(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
res = validate_once(schema, instance)
res = validate(schema, instance)
self.assertTrue(res)

@parameterized.parameterized.expand(
Expand Down Expand Up @@ -59,5 +59,5 @@ def test_true(self, name, schema, instance):
]
)
def test_false(self, name, schema, instance):
res = validate_once(schema, instance)
res = validate(schema, instance)
self.assertFalse(res)
4 changes: 2 additions & 2 deletions tests/draft_2019_09/rand_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import unittest

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestRand(unittest.TestCase):
def test(self):
res = validate_once(
res = validate(
schema={
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/product.schema.json",
Expand Down
6 changes: 3 additions & 3 deletions tests/draft_2019_09/referencing/ref_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized # type: ignore

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestRefValidate(unittest.TestCase):
Expand Down Expand Up @@ -45,7 +45,7 @@ class TestRefValidate(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
result = validate_once(schema=schema, instance=instance)
result = validate(schema=schema, instance=instance)
self.assertTrue(result)

@parameterized.parameterized.expand(
Expand All @@ -69,5 +69,5 @@ def test_true(self, name, schema, instance):
]
)
def test_false(self, name, schema, instance):
result = validate_once(schema=schema, instance=instance)
result = validate(schema=schema, instance=instance)
self.assertFalse(result)
4 changes: 2 additions & 2 deletions tests/draft_2019_09/reject_all_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestRejectAll(unittest.TestCase):
Expand All @@ -17,4 +17,4 @@ class TestRejectAll(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
self.assertFalse(validate_once(schema=schema, instance=instance))
self.assertFalse(validate(schema=schema, instance=instance))
6 changes: 3 additions & 3 deletions tests/draft_2019_09/types/array_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestArrayValidation(unittest.TestCase):
Expand Down Expand Up @@ -136,7 +136,7 @@ class TestArrayValidation(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
res = validate_once(schema=schema, instance=instance)
res = validate(schema=schema, instance=instance)
self.assertTrue(res)

@parameterized.parameterized.expand(
Expand Down Expand Up @@ -252,4 +252,4 @@ def test_true(self, name, schema, instance):
]
)
def test_false(self, name, schema, instance):
self.assertFalse(validate_once(schema=schema, instance=instance))
self.assertFalse(validate(schema=schema, instance=instance))
6 changes: 3 additions & 3 deletions tests/draft_2019_09/types/boolean_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestBoolean(unittest.TestCase):
Expand All @@ -13,10 +13,10 @@ class TestBoolean(unittest.TestCase):
]
)
def test_instance_true(self, name, schema, instance):
self.assertTrue(validate_once(schema=schema, instance=instance))
self.assertTrue(validate(schema=schema, instance=instance))

def test_instance_not_boolean(self):
res = validate_once(
res = validate(
schema={"$id": "https://example.com/ope", "type": "boolean"}, instance=123
)
self.assertFalse(res)
6 changes: 3 additions & 3 deletions tests/draft_2019_09/types/number_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestNumber(unittest.TestCase):
Expand Down Expand Up @@ -46,7 +46,7 @@ class TestNumber(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
self.assertTrue(validate_once(schema=schema, instance=instance))
self.assertTrue(validate(schema=schema, instance=instance))

@parameterized.parameterized.expand(
[
Expand Down Expand Up @@ -92,4 +92,4 @@ def test_true(self, name, schema, instance):
]
)
def test_false(self, name, schema, instance):
self.assertFalse(validate_once(schema=schema, instance=instance))
self.assertFalse(validate(schema=schema, instance=instance))
6 changes: 3 additions & 3 deletions tests/draft_2019_09/types/object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import parameterized

from pyjschema.draft_2019_09 import validate_once
from pyjschema.draft_2019_09 import validate


class TestObject(unittest.TestCase):
Expand Down Expand Up @@ -117,7 +117,7 @@ class TestObject(unittest.TestCase):
]
)
def test_true(self, name, schema, instance):
self.assertTrue(validate_once(schema=schema, instance=instance))
self.assertTrue(validate(schema=schema, instance=instance))

@parameterized.parameterized.expand(
[
Expand Down Expand Up @@ -225,4 +225,4 @@ def test_true(self, name, schema, instance):
]
)
def test_false(self, name, schema, instance):
self.assertFalse(validate_once(schema=schema, instance=instance))
self.assertFalse(validate(schema=schema, instance=instance))
Loading

0 comments on commit 8f493db

Please sign in to comment.