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

Added basic usage instructions to README.md #19

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,47 @@ Testing
You will need tox (get it from pip) as python2.7

$ tox

Quickstart
==========

The fastest way to validate a schema against json is to use the `validate()` function provided in `json_schema_validator.shortcuts`.

To import 'shortcuts' use:
`from json_schema_validator.shortcuts import *`

The function `validate()` provided by 'shortcuts' takes two positional arguments, `schema text` and `json text`. Simply pass in a valid json schema as raw string and then the json text to validate as a raw string:

from json_schema_validator.shortcuts import *

schema = open("a_schema.json", 'r')
json = open("some_json.json", 'r')

validate(schema.read(), json.read())

`validate()` will first test the schema parameter against it's schema specification, followed by the json parameter against the schema parameter. Problems with:
- json syntax in either schema or json subject documents
- adherence to schema specifications
- adherence of your subject json to your provided schema


Will throw exceptions, otherwise this function will return `True`

Checking a Schema against a Schema specification
================================================

Given the scenario that you have just created a new schema and you want to test that it is valid json/ that it adheres to your provided schema specification draft, you should use the 'Schema' class from `json_schema_validator.schema`.

Instead of providing a raw string the `Schema()` constructor, `Schema()` requires a Python object (a dict).

To get your raw json string into this format use the [json](https://docs.python.org/3.8/library/json.html) library's `.load()` method (or `.loads()` for a raw string json schema).
This means any syntax mistakes in your schema's json will throw exceptions when using `json.load()/.loads()` instead of inside of **json_schema_validator** functionality. See the example:

from json_schema_validator.schema import Schema
import json

schema = open("a_schema.json", 'r')
#convert to native python object via json library
schema_obj = json.load(schema)

validated = Schema(schema_obj)