- JSONLoader">type JSONLoader
- Schema">type Schema
- Result">type Result
- ResultError">type ResultError
- Demo
文档:https://pkg.go.dev/github.com/xeipuuv/gojsonschema
type JSONLoader
type JSONLoader interface {
JsonSource() interface{}
LoadJSON() (interface{}, error)
JsonReference() (gojsonreference.JsonReference, error)
LoaderFactory() JSONLoaderFactory
}
func NewBytesLoader(source []byte) JSONLoader
func NewStringLoader(source string) JSONLoader
type Schema
type Schema struct {}
func NewSchema(l JSONLoader) (*Schema, error)
func (d *Schema) SetRootSchemaName(name string)
func (v *Schema) Validate(l JSONLoader) (*Result, error)
type Result
type Result struct {}
func Validate(ls JSONLoader, ld JSONLoader) (*Result, error)
func (v *Result) Errors() []ResultError
func (v *Result) Valid() bool // 是否没发现错误
type ResultError
type ResultError interface {
// Field returns the field name without the root context
// i.e. firstName or person.firstName instead of (root).firstName or (root).person.firstName
Field() string
// SetType sets the error-type
SetType(string)
// Type returns the error-type
Type() string
// SetContext sets the JSON-context for the error
SetContext(*JsonContext)
// Context returns the JSON-context of the error
Context() *JsonContext
// SetDescription sets a description for the error
SetDescription(string)
// Description returns the description of the error
Description() string
// SetDescriptionFormat sets the format for the description in the default text/template format
SetDescriptionFormat(string)
// DescriptionFormat returns the format for the description in the default text/template format
DescriptionFormat() string
// SetValue sets the value related to the error
SetValue(interface{})
// Value returns the value related to the error
Value() interface{}
// SetDetails sets the details specific to the error
SetDetails(ErrorDetails)
// Details returns details about the error
Details() ErrorDetails
// String returns a string representation of the error
String() string
}
Demo
package main
import (
"fmt"
"github.com/tidwall/gjson"
"github.com/xeipuuv/gojsonschema"
)
var schemaString = `
{
"customize": {
"system_config": {
"properties": {
"config_name": {
"maxLength":100,
"minLength":1,
"pattern":"^[a-zA-Z0-9_]+$",
"type":"string"
},
"desc": {
"maxLength":256,
"type":"string"
},
"payload": {
"type":"object",
"minProperties":1
},
"create_time": {
"type":"integer"
},
"update_time": {
"type":"integer"
}
},
"required": [
"config_name",
"payload"
],
"type":"object"
}
}
}
`
type SystemConfig struct {
ConfigName string `json:"config_name"`
Desc string `json:"desc,omitempty"`
Payload map[string]interface{} `json:"payload,omitempty"`
}
func main() {
objString := gjson.Get(schemaString, "customize.system_config").String()
schemaLoader := gojsonschema.NewStringLoader(objString)
schema, err := gojsonschema.NewSchema(schemaLoader)
if err != nil {
panic(err)
}
var sc = SystemConfig{
ConfigName: "",
Payload: nil,
}
goLoader := gojsonschema.NewGoLoader(sc)
// gojsonschema.Validate 每次 new 一个 schema, 效率低下
// result, err := gojsonschema.Validate(schemaLoader, goLoader)
result, err := schema.Validate(goLoader)
if err != nil {
panic(err.Error())
}
if result.Valid() {
fmt.Printf("The document is valid\n")
} else {
fmt.Printf("The document is not valid. see errors :\n")
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
}
}
-----------------------------------------------------------------------------
The document is not valid. see errors :
- (root): payload is required
- config_name: String length must be greater than or equal to 1
- config_name: Does not match pattern '^[a-zA-Z0-9_]+$'