def validate(schema, part, standard): if standard in legacy: cls = legacy[standard] try: jsonschema.validate(part.json, schema.json, cls=cls) return (True, '') except jsonschema.ValidationError as e: return (False, str(e)) except jsonschema.SchemaError as e: raise ValueError("Schema is invalid:\n{0}\n\n{1}".format( str(e), schema.content)) return (is_valid, message) else: catalogue = create_catalog('2019-09', '2020-12') compiled_schema = JSONSchema(schema.json, metaschema_uri=URI(standard)) if not compiled_schema.validate().valid: raise ValueError("Schema is invalid:\n{0}\n\n{1}".format( "INVALID SCHEMA", schema.content)) elif part.json == (1+1j): return (False, 'INVALID JSON') else: jsonValue = JSON.loads(part.content) validation_result = compiled_schema.evaluate(jsonValue) if validation_result.valid: return (True, ''); else: return (False, 'VALIDATION ERROR');
def test_schema_examples(example, json1_valid, json2_valid): schema = JSONSchema(example, metaschema_uri=metaschema_uri_2020_12) schema.validate() assert schema.value == example assert schema.type == "boolean" if isinstance(example, bool) else "object" assert schema.parent is None assert schema.key is None assert not schema.path assert schema.metaschema_uri == metaschema_uri_2020_12 assert schema.evaluate(json1).valid is json1_valid assert schema.evaluate(json2).valid is json2_valid
def test_contains(minmax, instval): min_contains = min(minmax) max_contains = max(minmax) contains_count = len( list(filter(lambda item: JSON(item).type == "boolean", instval))) schema = JSONSchema( { "contains": { "type": "boolean" }, "minContains": min_contains, "maxContains": max_contains, }, metaschema_uri=metaschema_uri_2019_09) scope = schema.evaluate(JSON(instval)) assert scope.valid == (min_contains <= contains_count <= max_contains)
def get_validity(metadata: dict[str, Any], schema: JSONSchema) -> Any: if (result := schema.evaluate(JSON(metadata))).valid: return result.output('flag')
def test_recursive_schema_extension_2020_12(): tree_schema = JSONSchema(tree_2020_12) strict_tree_schema = JSONSchema(strict_tree_2020_12) tree_json = JSON(tree_instance_2020_12) assert tree_schema.evaluate(tree_json).valid is True assert strict_tree_schema.evaluate(tree_json).valid is False