Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Schema for a simple JSON is not supported #58

Open
haizhouW opened this issue Mar 16, 2020 · 1 comment
Open

JSON Schema for a simple JSON is not supported #58

haizhouW opened this issue Mar 16, 2020 · 1 comment

Comments

@haizhouW
Copy link

Hello,

If a JSON Schema is for a simple JSON object (string/number/integer), warlock will fail to bind and we can't instantiate object.

For example, given the following JSON Schema:

schema={
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example",
  "type": "string",
  "enum": ['abc', 'xyz']
}

warlock will fail to create an object

>>> import warlock
>>> Example=warlock.model_factory(schema)
>>> Example('xyz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/warlock/core.py", line 35, in __init__
    base_class.__init__(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/warlock/model.py", line 30, in __init__
    d = dict(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

or

>>> Example(_='xyz')
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/warlock/model.py", line 140, in validate
    jsonschema.validate(obj, self.schema)
  File "/usr/local/lib/python3.6/site-packages/jsonschema/validators.py", line 934, in validate
    raise error
jsonschema.exceptions.ValidationError: {'_': 'xyz'} is not of type 'string'

Failed validating 'type' in schema:
    {'$schema': 'http://json-schema.org/draft-04/schema#',
     'enum': ['abc', 'xyz'],
     'title': 'Example',
     'type': 'string'}

On instance:
    {'_': 'xyz'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/warlock/model.py", line 33, in __init__
    self.validate(d)
  File "/usr/local/lib/python3.6/site-packages/warlock/model.py", line 142, in validate
    raise exceptions.ValidationError(str(exc))
warlock.exceptions.ValidationError: {'_': 'xyz'} is not of type 'string'

Failed validating 'type' in schema:
    {'$schema': 'http://json-schema.org/draft-04/schema#',
     'enum': ['abc', 'xyz'],
     'title': 'Example',
     'type': 'string'}

On instance:
    {'_': 'xyz'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/warlock/core.py", line 35, in __init__
    base_class.__init__(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/warlock/model.py", line 35, in __init__
    raise ValueError(str(exc))
ValueError: {'_': 'xyz'} is not of type 'string'

Failed validating 'type' in schema:
    {'$schema': 'http://json-schema.org/draft-04/schema#',
     'enum': ['abc', 'xyz'],
     'title': 'Example',
     'type': 'string'}

On instance:
    {'_': 'xyz'}
@louloizides
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants