Code Monkey home page Code Monkey logo

py-orderly-json's Introduction

orderlyjson Python Library

Usage

>>> import orderlyjson, json
>>> orderlydoc = open('test.orderly').read()
>>> print orderlydoc
object {
  string name;
  string description?;
  string homepage /^http:/;
  integer {1500,3000} invented;
}*;

>>> print json.dumps(
...    orderlyjson.parse(orderlydoc), indent=4)
{
    "additionalProperties": true,
    "type": "object",
    "properties": {
        "invented": {
            "minimum": 1500,
            "type": "integer",
            "maximum": 3000
        },
        "homepage": {
            "pattern": "^http:",
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "description": {
            "optional": true,
            "type": "string"
        }
    }
}

>>> jsondoc = open('test.json').read()
>>> print jsondoc
{
  "name": "orderly",
  "description": "A schema language for JSON",
  "homepage": "http://orderly-json.org",
  "invented": 2009
}

>>> orderlyjson.validate(json.loads(jsondoc), orderlydoc)
>>> badjsondoc = """
... {
...   "name": "orderly",
...   "description": "A schema language for JSON",
...   "homepage": "http://orderly-json.org",
...   "invented": 4009
... }
... """
>>> orderlyjson.validate(json.loads(badjsondoc), orderlydoc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "orderlyjson/__init__.py", line 13, in validate
    jsonschema.validate(json_object, orderly_object)
  File "orderlyjson/jsonschema/__init__.py", line 101, in validate
    return v.validate(data,schema)
  File "orderlyjson/jsonschema/validator.py", line 410, in validate
    self._validate(data, schema)
  File "orderlyjson/jsonschema/validator.py", line 413, in _validate
    self.__validate("_data", {"_data": data}, schema)
  File "orderlyjson/jsonschema/validator.py", line 439, in __validate
    validator(data, fieldname, schema, new_schema.get(schemaprop))
  File "orderlyjson/jsonschema/validator.py", line 128, in validate_properties
    self.__validate(eachProp, value, properties.get(eachProp))
  File "orderlyjson/jsonschema/validator.py", line 439, in __validate
    validator(data, fieldname, schema, new_schema.get(schemaprop))
  File "orderlyjson/jsonschema/validator.py", line 230, in validate_maximum
    raise ValueError("Value %r for field '%s' is greater than maximum value: %f" % (value, fieldname, maximum))
ValueError: Value 4009 for field 'invented' is greater than maximum value: 3000.000000

Validation Tool

As a starting point, theres a short python script for validating a json file with an associated orderly schema in the tools directory named validate.py; thanks to Jens Hübel for the contribution:

Usage is then like this:

python validate.py invoice.json PropDef.orderly

gives:

OK

or:

python validate.py invoice_bad.json PropDef.orderly

results in:

Validation FAILED

Value 'XXX' for field 'updatability' is not in the enumeration: ['readonly', 're adwrite', 'whencheckedout', 'oncreate']

Thanks and License

Thanks to Lloyd Hilaiel for the documentation and language specification at orderly-json.org.

This code is licensed under the MIT license. It currently includes the jsonschema library found here;

py-orderly-json's People

Contributors

kroo avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

drdaeman sbonnici

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.