Skip to content

Commit

Permalink
Protect against old jsonschema (eg 3.2.0) that lack string format con…
Browse files Browse the repository at this point in the history
…straints
  • Loading branch information
brettviren committed Oct 16, 2023
1 parent 203c273 commit c3f62bd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
12 changes: 8 additions & 4 deletions moo/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

import jsonschema

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


# Without this, format="..." will be ignored.
# Older versions of jsonschema (eg 3.2.0) lack it.
try:
format_checker = jsonschema.Draft7Validator.FORMAT_CHECKER
except AttributeError:
import sys
sys.stderr.write("WARNING: your jsonschema is old. string format constraints will be ignored")
format_checker = None

def ref(oschema):
'''
Expand Down
7 changes: 2 additions & 5 deletions moo/ogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
from moo.modutil import resolve, module_at
from collections.abc import Mapping

from jsonschema import validate as js_validate
from jsonschema.exceptions import ValidationError
from .jsonschema import format_checker
from .jsonschema import validate, ValidationError

class BaseType:
pass
Expand Down Expand Up @@ -100,8 +98,7 @@ def wash_string(types, ost, *args, **kwds):
schema["pattern"] = ost["pattern"]

try:
js_validate(instance=val, schema=schema,
format_checker=format_checker)
validate(val, schema)
except ValidationError as verr:
raise ValueError(f'illegal string format {name} value: {val}') from verr
return val
Expand Down
8 changes: 1 addition & 7 deletions moo/oschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@


import numpy
import jsonschema


def validate(value, schema):
jsonschema.validate(value, schema,
format_checker=jsonschema.draft7_format_checker)
return value
from .jsonschema import validate, ValidationError


# this holds all known types
Expand Down
6 changes: 2 additions & 4 deletions moo/otypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from importlib import import_module
from moo.modutil import module_at
from moo.io import load as load_file
from .jsonschema import validate, ValidationError


def get_deps(deps=None, **ost):
Expand Down Expand Up @@ -373,11 +374,8 @@ def update(self, val: str):
schema["pattern"] = ost["pattern"]
if ost["format"]:
schema["format"] = ost["format"]
from jsonschema import validate as js_validate
from .jsonschema import format_checker
from jsonschema.exceptions import ValidationError
try:
js_validate(instance=val, schema=schema,format_checker=format_checker)
validate(val, schema)
except ValidationError as verr:
raise ValueError(f'format mismatch for string {cname}') from verr
self._value = val
Expand Down
7 changes: 4 additions & 3 deletions test/test_moo_ogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import moo
from moo.ogen import TypeBuilder

from moo.jsonschema import format_checker

def test_type_builder():
sc = [
Expand Down Expand Up @@ -50,8 +50,9 @@ def test_type_builder():
Age(Height(177.8))

from a.b import Email
with pytest.raises(ValueError):
Email("not-an-email-address")
if format_checker:
with pytest.raises(ValueError):
Email("not-an-email-address")

from a.b import Fruit
assert "apple" == Fruit("apple").pod()
Expand Down
14 changes: 8 additions & 6 deletions test/test_moo_otypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import moo
import pytest

from moo.jsonschema import format_checker

def test_junk():
'Test various stuff about moo.otypes'
Expand Down Expand Up @@ -57,13 +57,15 @@ 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)}')
if format_checker:
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"
if format_checker:
with pytest.raises(ValueError):
p.email = "this should fail"
with pytest.raises(AttributeError):
p.email
p.email = "brett.viren@gmail.com"
Expand Down
12 changes: 7 additions & 5 deletions test/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from moo.jsonschema import format_checker
from moo.ovalid import validate, ValidationError

def test_regex():
Expand All @@ -18,12 +19,13 @@ def test_regex():
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"))
if format_checker:
with pytest.raises(ValidationError):
validate("& not @ ip", dict(js, type="string", format="ipv4"))

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"))
Expand Down

0 comments on commit c3f62bd

Please sign in to comment.