def validate(data, schema=None): if schema is None: schema = generate() JsonSchemaValidator.check_schema(schema) validator = JsonSchemaValidator(schema) errors = list(validator.iter_errors(data)) if not errors: return check_unique(data) or [] try: resp = policy_error_scope(specific_error(errors[0]), data) name = isinstance( errors[0].instance, dict) and errors[0].instance.get( 'name', 'unknown') or 'unknown' return [resp, name] except Exception: logging.exception( "specific_error failed, traceback, followed by fallback") return list(filter(None, [ errors[0], best_match(validator.iter_errors(data)), ]))
def __init__(self, conf_type: ConfigurationType): self.conf_type = ConfigurationType(conf_type) self.schema_path = Path(CONFIG_JSON_SCHEMA_PATH, f"{self.conf_type.value}.jsonschema.json") with self.schema_path.open("r") as f: self.schema_dict = json.load(f) self.validator = JsonSchemaValidator(self.schema_dict)
def __init__(self, schema, only_error=True): super(DataValidator, self).__init__(schema, name_field='name', only_error=only_error) self._v = JsonSchemaValidator(self._schema)