在实际的接口断言中,碰到一些返回结果是json结构,但是每一次返回的value值都不一样,所以考虑使用jsonschema进行断言。
具体jsonschema的规则可以参考https://www.cnblogs.com/terencezhou/p/10474617.html,一般我们可以通过jsonschema的转换工具https://easy-json-schema.github.io/进行简易的转换,然后在上面基础上进行结构的调整
在golang中,我们使用的是gojsonschema,导包:go get github.com/xeipuuv/gojsonschema

func JsonschemaAssert(schemaFile string ,postResult string) bool{
   b1, err := ioutil.ReadFile(schemaFile)//读取文件
   if err != nil {
      panic(err.Error())
   }
 
 
   schemaLoader := gojsonschema.NewStringLoader(string(b1)) // jsonschema格式
   documentLoader := gojsonschema.NewStringLoader(postResult) // 待校验的json数据
 
   result, err := gojsonschema.Validate(schemaLoader, documentLoader)
   if err != nil {
      panic(err.Error())
   }
 
   if result.Valid() {
      fmt.Printf("The document is valid\n")
      return true
   } else {
      fmt.Printf("The document is not valid. see errors :\n")
      for _, desc := range result.Errors() {
         fmt.Printf("- %s\n", desc)
      }
      return false
   }
}