Skip to content

Instantly share code, notes, and snippets.

@jason-s
Created June 11, 2020 22:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jason-s/188c226e51f0982ab05538c2d6581c21 to your computer and use it in GitHub Desktop.
import jsonschema
import yaml
schema1 = yaml.safe_load("""
title: SCHEMA 1 (foo_node or bar_node)
$ref: '#/definitions/root_node'
$schema: http://json-schema.org/draft-07/schema#
definitions:
bar_node:
additionalProperties: false
properties:
type: {const: bar}
flavor: {type: string}
required: [flavor]
type: object
foo_node:
additionalProperties: false
properties:
type: {const: foo}
color: {type: string}
required: [color]
type: object
root_node:
allOf:
- if:
properties:
type: {const: foo}
then: {$ref: '#/definitions/foo_node'}
- if:
properties:
type: {const: bar}
then: {$ref: '#/definitions/bar_node'}
properties:
type:
enum: [foo, bar]
required: [type]
""")
data = yaml.safe_load("""
item1:
type: bar
flavor: yummy
item2:
type: foo
color: red
item3:
type: foo
color: 0
item4:
type: foo
color: red
zipcode: 12345
""")
print("jsonschema version: %s" % jsonschema.__version__)
for k in sorted(data.keys()):
v = data[k]
try:
jsonschema.validate(instance=v, schema=schema1)
print("%s: success" % k)
except jsonschema.exceptions.ValidationError, e:
print("=====\n%s: failure\nmessage: %s\npath: %s\nabsolute_schema_path: %s\n%s" %
(k,e.message,list(e.path),list(e.absolute_schema_path),e)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment