Skip to content

Commit

Permalink
Reinstate format checker and add some related tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettviren committed Oct 16, 2023
1 parent 808c395 commit 203c273
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
8 changes: 4 additions & 4 deletions moo/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from jsonschema.exceptions import ValidationError

import jsonschema
# Deprecated:
# https://python-jsonschema.readthedocs.io/en/stable/api/jsonschema/protocols/#jsonschema.protocols.Validator.FORMAT_CHECKER
# format_checker = jsonschema.Draft7Validator.FORMAT_CHECKER

# without this, format="..." will be ignored.
format_checker = jsonschema.Draft7Validator.FORMAT_CHECKER



Expand Down Expand Up @@ -153,7 +153,7 @@ def make_validator_jsonschema():
from jsonschema import validate as js_validate
def validate(model, schema={}):
try:
return js_validate(instance=model, schema=schema)#,format_checker=format_checker)
return js_validate(instance=model, schema=schema,format_checker=format_checker)
except SchemaError as err:
raise ValidationError('invalid') from err
return validate
Expand Down
1 change: 1 addition & 0 deletions moo/ogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def wash_string(types, ost, *args, **kwds):
schema["format"] = ost["format"]
if "pattern" in ost:
schema["pattern"] = ost["pattern"]

try:
js_validate(instance=val, schema=schema,
format_checker=format_checker)
Expand Down
4 changes: 2 additions & 2 deletions moo/otypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ def update(self, val: str):
if ost["format"]:
schema["format"] = ost["format"]
from jsonschema import validate as js_validate
#from .jsonschema import format_checker
from .jsonschema import format_checker
from jsonschema.exceptions import ValidationError
try:
js_validate(instance=val, schema=schema) #,format_checker=format_checker)
js_validate(instance=val, schema=schema,format_checker=format_checker)
except ValidationError as verr:
raise ValueError(f'format mismatch for string {cname}') from verr
self._value = val
Expand Down
2 changes: 1 addition & 1 deletion moo/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This value is used in setup.py and available as moo.__version__ which
is exported to the CLI via "moo version".
'''
version = "0.6.3"
version = "0.6.7"
4 changes: 4 additions & 0 deletions test/test_moo_otypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def test_with_schema():
Person = types['app.Person']
Affiliation = types['app.Affiliation']

with pytest.raises(ValueError):
e = Email("this is not an email address and should fail")
print(f'EMAIL: {e} {repr(e)}')

p = Person()
with pytest.raises(ValueError):
p.email = "this should fail"
Expand Down
33 changes: 33 additions & 0 deletions test/test_validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from moo.ovalid import validate, ValidationError

def test_regex():
'Check validation method'

js = {"$schema": "http://json-schema.org/draft-07/schema#"}

validate("hello", dict(js, type="string"))
validate(1234, dict(js, type="number"))
validate("1234", dict(js, type="string", pattern="^[0-9]+$"))

validate("127.0.0.1", dict(js, type="string",
pattern = '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'))
with pytest.raises(ValidationError):
validate("& not @ ip", dict(js, type="string",
pattern = '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'))

validate("127.0.0.1", dict(js, type="string", format="ipv4"))
with pytest.raises(ValidationError):
validate("& not @ ip", dict(js, type="string", format="ipv4"))

# https://github.com/python-jsonschema/jsonschema/issues/403
with pytest.raises(ValidationError):
validate("1;2;3#4", dict(js, type="string", format="email"))

with pytest.raises(ValidationError):
validate(1234, dict(js, type="string"))

with pytest.raises(ValidationError):
validate("wrong", dict(js, type="number"))

0 comments on commit 203c273

Please sign in to comment.