def get_properties(schema: str, required: bool = False) -> List[str]: """ Return properties of given schema. """ # get schema data_schema = get_schema(schema=schema) # return either required or all properties if required: return Draft7Validator(data_schema).schema["required"] else: return list(Draft7Validator(data_schema).schema["properties"].keys())
def validate_schema(data: dict, schema_file: dict) -> Tuple[bool, iter]: """Do assertion that data validates against the JSONSchema in schema_file.""" schema = _load_json_schema(schema_file) if Draft7Validator(schema, format_checker=draft7_format_checker).is_valid(data): return True, None errors = Draft7Validator( schema, format_checker=draft7_format_checker).iter_errors(data) return False, errors
def get_guild_and_save_groups(user: UserModel): members_resp = requests.get( 'https://discordapp.com/api/v6/guilds/' + GUILD_ID + '/members?limit=1000', headers={'Authorization': 'Bot ' + BOT_TOKEN}) guild_resp = requests.get( 'https://discordapp.com/api/v6/guilds/' + GUILD_ID, headers={'Authorization': 'Bot ' + BOT_TOKEN}) if members_resp.ok \ and guild_resp.ok \ and Draft7Validator(DiscordApi.members_schema).is_valid(members_resp.json()) \ and Draft7Validator(DiscordApi.guild_schema).is_valid(guild_resp.json()): guild = Guild(guild_resp.json(), members_resp.json()) DiscordApi.set_groups(user, guild)
def validateJsonSchema(self, file: Path): schema = self.jsonSchema data = self.validateSyntax(file) errors = list() try: Draft7Validator(schema).validate(data) except exceptions.ValidationError: self._error = True for error in sorted(Draft7Validator(schema).iter_errors(data), key=str): errors.append(error.message) if errors: self.saveIndentedError(2, f'Schema errors in {file.parent.name}/{file.name}:') self.printErrorList(errors, 4)
def _validate(self, schema, data): validator = Draft7Validator(schema) validation_errors = sorted(validator.iter_errors(data), key=lambda e: e.path) if validation_errors: self._result["failed"] = True self._result["msg"] = "Validation errors were found. " self._result["errors"] = [] for validation_error in validation_errors: if isinstance(validation_error, ValidationError): error = { "message": validation_error.message, "var_path": self._to_path(validation_error.relative_path), "schema_path": self._to_path(validation_error.relative_schema_path), "relative_schema": validation_error.schema, "expected": validation_error.validator_value, "validator": validation_error.validator, "found": validation_error.instance, } self._result["errors"].append(error) var_path = error["var_path"] or "root" error_msg = "At '{var_path}' {ve}. ".format( var_path=var_path, ve=validation_error.message) self._result["msg"] += error_msg
def validate_schema(model_list): schema = { "type" : "array", "items" : { "oneOf": [ { "type" : "object", "properties" : { "model" : {"type" : "string"}, "alias" : {"type" : "string"}, "version" : {"type" : ["string", "integer"]}, "precision" : { "type" : "array", "items" : {"enum" : ["FP32", "FP16", "INT8", "FP16-INT8", "FP32-INT8", "FP32-INT1", "FP16-INT1", "INT1"]} } }, "required" : ["model"], "additionalProperties": False }, { "type" : "string" } ] } } try: validator = Draft7Validator(schema, format_checker=FormatChecker()) validator.validate(model_list) except Exception as err: print("Yaml input schema validation error.") print(err) sys.exit(1)
def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]: schema_file = os.path.join(SCHEMA_DIR, f"{schema_name}.json") if not os.path.exists(schema_file): raise ValueError(f"Schema {schema_name} does not exist.") with open(schema_file, encoding="utf-8") as f: schema = json.loads(f.read()) from jsonschema import Draft7Validator validator = Draft7Validator(schema) validation_errors = sorted(validator.iter_errors(obj), key=lambda e: e.path) # type: ignore[no-any-return] errors = [] for error in validation_errors: message = error.message if error.path: path = ".".join(str(x) for x in error.absolute_path) message = f"[{path}] {message}" errors.append(message) return errors
def _check_body_for_errors(self, schema, request): json_schema = self._get_combined_schema(schema) schema_validator = Draft7Validator(json_schema) for schema_error in sorted(schema_validator.iter_errors(request), key=str): self.ResponseClient.set_error(self._get_error_path(schema_error), schema_error.message)
def check_files(event_dir, schema_dir): data_schemas = {} name_schemas = [] events = os.listdir(path=event_dir + ".") schemas = os.listdir(path=schema_dir + ".") for f in schemas: with open(schema_dir + f, "r") as read_file: name_schemas.append(f) data = json.load(read_file) data_schemas[f] = data for f in events: with open(event_dir + f, "r") as read_file: print('-------the following errors were found in file ' + f + ':') data = json.load(read_file) name_sch = '' if data != None and 'event' in data: name_sch = data['event'] + '.schema' print('-----------for schema ' + name_sch + ':') schema_detect = False for i in name_schemas: if i == name_sch: schema_detect = True v = Draft7Validator(data_schemas[i]) errors = sorted(v.iter_errors(data), key=lambda e: e.path) for error in errors: print(error.message) if schema_detect == False: print ('schema failed')
def importDialog(self): def addAfterImport(iterations_new): self.setupUi(self.ic + [deserialize_iteration(iter) for iter in iterations_new]) def replaceAfterImport(iterations_new): self.setupUi([deserialize_iteration(iter) for iter in iterations_new]) dirpath = Path(f'{os.path.dirname(os.path.realpath(__file__))}', '../../json_schemas/iterations.json') schema_path = dirpath.absolute().as_uri() with dirpath.open('r') as jsonfile: schema = json.load(jsonfile) resolver = RefResolver( schema_path, schema, ) validator = Draft7Validator(schema, resolver=resolver, format_checker=None) dial = SRSettingAddReplace(mw) dial.setupUi( json.dumps([serialize_iteration(iter) for iterCouple in self.ic for iter in iterCouple], sort_keys=True, indent=4), validator, addAfterImport, replaceAfterImport, ) dial.exec_()
def validate(file: TextIO): """ Checks the orca file against a schema. which schema is determined by the 'apiVersion' defined in the orca configuration, if no configuration :param file: :return: """ data = file.read() log.debug("Raw yaml: {0}".format(data)) orca_data = yaml.load(data, yaml.Loader) try: version = orca_data['apiVersion'] except KeyError: raise ConfigurationError( "'apiVersion is missing. An API version must be specified on an orca document," + " the latest current version is {0}'".format(LATEST_SCHEMA_VERSION) ) schema_file = os.path.join(_get_schema_location(), "ORCA_SCHEMA_{0}.json".format(version)) try: # check if the schema file exists. with open(schema_file) as fp: schema_data = json.load(fp) validator = Draft7Validator(schema_data) errors = list(validator.iter_errors(orca_data)) _handle_errors(errors, _handle_generic_error, file.name) return data except FileNotFoundError: raise ConfigurationError( "'The apiVersion {0}, is an invalid apiVersion version. It did not match one of the" .format(version) + " supported apiVersions: {0}'".format(AVAILABLE_SCHEMA_VERSIONS))
def is_valid(self, adict, schema, messages=None, draft=4): def trace_error_value(error): if len(error.path) != 0: return (error.path[-1], error.message) return ('keyError', error.message) if draft == 4: self.__errors = dict( trace_error_value(e) for e in sorted(Draft4Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if draft == 6: self.__errors = dict( trace_error_value(e) for e in sorted(Draft6Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if draft == 7: self.__errors = dict( trace_error_value(e) for e in sorted(Draft7Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if len(self.__errors) > 0 and messages: self.__errors = self.remap_error_message(self.__errors, messages) self.__data = adict if len(self.__errors) == 0 else [] return len(self.__errors) == 0
def json_schema_error(self): schema = {"type": "object", "properties": {"a": {"type": "string"}}} validator = Draft7Validator(schema) errors = list(validator.iter_errors({"a": 1})) return errors[0]
def verify_records_schema( records: List[AirbyteRecordMessage], catalog: ConfiguredAirbyteCatalog ) -> Mapping[str, Mapping[str, ValidationError]]: """Check records against their schemas from the catalog, yield error messages. Only first record with error will be yielded for each stream. """ validators = {} for stream in catalog.streams: validators[stream.stream.name] = Draft7Validator( stream.stream.json_schema, format_checker=CustomFormatChecker()) stream_errors = defaultdict(dict) for record in records: validator = validators.get(record.stream) if not validator: logging.error( f"Record from the {record.stream} stream that is not in the catalog." ) continue errors = list(validator.iter_errors(record.data)) for error in errors: stream_errors[record.stream][str(error.schema_path)] = error return stream_errors
def __init__(self): # Dynamically constructs the JSON schema and the Draft7Validator used in the CommandParser # IMPORTANT: validate schema with Draft7Validator.check_schema(schema) before using it! inflector = Inflector() commands = 'commands' current_path = dirname(abspath(__file__)) excluded_files = ['__init__.py'] modules = [ file_name[:-3] for file_name in listdir(commands) if isfile(join(join(current_path, commands), file_name)) and file_name not in excluded_files ] schema = { "$schema": "https://json-schema.org/schema#", "type": "object", "properties": { "name": { "type": "string", "enum": modules }, "args": { "type": "object" } }, "required": ["name"] } classes = dict() for module_name in modules: classes[module_name] = getattr( import_module(f'{commands}.{module_name}'), inflector.camelize(module_name)) self.validator = Draft7Validator(schema) self.classes = classes
def __init__(self): super().__init__() arguments_schema = { "$schema": "https://json-schema.org/schema#", "$defs": { "section": { "type": "object", "properties": { "start": { "type": "integer" }, "end": { "type": "integer" }, "color": { "type": "string", "pattern": "^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$" } }, "required": ["start", "end", "color"] } }, "type": "object", "properties": { "sections": { "type": "array", "items": {"$ref": "#/$defs/section"} } }, "required": ["sections"] } self.validator = Draft7Validator(arguments_schema)
def validate_schema(self): """ Draft7 formats: https://json-schema.org/understanding-json-schema/reference/string.html for additional format validation refer https://python-jsonschema.readthedocs.io/en/stable/validate/ for ex. enable uri checks: sudo -H pip3 install rfc3987 :return: """ try: import jsonsempai with jsonsempai.imports(): from ocli.ai import recipe_schema # my_path = os.path.abspath(os.path.dirname(__file__)) # path = os.path.join(my_path, "recipe_schema.json") # f = open(path, 'r') # schema = json.load(f) # f.close() schema = recipe_schema.properties.envelope v = Draft7Validator(schema, format_checker=draft7_format_checker) errors = sorted(v.iter_errors(self.recipe), key=lambda e: e.path) if not len(errors): self.log.info(f"recipe {self.file}: syntax is valid") return None for error in errors: self.log.error(f'{error.message} in {list(error.path)}') return 1 except Exception as e: self.log.error(f'Could not perform validation: {e}') return 1
def main_schema(data): with open('schemas/full_schema.json') as api_schema: main_schema = json.load(api_schema) v = Draft7Validator(main_schema) yield v main_teardown(v, data)
def run(input_json_path, schema_path): print(f'read: {input_json_path}') with open(input_json_path, 'r', encoding='utf8') as file: data = json.load(file) print(f'read: {schema_path}') with open(schema_path, 'r', encoding='utf8') as file: schema = json.load(file) os.makedirs('./out', exist_ok=True) ext_removed = os.path.splitext(os.path.normpath(input_json_path))[0] file_name = ext_removed.split(os.path.sep)[-1] print('process: validate with schema') v = Draft7Validator(schema) errors = sorted(v.iter_errors(data), key=lambda e: e.path) out_path = f'./out/{file_name}.vallog.txt' print(f'write: {out_path}') if len(errors) else print('passed all!!') if len(errors): with open(out_path, 'w', encoding='utf8') as file: sys.stdout = file for error in errors: print(error) print('-----------------------') sys.stdout = sys.__stdout__ print('DONE!!')
def validate_attribute_schema(schema, data_model): """ validate each attribute against JSON schema @param schema: JSON validation schema @param data_model: uploaded data model @return: dictionary with all schema errors """ schema = get_json(schema) v = Draft7Validator(schema, format_checker=draft7_format_checker) errors = sorted(v.iter_errors(data_model), key=lambda e: e.path) print(data_model['id'], ": Number of validation errors = ", len(errors)) err = {} for error in errors: if len(list(error.path)): attribute = list(error.path)[0] err.setdefault(attribute, []).append(error.message) print(attribute, error.message, sep=": ") # err['attribute'] = list(error.path)[0] # err['message'] = ": ".join([err['attribute'], error.message]) # for suberror in sorted(error.context, key=lambda e: e.schema_path): # print(" ", list(suberror.schema_path)[1], ": ", suberror.message) # err['suberrors'] = " " + list(suberror.schema_path)[1] + ": " + suberror.message else: print(error.message) attribute = re.findall(r"(.*?)'", error.message)[1] err.setdefault(attribute,[]).append(error.message) # err['attribute'] = re.findall(r"(.*?)'", error.message)[1] # err['message'] = error.message return err
def validate_linked_field_sets(self, value): v = Draft7Validator(LINKED_FIELD_SETS_SCHEMA) validation = v.is_valid(value) if not validation: raise serializers.ValidationError( [str(error.message) for error in v.iter_errors(value)]) return value
def __get_strict_validator(self): """Return a strict version of the Validator, create it if it doesn't exist already. To create a strict version of the schema, this function adds `additionalProperties` to all objects in the schema. Returns: Draft7Validator: Validator for this schema in strict mode. """ # TODO Currently the function is only modifying the top level object, need to add that to all objects recursively if self.strict_validator: return self.strict_validator # Create a copy if the schema first and modify it to insert `additionalProperties` schema = copy.deepcopy(self.data) if schema.get("additionalProperties", False) is not False: print( f"{schema['$id']}: Overriding existing additionalProperties: {schema['additionalProperties']}" ) schema["additionalProperties"] = False # TODO This should be recursive, e.g. all sub-objects, currently it only goes one level deep, look in jsonschema for utilitiies for prop_name, prop in schema.get("properties", {}).items(): items = prop.get("items", {}) if items.get("type") == "object": if items.get("additionalProperties", False) is not False: print( f"{schema['$id']}: Overriding item {prop_name}.additionalProperties: {items['additionalProperties']}" ) items["additionalProperties"] = False self.strict_validator = Draft7Validator(schema) return self.strict_validator
def validate_schema(self, json_data: json, business_entity: str, version: str) -> json: if not self.version_exist(version, business_entity): return ControllerConstants.VERSION_NOT_EXIST.format(version) schema = self._get_json_schema(business_entity, version) data_validated = Draft7Validator(schema) sorted_errors = sorted(data_validated.iter_errors(json_data), key=lambda e: e.path) errors = {} for error in sorted_errors: if error.validator == ValidatorResponseConstants.REQUIRED_KEY: error_property = re.search("'(.+?)'", error.message) if error_property: errors[error_property.group(1)] = { ValidatorResponseConstants.ERROR_MESSAGE: error.message, ValidatorResponseConstants.VALIDATE_KEY: error.validator } elif error.validator == ValidatorResponseConstants.ADDITIONAL_PROPERTIES: errors[error.validator] = { ValidatorResponseConstants.ERROR_MESSAGE: error.message, ValidatorResponseConstants.VALIDATE_KEY: error.validator } else: for error_property in error.path: errors[error_property] = { ValidatorResponseConstants.ERROR_MESSAGE: error.message, ValidatorResponseConstants.VALIDATE_KEY: error.validator_value } return errors
def post(self): ''' Takes in an json obj and creats a record from it Using jsonscheme to validate the json obj :return: Ok with status code 201 if added record Error if inbound json obj bad ''' appt_json = request.json error = best_match(Draft7Validator(schema).iter_errors(appt_json)) if error: return jsonify({"error": error.message}), 400 try: date = datetime.datetime.strptime(appt_json.get('date'), "%Y-%m-%dT%H:%M:%SZ") except: return jsonify({"error": "INVALID_DATE"}), 400 appt = Appts(external_id=str(uuid.uuid4()), first_name=appt_json.get('first_name'), last_name=appt_json.get('last_name'), service=appt_json.get('service'), status=appt_json.get('status'), street_address=appt_json.get('street_address'), city=appt_json.get('city'), state=appt_json.get('state'), zip=appt_json.get('zip'), phone=appt_json.get('phone'), date=date, price=appt_json.get('price')).save() reply = {"result": "ok", "appt": appt_obj(appt)} return jsonify(reply), 201
def validateSchema(self) -> None: schema = self.jsonSchema for file in self.jsonFiles: self._validModule['schema'][file.name] = list() jsonPath = self._validModule['schema'][file.name] # try to load json from file and return error when the format is invalid data = self.validateSyntax(file) # validate the loaded json try: Draft7Validator(schema).validate(data) except exceptions.ValidationError: self._error = True for error in sorted(Draft7Validator(schema).iter_errors(data), key=str): jsonPath.append(error.message)
def put(self, appt_id): ''' Updates the status of a recod :param appt_id: rec id to update :return: Error if fail json schema validation If updated send back ok with status code 200 ''' if appt_id: appt = Appts.objects.filter(external_id=appt_id, live=True).first() if not appt: return jsonify({}), 404 appt_json = request.json error = best_match( Draft7Validator(schema_put).iter_errors(appt_json)) if error: return jsonify({'error': error.message}), 400 appt.status = appt_json.get('status') appt.save() reply = {"result": "ok", "appt": appt_obj(appt)} return jsonify(reply), 200
def __init__(self): super().__init__() arguments_schema = { "$schema": "https://json-schema.org/schema#", "type": "object", "properties": { "section_id": { "type": "string" }, "start": { "type": "integer" }, "end": { "type": "integer" }, "color": { "type": "string", "pattern": "^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$" } }, "required": ["section_id"] } self.section_id = None self.color = None self.start = None self.end = None self.validator = Draft7Validator(arguments_schema)
def post(self): parcels_json = request.json error = best_match(Draft7Validator(schema).iter_errors(parcels_json)) if error: return jsonify({"error": error.message}), 400 owner = parcels_json.get("owner") metadata = parcels_json.get("metadata") data = bytes.fromhex(parcels_json.get("data")) parcel_id = SHA256.new(data).digest().hex().upper() ownership_obj = Ownership(parcel_id=parcel_id, owner=owner) metadata_obj = MetaData(parcel_id=parcel_id, parcel_meta=metadata) db.session.add(ownership_obj) db.session.add(metadata_obj) try: db.session.commit() except IntegrityError: return jsonify({"error": "Parcel ID %s already exists" % parcel_id}), 409 except: return jsonify({"error": "Error occurred on saving ownership and metadata"}), 500 try: ceph.upload(parcel_id, data) self._delete_key(request) except CephAdapterError as e: # To operate atomic db.session.delete(ownership_obj) db.session.delete(metadata_obj) db.session.commit() return jsonify({"error": e.msg}), 500 return jsonify({"id": parcel_id}), 200
def validate(self, data): """ Validate that the Serializer contains valid data. Set the AnsibleRepository based on the RepositoryVersion if only the latter is provided. Set the RepositoryVersion based on the AnsibleRepository if only the latter is provided. Convert the human-friendly names of the content types into what Pulp needs to query on. """ super().validate(data) if hasattr(self, "initial_data"): validate_unknown_fields(self.initial_data, self.fields) if "config" in data: validator = Draft7Validator(COPY_CONFIG_SCHEMA) err = [] for error in sorted(validator.iter_errors(data["config"]), key=str): err.append(error.message) if err: raise serializers.ValidationError( _("Provided copy criteria is invalid:'{}'".format(err))) return data
def schema_business_validate(payload, client_configuration, email_ids, customer_id): # pylint: disable-msg=R0801 ''' validating einvoice payload params - payload: payload is request body params - client_configuration: configuration of client params - email_ids: client email_id params - customer_id: client id ''' if not "Version" in payload: payload["Version"] = "1.00" # adding valdtls node if does not exist in payload ValDtlsTransform.transform_valdtls(payload) validator = Draft7Validator(SCHEMA) res_body = copy.deepcopy(RESPONSE_STRUCTURE) res_body["pwc_response"]["message"] = RESPONSE_MESSAGE['AUTH_PASSED'] res_body["pwc_response"]["invoice_number"] = payload.get("DocDtls", {}).get("No", "") res_body["pwc_response"]["document_date"] = payload.get("DocDtls", {}).get("Dt", "") res_body["pwc_response"]["document_type"] = payload.get("DocDtls", {}).get("Typ", "") gstin = payload.get('User_GSTIN') if not gstin: gstin = payload.get('SellerDtls', {}).get('Gstin', '') res_body["pwc_response"]["gstin"] = gstin # transformation TransformData.transform_mis(payload, client_configuration, res_body, customer_id) if client_configuration.get("validation", "") == FLAG["YES"]: errors = sorted(validator.iter_errors(payload), key=lambda e: e.path) # checking if errors then create error response if errors: for error in errors: error_dict = get_error(error.message, ".".join(\ str(x) for x in error.relative_path), "Error") res_body["pwc_response"]["validation_remarks"].append(error_dict) res_body["pwc_response"]["message"] += f", {RESPONSE_MESSAGE['SCH_VAL_FAILED']}" res_body["pwc_response"]["validation_status"] = STATUS["ERROR"] res_body["pwc_response"]["status"] = STATUS_CODE["SCH_VAL_FAILED"] # sending sqs for schema validation failed SQSService.exception_sending(customer_id, PROCESS_NAME["IRN_GENERATE"],\ SQS_TEMPLATE["SCH_VAL_FAILED"], email_ids, [res_body], payload) # is_exclude validation else: res_body["pwc_response"]["message"] += f", {RESPONSE_MESSAGE['SCH_VAL_PASSED']}" res_body["pwc_response"]["validation_status"] = STATUS["SUCCESS"] res_body["pwc_response"]["status"] = STATUS_CODE["SUCCESS"] validationrow(payload, client_configuration, res_body) if res_body["pwc_response"]["status"] == STATUS_CODE["SUCCESS"]: res_body["pwc_response"]["message"] += f", {RESPONSE_MESSAGE['BSN_VAL_PASSED']}" else: # sending sqs for business logic failed SQSService.exception_sending(customer_id, PROCESS_NAME["IRN_GENERATE"],\ SQS_TEMPLATE["BSN_VAL_FAILED"], email_ids, [res_body], payload) if res_body["pwc_response"]["validation_remarks"]: if email_ids: send_email(email_ids, json.dumps(res_body)) else: res_body["pwc_response"]["status"] = STATUS_CODE["SUCCESS"] res_body["pwc_response"]["validation_status"] = STATUS["SUCCESS"] res_body["pwc_response"]["message"] = RESPONSE_MESSAGE["VAL_NT_REQ"] return res_body