def test_context_missing_definitions(msg_assertions): with pytest.raises(KeyError) as err: schema_validator({'$ref': 'SomeReference'}) msg_assertions.assert_error_message_equal( str(err.value), MESSAGES['unknown_reference']['no_definitions'], )
def test_external_docs_with_valid_value(msg_assertions): try: schema_validator({'consumes': {'url': 'http://example.com'}}) except ValidationError as err: errors = err.detail else: errors = {} msg_assertions.assert_path_not_in_errors('externalDocs', errors)
def test_reference_type_validation(value, msg_assertions): with pytest.raises(ValidationError) as err: schema_validator({'$ref': value}) msg_assertions.assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, '$ref', )
def test_external_docs_type_validation(value, MESSAGES, msg_assertions): with pytest.raises(ValidationError) as err: schema_validator({'externalDocs': value}) msg_assertions.assert_message_in_errors( MESSAGES['type']['invalid'], err.value.detail, 'externalDocs.type', )
def test_external_docs_is_not_required(msg_assertions): try: schema_validator({}) except ValidationError as err: errors = err.detail else: errors = {} msg_assertions.assert_path_not_in_errors('externalDocs', errors)
def test_reference_not_found_in_definitions(msg_assertions): with pytest.raises(ValidationError) as err: schema_validator({'$ref': 'UnknownReference'}, context={'definitions': set()}) msg_assertions.assert_message_in_errors( MESSAGES['unknown_reference']['definition'], err.value.detail, '$ref', )
def test_reference_not_found_in_definitions(msg_assertions): with pytest.raises(ValidationError) as err: schema_validator( {'$ref': '#/definitions/UnknownReference'}, context={'definitions': set()}, ) msg_assertions.assert_message_in_errors( MESSAGES['reference']['undefined'], err.value.detail, '$ref', )
def test_with_valid_reference(msg_assertions): try: schema_validator({'$ref': 'SomeReference'}, context={'definitions': {'SomeReference'}}) except ValidationError as err: errors = err.detail else: errors = {} msg_assertions.assert_path_not_in_errors( '$ref', errors, )
def test_with_valid_external_reference(msg_assertions): try: schema_validator({'$ref': 'jsonschemas/ext.json#'}, context={}, base_path=DIR) except ValidationError as err: errors = err.detail else: errors = {} msg_assertions.assert_path_not_in_errors( '$ref', errors, )
def test_with_valid_reference(msg_assertions): try: schema_validator( {'$ref': '#/definitions/SomeReference'}, context={ 'definitions': { 'SomeReference': {'type': OBJECT}, }, }, ) except ValidationError as err: errors = err.detail else: errors = {} msg_assertions.assert_path_not_in_errors( '$ref', errors, )
def validate(raw_schema, target=None, **kwargs): """ Given the python representation of a JSONschema as defined in the swagger spec, validate that the schema complies to spec. If `target` is provided, that target will be validated against the provided schema. """ schema = schema_validator(raw_schema, **kwargs) if target is not None: validate_object(target, schema=schema, **kwargs)
def retype_graph_aux(graph_id): typing_graph = request.args.get("typingGraph") if not typing_graph: return "argument typingGraph is required" relation = request.json try: schema = schema_validator({'$ref': '#/definitions/Matching'}, context=json_schema_context) flex.core.validate(schema, relation, context=json_schema_context) partial_typing = {c["left"]: c["right"] for c in relation} except ValueError as e: return (str(e), 404) kappa.add_nugget_to_action_graph( hie, graph_id, get_node_id(hie, kami_blueprint.top, typing_graph), partial_typing) return ("graph moved successfully", 200)
def generate_validator_from_schema(raw_schema, **kwargs): schema = schema_validator(raw_schema, **kwargs) validator = functools.partial(validate_object, schema=schema, **kwargs) return validator