用于json模式验证的PyHamcrest matchers扩展
json-schema-matchers的Python项目详细描述
json架构匹配器
用于json模式验证的PyHamcrest matchers扩展架构应该按照Draft-07 to 2019-09版本的语法实现。 架构示例:
single_user_schema={"title":"Single User Info","type":"object","properties":{"first_name":{"type":"string","minLength":3},"last_name":{"type":"string","minLength":3},"phone_number":{"type":"number"}},"required":["first_name","last_name",]}all_users_info_schema={"title":"All Users Info","type":"array","items":single_user_schema}
然后您可以将matches_json_schema
匹配器与所有hamcrest匹配器一起使用:
如果验证失败,匹配器将迭代返回所有不匹配项:
users_list_json_obj=[{"first_name":'John',"last_name":"Johnson","phone_number":'123456789'},{"first_name":None,"last_name":'Je'}]assert_that(users_list_json_obj,matches_json_schema(all_users_info_schema))
AssertionError:Expected:JSONobjectshouldmatchschema"All Users Info"but:mismatchesoccurred:'123456789'isnotoftype'number'Failedvalidating'type'inschema['items']['properties']['phone_number']:{'type':'number'}Oninstance[0]['phone_number']:'123456789'------------'Je'istooshortFailedvalidating'minLength'inschema['items']['properties']['last_name']:{'minLength':3,'type':'string'}Oninstance[1]['last_name']:'Je'------------Noneisnotoftype'string'Failedvalidating'type'inschema['items']['properties']['first_name']:{'minLength':3,'type':'string'}Oninstance[1]['first_name']:None------------
依赖关系:
jsonschema==3.2.0 PyHamcrest==2.0.2
- 项目
标签: