Hide keyboard shortcuts

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''' 

5 

6import sys 

7import jsonref 

8 

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) 

17 

18from jsonsubschema.exceptions import UnsupportedRecursiveRef 

19 

20 

21def prepare_operands(s1, s2): 

22 # First, we load schemas using jsonref to resolve $ref 

23 # before starting canonicalization. 

24 

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) 

30 

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. 

34 

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 

43 

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 

50 

51 return _s1, _s2 

52 

53 

54def isSubschema(s1, s2): 

55 ''' Entry point for schema subtype checking. ''' 

56 s1, s2 = prepare_operands(s1, s2) 

57 return s1.isSubtype(s2) 

58 

59 

60def meet(s1, s2): 

61 ''' Entry point for schema meet operation. ''' 

62 s1, s2 = prepare_operands(s1, s2) 

63 return s1.meet(s2) 

64 

65 

66def join(s1, s2): 

67 ''' Entry point for schema meet operation. ''' 

68 s1, s2 = prepare_operands(s1, s2) 

69 return s1.join(s2) 

70 

71 

72def isEquivalent(s1, s2): 

73 ''' Entry point for schema equivalence check operation. ''' 

74 return isSubschema(s1, s2) and isSubschema(s2, s1)