Support ALL the schemas
This commit is contained in:
parent
b6c8126589
commit
087469f1f3
@ -136,7 +136,7 @@ setup(
|
|||||||
"publish": PublishCommand,
|
"publish": PublishCommand,
|
||||||
"publish_docs": PublishDocsCommand,
|
"publish_docs": PublishDocsCommand,
|
||||||
},
|
},
|
||||||
install_requires=["jsonschema", "mypy_extensions"],
|
install_requires=["jsonschema", "mypy_extensions", "stringcase"],
|
||||||
extras_require={
|
extras_require={
|
||||||
"dev": [
|
"dev": [
|
||||||
"bandit",
|
"bandit",
|
||||||
|
@ -6,6 +6,7 @@ from typing import Mapping
|
|||||||
|
|
||||||
from pkg_resources import resource_string
|
from pkg_resources import resource_string
|
||||||
import jsonschema
|
import jsonschema
|
||||||
|
from stringcase import snakecase
|
||||||
|
|
||||||
|
|
||||||
class _LocalRefResolver(jsonschema.RefResolver):
|
class _LocalRefResolver(jsonschema.RefResolver):
|
||||||
@ -13,19 +14,23 @@ class _LocalRefResolver(jsonschema.RefResolver):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize a new instance."""
|
"""Initialize a new instance."""
|
||||||
self.ref_to_file = {
|
|
||||||
"/addressSchema": "address_schema.json",
|
|
||||||
"/hexSchema": "hex_schema.json",
|
|
||||||
"/orderSchema": "order_schema.json",
|
|
||||||
"/wholeNumberSchema": "whole_number_schema.json",
|
|
||||||
"/ECSignature": "ec_signature_schema.json",
|
|
||||||
"/signedOrderSchema": "signed_order_schema.json",
|
|
||||||
"/ecSignatureParameterSchema": (
|
|
||||||
"ec_signature_parameter_schema.json" + ""
|
|
||||||
),
|
|
||||||
}
|
|
||||||
jsonschema.RefResolver.__init__(self, "", "")
|
jsonschema.RefResolver.__init__(self, "", "")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _ref_to_file(ref: str) -> str:
|
||||||
|
"""Translate a JSON schema ref to its corresponding file name.
|
||||||
|
|
||||||
|
>>> _LocalRefResolver._ref_to_file("/addressSchema")
|
||||||
|
'address_schema.json'
|
||||||
|
"""
|
||||||
|
_ref = ref.lstrip("/")
|
||||||
|
|
||||||
|
# handle weird special cases
|
||||||
|
_ref = _ref.replace("ECSignature", "EcSignature")
|
||||||
|
_ref = _ref.replace("Schema", "")
|
||||||
|
|
||||||
|
return f"{snakecase(_ref)}_schema.json"
|
||||||
|
|
||||||
def resolve_from_url(self, url: str) -> str:
|
def resolve_from_url(self, url: str) -> str:
|
||||||
"""Resolve the given URL.
|
"""Resolve the given URL.
|
||||||
|
|
||||||
@ -35,15 +40,11 @@ class _LocalRefResolver(jsonschema.RefResolver):
|
|||||||
`url` does not exist.
|
`url` does not exist.
|
||||||
"""
|
"""
|
||||||
ref = url.replace("file://", "")
|
ref = url.replace("file://", "")
|
||||||
if ref in self.ref_to_file:
|
return json.loads(
|
||||||
return json.loads(
|
resource_string(
|
||||||
resource_string(
|
"zero_ex.json_schemas",
|
||||||
"zero_ex.json_schemas", f"schemas/{self.ref_to_file[ref]}"
|
f"schemas/{_LocalRefResolver._ref_to_file(ref)}",
|
||||||
)
|
|
||||||
)
|
)
|
||||||
raise jsonschema.ValidationError(
|
|
||||||
f"Unknown ref '{ref}'. "
|
|
||||||
+ f"Known refs: {list(self.ref_to_file.keys())}."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
def snakecase(_: str):
|
||||||
|
...
|
Loading…
x
Reference in New Issue
Block a user