Code Monkey home page Code Monkey logo

json-spec's People

Contributors

dm-xba avatar drawingboardmedia avatar edhiley avatar ismtabo avatar johnnoone avatar misja avatar rdowne avatar sharkberto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

json-spec's Issues

dependency problem

json-spec 0.11.0 requires importlib-metadata<6.0.0,>=5.0.0, but you have importlib-metadata 7.1.0 which is incompatible.
opentelemetry-api 1.24.0 requires importlib-metadata<=7.0,>=6.0, but you have importlib-metadata 7.1.0 which is incompatible.

type error when using oneOf

I have the following schema:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "props": {
            "type": "array",
            "items": {
                "oneOf": [
                    {"type": "string"},
                    {"type": "number"}
                ]
            }
        }
    }
}

Validating the following documents raises a ValidationError('Wrong type', 'hello') error:

doc = {
    "props": ["hello"]
}

This is due to validate_type marking the value "hello" as not a type number during the recursion, but am not able to trace why this error is bubbling up since validate_one_of already validated the condition as true. Any ideas?

PkgProvider not fully implemented

PkgProvider isn't fully implemented, missing __iter__ and __len__, which makes it impossible to pass to the validator Factory. My question is if this was done on purpose, encouraging to subclass and implement or if a pull request would be appreciated?

Deprecation warnings when importing from collections

There are a few places where importing from collections as opposed to collections.abc raises the following deprecation warning in Python3

DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working

This can be fixed using the import style in json-spec/src/reference/util.py.

I will soon raise a PR using this import style everywhere.

pypi metadata sets python 3.10 as a requirement

I'm using json-spec with Python 3.8 and would like to the package to 0.11.0 (to get rid of collections.abc warnings; see also #26). Although the documentation (and the .travis file) indicate that Python versions as low as 3.6 are supported, the pypi metadata for the package places a requirement of 3.10. This prevents pip from upgrading the package.

It appears that the source of this metadata is the pyproject.toml file (at line 63):

[tool.poetry.dependencies]
python = "^3.10"

Is 3.10 truly required? If so, we can update the documentation, but hopefully just the pyproject.toml file can be updated to reduce this requirement.

Unable to validate via cli

I am trying to validate a file against a schema as follows:

$ jsonschema -i /path/to/schema.json test.json

It fails with this message:

usage: jsonschema [-h] [-i INSTANCES] [-F ERROR_FORMAT] [-V VALIDATOR] schema
jsonschema: error: argument -i/--instance: invalid _json_file value: '/path/to/schema.json'

However, when validating the schema itself in an online json validator, e.g. this one, it succeeds.

Recursion error

I was looking at validating hyper-schema documents and figured I could use the draft4 compiler since hyper-schema is just an extension. It fails however, with a maximum recursion depth exceeded error, this was my approach for a simple test -

from jsonspec.validators import load, Factory
from jsonspec.reference.providers import SpecProvider

data = {
    "$schema": "http://json-schema.org/draft-04/hyper-schema#",
    "id": "http://my.com/customer#",
    "type": "object",
    "properties": {
        "name": {
            "description": "Name of the customer",
            "type": "string",
            "pattern": "[a-zA-Z\\s]+"
        }
    },
    "required": ["name"],
    "links": [
        {
            "title": "Create",
            "description": "Create a customer.",
            "href": "/customers",
            "method": "POST",
            "rel": "create",
            "schema": {
                "$ref": "#"
            },
            "targetSchema": {
                "$ref": "#"
            }
        }
    ]
}

Factory.compilers['http://json-schema.org/draft-04/hyper-schema#'] = \
    Factory.compilers['http://json-schema.org/draft-04/schema#']

provider = SpecProvider()
validator = load(provider['draft-04/hyper-schema'], provider=provider)

validator.validate(data)

Schema files missing in distribution

I didn't notice this earlier but it appears schema files are missing from the distributed pypi files. 0.9.11 still seems to be ok but e.g. the 0.9.15 wheel doesn't include them. The source package does, but they apparently do not get installed - somewhere the setup changed which causes it to miss the includes in the manifest?

Add details to Missing Property error

Please consider the patch below. In reporting a missing property during validation, this include the name of the property in the ValidationError's reason.

(Sorry for the inline code. I haven't figured out how to properly submit a patch. The github's issues editor won't let me attach a *.patch file to this post, and other options I tried get rejected because I don't have permission to contribute.)

From 42c4a75835ccb98907c75b7611f550774e3e245f Mon Sep 17 00:00:00 2001
From: Greg Bullock <[email protected]>
Date: Wed, 20 Apr 2016 15:30:37 -0700
Subject: [PATCH] Add details to Missing Property error

In reporting a missing property during validation, include the name of
the property in the ValidationError's reason.

---
 src/jsonspec/validators/draft04.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/jsonspec/validators/draft04.py b/src/jsonspec/validators/draft04.py
index adb6ee9..f6d67bb 100644
--- a/src/jsonspec/validators/draft04.py
+++ b/src/jsonspec/validators/draft04.py
@@ -380,7 +380,7 @@ class Draft04Validator(Validator):
             if key in obj:
                 if isinstance(dependencies, sequence_types):
                     for dep in set(dependencies) - set(obj.keys()):
-                        self.fail('Missing property', obj, pointer)
+                        self.fail('Missing property "{}"'.format(dep), obj, pointer)
                 else:
                     dependencies(obj)
         return obj
@@ -609,7 +609,7 @@ class Draft04Validator(Validator):
         if 'required' in self.attrs:
             for name in self.attrs['required']:
                 if name not in obj:
-                    self.fail('Missing property', obj, pointer)
+                    self.fail('Missing property "{}"'.format(name), obj, pointer)
         return obj

     def validate_type(self, obj, pointer=None):
-- 
2.7.1.windows.2


Support for schema draft-07

I have just started to look at JSON Schema and the current line seems to be causing some issue with the tool.

"$schema": "http://json-schema.org/draft-07/schema#",

Error message:

jsonspec.validators.exceptions.CompilationError: ("u'http://json-schema.org/draft-07/schema#' not registered"

Changing this to draft-04 will solve the error, but then I'll get a bunch of warnings because I am using "format" values that are not supported yet.

Is there a plan to support draft-07?
I am currently using version 0.9.11, which I got from pip install.

Thanks!

Complex schema validation broken on Windows - Wrong os.path use?

Hi,

It get the following error when trying to validate a schema with references on Windows:

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    "more": 2
  File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 320, in validate
    obj = validator.validate_properties(obj, pointer)
  File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 577, in validate_properties
    obj[name] = validator(obj[name], pointer_join(pointer, name))  # noqa
  File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 57, in __call__
    return self.validate(obj, pointer)
  File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 104, in validate
    return self.validator.validate(obj, pointer)
  File "C:\(...)\lib\site-packages\jsonspec\validators\bases.py", line 84, in validator
    self._validator = self.context.resolve(self.pointer)
  File "C:\(...)\lib\site-packages\jsonspec\validators\factorize.py", line 45, in resolve
    self.spec)
  File "C:\(...)\lib\site-packages\jsonspec\validators\factorize.py", line 96, in local
    return compiler(schema, pointer, context)
  File "C:\(...)\lib\site-packages\jsonspec\validators\draft04.py", line 69, in compile
    subpointer = os.path.join(pointer, 'additionalProperties')
  File "C:\(...)\lib\ntpath.py", line 65, in join
    result_drive, result_path = splitdrive(path)
  File "C:\(...)\lib\ntpath.py", line 115, in splitdrive
    if len(p) > 1:
TypeError: object of type 'DocumentPointer' has no len()

You can reproduce the problem with the following sample code:

from jsonspec.validators import load

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "definitions": {
        "test": {
            "type": "object",
            "properties": {
                "foo": {"type": "string"}
            },
            "additionalProperties": False
        }
    },
    "properties": {
        "bar": {
            "$ref": "#/definitions/test"
        }
    }
}

load(schema).validate({
    "bar": {
        "foo": "test",
        "more": 2
    }
})

The exception is triggered in the NT version of os.path.join(). Which raise the question: why use os.path.join() on elements that are not filesystem paths? Maybe I'm missing a point, but according to https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3 it seems to me that references should always follow an URI pattern and even if the exception was not raised we would end up with backslashes in references which seems erroneous.

Suggested fix: os.path import should be removed from draft03.py and draft04.py, and

os.path.join(pointer, 'xxx'...)

occurrences replaced by simple

"/".join(pointer, 'xxx'...)

I hope you can push a minor version bump with the fix on Pypi soon.

error with Python 2.7

Trying to run json validate on the command line, I get this traceback:

json validate --schema-file .\display_descriptor_schema_1.json --document-file .\apps\displays\HMD.json
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\json.exe\__main__.py", line 9, in <module>
  File "C:\Python27\lib\site-packages\jsonspec\cli\__init__.py", line 504, in main
    args.func(args)
  File "C:\Python27\lib\site-packages\jsonspec\cli\__init__.py", line 172, in __call__
    return self.run(args)
  File "C:\Python27\lib\site-packages\jsonspec\cli\__init__.py", line 451, in run
    validated = load(args.schema).validate(args.document)
  File "C:\Python27\lib\site-packages\jsonspec\validators\__init__.py", line 37, in load
    return factory(schema, uri or '#')
  File "C:\Python27\lib\site-packages\jsonspec\validators\factorize.py", line 86, in __call__
    return compiler(schema, pointer, context)
  File "C:\Python27\lib\site-packages\jsonspec\validators\draft04.py", line 221, in compile
    compiled = compile(subschema, subpointer, context, scope)
  File "C:\Python27\lib\site-packages\jsonspec\validators\draft04.py", line 221, in compile
    compiled = compile(subschema, subpointer, context, scope)
  File "C:\Python27\lib\site-packages\jsonspec\validators\draft04.py", line 49, in compile
    scope = urljoin(scope or str(pointer), schm.pop('id', None))
TypeError: pop() takes at most 1 argument (2 given)

Command-line validate with a schema reference

Could a --schema-pointer <pointer> parameter be added to json-validate?

I would like to validate various similar JSON file with just one schema file containing various related schema definitions.

Example:

  • foo.json contains {foo:3}
  • foo_bar.json contains {foo:3, bar:4}]
  • foos.json contains [{foo:3}, {foo:4}]
  • schema.json contains:
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "foo": {
            "type": "object",
            "properties": {
                "foo": { "type": "integer" }
            },
            "required": ["foo"]
        },
        "foo_bar": {
            "allOf": [
                { "$ref": "#/definitions/foo" },
                {
                    "properties": {
                        "bar": { "type": "integer" }
                    },
                    "required": ["bar"]
                }
            ]
        },
        "foos": {
            "type": "array",
            "items": { "$ref": "#/definitions/foo" }
        }
    }
}
$ json-validate --schema-file=schema.json --schema-pointer=/definitions/foo --document-file=foo.json
$ json-validate --schema-file=schema.json --schema-pointer=/definitions/foo_bar --document-file=foo_bar.json
$ json-validate --schema-file=schema.json --schema-pointer=/definitions/foos --document-file=foos.json

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.