Coverage for jsonsubschema/api.py : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1'''
2Created on June 24, 2019
3@author: Andrew Habib
4'''
6import sys
7import jsonref
9from jsonsubschema._canonicalization import (
10 canonicalize_schema,
11 simplify_schema_and_embed_checkers
12)
13from jsonsubschema._utils import (
14 validate_schema,
15 print_db
16)
18from jsonsubschema.exceptions import UnsupportedRecursiveRef
21def prepare_operands(s1, s2):
22 # First, we load schemas using jsonref to resolve $ref
23 # before starting canonicalization.
25 # s1 = jsonref.loads(json.dumps(s1))
26 # s2 = jsonref.loads(json.dumps(s2))
27 # This is not very efficient, should be done lazily maybe?
28 s1 = jsonref.JsonRef.replace_refs(s1)
29 s2 = jsonref.JsonRef.replace_refs(s2)
31 # Canonicalize and embed checkers for both lhs
32 # and rhs schemas before starting the subtype checking.
33 # This also validates input schemas and canonicalized schemas.
35 # At the moment, recursive/circual refs are not supported and hence, canonicalization
36 # throws a RecursionError.
37 try:
38 _s1 = simplify_schema_and_embed_checkers(
39 canonicalize_schema(s1))
40 except RecursionError:
41 # avoid cluttering output by unchaining the recursion error
42 raise UnsupportedRecursiveRef(s1, 'LHS') from None
44 try:
45 _s2 = simplify_schema_and_embed_checkers(
46 canonicalize_schema(s2))
47 except RecursionError:
48 # avoid cluttering output by unchaining the recursion error
49 raise UnsupportedRecursiveRef(s2, 'RHS') from None
51 return _s1, _s2
54def isSubschema(s1, s2):
55 ''' Entry point for schema subtype checking. '''
56 s1, s2 = prepare_operands(s1, s2)
57 return s1.isSubtype(s2)
60def meet(s1, s2):
61 ''' Entry point for schema meet operation. '''
62 s1, s2 = prepare_operands(s1, s2)
63 return s1.meet(s2)
66def join(s1, s2):
67 ''' Entry point for schema meet operation. '''
68 s1, s2 = prepare_operands(s1, s2)
69 return s1.join(s2)
72def isEquivalent(s1, s2):
73 ''' Entry point for schema equivalence check operation. '''
74 return isSubschema(s1, s2) and isSubschema(s2, s1)