44
55Validates a given example file against the schema specification
66"""
7- from json import loads
87from yaml import safe_load
9- from typing import Union
108from os import path
119from openapi_schema_validator import OAS30Validator
1210import json
1311import datetime
14- from requests import get
1512import sys
1613
1714SCHEMA_FILE_PATH = "../specification/validated-relationships-service-api.yaml"
18- SCHEMA_FILE = path .join (
19- path .dirname (path .realpath (__file__ )), SCHEMA_FILE_PATH
20- )
15+ SCHEMA_FILE = path .join (path .dirname (path .realpath (__file__ )), SCHEMA_FILE_PATH )
2116openapi_schema = {}
22- import pdb
2317
24- def main (openapi_schema :dict ):
18+
19+ def main (openapi_schema : dict ):
2520 """Main entrypoint"""
2621 args = sys .argv
27- if ( len (args ) != 3 ) :
22+ if len (args ) != 3 :
2823 print ("Require schema reference and file to validate" )
2924 exit ()
3025
3126 schema = schema_lookup (args [1 ])
3227 file = args [2 ]
33- if ( len (openapi_schema ) == 0 ) :
28+ if len (openapi_schema ) == 0 :
3429 openapi_schema = safe_load (load_file (SCHEMA_FILE ))
3530 validate_consent (openapi_schema , schema , file )
3631
37- def schema_lookup (schema :str ) -> str :
32+
33+ def schema_lookup (schema : str ) -> str :
3834 schema = schema .lower ()
3935 if schema == "consent" :
4036 return "#/components/schemas/ConsentBundle"
@@ -43,7 +39,8 @@ def schema_lookup(schema:str) -> str:
4339
4440 print ("" )
4541
46- def validate_consent (schema :dict , schema_ref :str , file :str ) -> None :
42+
43+ def validate_consent (schema : dict , schema_ref : str , file : str ) -> None :
4744 schema ["$ref" ] = schema_ref
4845 # openapi_schema["$ref"] = [
4946 # "#/components/schemas/ConsentBundle",
@@ -53,15 +50,17 @@ def validate_consent(schema:dict, schema_ref:str, file:str) -> None:
5350 json_contents = load_example_file (file )
5451 OAS30Validator (schema ).validate (json_contents )
5552
56- def load_example_file (file :str ) -> dict :
5753
58- patch = path . join (
59- path . dirname ( path . realpath ( __file__ )), file
60- )
54+ def load_example_file ( file : str ) -> dict :
55+
56+ patch = path . join ( path . dirname ( path . realpath ( __file__ )), file )
6157 yamlfile = safe_load (load_file (patch ))
62- jsonstr = json .dumps (yamlfile [next (iter (yamlfile ))]["value" ], default = date_converter )
58+ jsonstr = json .dumps (
59+ yamlfile [next (iter (yamlfile ))]["value" ], default = date_converter
60+ )
6361 return json .loads (jsonstr )
6462
63+
6564def date_converter (obj ):
6665 """Date and datetime converter to correctly render dates in json"""
6766 if isinstance (obj , datetime .datetime ):
@@ -70,9 +69,11 @@ def date_converter(obj):
7069 return obj .isoformat ()
7170 return obj
7271
72+
7373def load_file (file_path ) -> None :
7474 with open (file_path ) as file :
7575 return file .read ()
7676
77+
7778if __name__ == "__main__" :
7879 main (openapi_schema )
0 commit comments