文档:https://pkg.go.dev/github.com/xeipuuv/gojsonschema

type JSONLoader

  1. type JSONLoader interface {
  2. JsonSource() interface{}
  3. LoadJSON() (interface{}, error)
  4. JsonReference() (gojsonreference.JsonReference, error)
  5. LoaderFactory() JSONLoaderFactory
  6. }
  7. func NewBytesLoader(source []byte) JSONLoader
  8. func NewStringLoader(source string) JSONLoader

type Schema

  1. type Schema struct {}
  2. func NewSchema(l JSONLoader) (*Schema, error)
  3. func (d *Schema) SetRootSchemaName(name string)
  4. func (v *Schema) Validate(l JSONLoader) (*Result, error)

type Result

  1. type Result struct {}
  2. func Validate(ls JSONLoader, ld JSONLoader) (*Result, error)
  3. func (v *Result) Errors() []ResultError
  4. func (v *Result) Valid() bool // 是否没发现错误

type ResultError

  1. type ResultError interface {
  2. // Field returns the field name without the root context
  3. // i.e. firstName or person.firstName instead of (root).firstName or (root).person.firstName
  4. Field() string
  5. // SetType sets the error-type
  6. SetType(string)
  7. // Type returns the error-type
  8. Type() string
  9. // SetContext sets the JSON-context for the error
  10. SetContext(*JsonContext)
  11. // Context returns the JSON-context of the error
  12. Context() *JsonContext
  13. // SetDescription sets a description for the error
  14. SetDescription(string)
  15. // Description returns the description of the error
  16. Description() string
  17. // SetDescriptionFormat sets the format for the description in the default text/template format
  18. SetDescriptionFormat(string)
  19. // DescriptionFormat returns the format for the description in the default text/template format
  20. DescriptionFormat() string
  21. // SetValue sets the value related to the error
  22. SetValue(interface{})
  23. // Value returns the value related to the error
  24. Value() interface{}
  25. // SetDetails sets the details specific to the error
  26. SetDetails(ErrorDetails)
  27. // Details returns details about the error
  28. Details() ErrorDetails
  29. // String returns a string representation of the error
  30. String() string
  31. }

Demo

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/tidwall/gjson"
  5. "github.com/xeipuuv/gojsonschema"
  6. )
  7. var schemaString = `
  8. {
  9. "customize": {
  10. "system_config": {
  11. "properties": {
  12. "config_name": {
  13. "maxLength":100,
  14. "minLength":1,
  15. "pattern":"^[a-zA-Z0-9_]+$",
  16. "type":"string"
  17. },
  18. "desc": {
  19. "maxLength":256,
  20. "type":"string"
  21. },
  22. "payload": {
  23. "type":"object",
  24. "minProperties":1
  25. },
  26. "create_time": {
  27. "type":"integer"
  28. },
  29. "update_time": {
  30. "type":"integer"
  31. }
  32. },
  33. "required": [
  34. "config_name",
  35. "payload"
  36. ],
  37. "type":"object"
  38. }
  39. }
  40. }
  41. `
  42. type SystemConfig struct {
  43. ConfigName string `json:"config_name"`
  44. Desc string `json:"desc,omitempty"`
  45. Payload map[string]interface{} `json:"payload,omitempty"`
  46. }
  47. func main() {
  48. objString := gjson.Get(schemaString, "customize.system_config").String()
  49. schemaLoader := gojsonschema.NewStringLoader(objString)
  50. schema, err := gojsonschema.NewSchema(schemaLoader)
  51. if err != nil {
  52. panic(err)
  53. }
  54. var sc = SystemConfig{
  55. ConfigName: "",
  56. Payload: nil,
  57. }
  58. goLoader := gojsonschema.NewGoLoader(sc)
  59. // gojsonschema.Validate 每次 new 一个 schema, 效率低下
  60. // result, err := gojsonschema.Validate(schemaLoader, goLoader)
  61. result, err := schema.Validate(goLoader)
  62. if err != nil {
  63. panic(err.Error())
  64. }
  65. if result.Valid() {
  66. fmt.Printf("The document is valid\n")
  67. } else {
  68. fmt.Printf("The document is not valid. see errors :\n")
  69. for _, desc := range result.Errors() {
  70. fmt.Printf("- %s\n", desc)
  71. }
  72. }
  73. }
  74. -----------------------------------------------------------------------------
  75. The document is not valid. see errors :
  76. - (root): payload is required
  77. - config_name: String length must be greater than or equal to 1
  78. - config_name: Does not match pattern '^[a-zA-Z0-9_]+$'