"""Json Utils.""" import json import jsonschema from marketing import config def create_draft_validator_from_model(model_name): """Get a draft validator from a model. Args: model_name (str): name of the model. Returns: Draft3Validator: the data validator. """ filename = config.MODEL_DEFINITION_PATH.format(model_name=model_name) return create_draft_validator_from_file(filename) def create_draft_validator_from_file(filename): """Get a draft validator from a file. Args: filename (str): the file that contains the schema. Returns: Draft3Validator: the data validator. """ with open(filename) as content: definition = json.load(content) return jsonschema.Draft3Validator(definition) def get_enum_from_draft(draft, property_name): """Get enum values from a schema. Args: draft (Draft3Validator): the draft validator that contains the schema. property_name (str): name of the property. Throws: KeyError: if one of the key is not accessible. This is expected as this method will be used for system operations and will not be used in flows that involve user input. Returns: list: the enum values. """ return draft.schema['properties'][property_name]['enum'] def get_enums_from_draft(draft, *property_names): """Get enum values from a schema. Args: draft (Draft3Validator): the draft validator that contains the schema. property_names (list): names of the properties. Throws: KeyError: if one of the key is not accessible. This is expected as this method will be used for system operations and will not be used in flows that involve user input. Returns: tuple: contains for each property the enums. """ return [ get_enum_from_draft(draft, property_name) for property_name in property_names]