- 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) JSONLoaderfunc 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() []ResultErrorfunc (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.firstNameField() string// SetType sets the error-typeSetType(string)// Type returns the error-typeType() string// SetContext sets the JSON-context for the errorSetContext(*JsonContext)// Context returns the JSON-context of the errorContext() *JsonContext// SetDescription sets a description for the errorSetDescription(string)// Description returns the description of the errorDescription() string// SetDescriptionFormat sets the format for the description in the default text/template formatSetDescriptionFormat(string)// DescriptionFormat returns the format for the description in the default text/template formatDescriptionFormat() string// SetValue sets the value related to the errorSetValue(interface{})// Value returns the value related to the errorValue() interface{}// SetDetails sets the details specific to the errorSetDetails(ErrorDetails)// Details returns details about the errorDetails() ErrorDetails// String returns a string representation of the errorString() string}
Demo
package mainimport ("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_]+$'
