Skip to content

JSON Schema for a simple JSON is not supported #58

Description

@haizhouW

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'}

Activity

  1. louloizides commented on Dec 1, 2020

    @louloizides
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions