schema_node.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package yaml_checker
  2. import (
  3. "github.com/pkg/errors"
  4. "reflect"
  5. )
  6. type SchemaNode interface {
  7. Validate(yamlMap map[string]any) error
  8. }
  9. type BaseNamingSchemaNode struct {
  10. Type string
  11. Name string
  12. Describe string
  13. Required bool
  14. }
  15. type ObjectSchemaNode struct {
  16. BaseNamingSchemaNode
  17. AsArray bool
  18. Fields []SchemaNode
  19. }
  20. func (schema *ObjectSchemaNode) Validate(yamlMap map[string]any) error {
  21. yamlValue, ok := yamlMap[schema.Name]
  22. if !ok {
  23. if schema.Required {
  24. return errors.New(schemaNodeTypeObject + "类型的yaml节点必须有" + schema.Name)
  25. }
  26. return nil
  27. }
  28. if yamlValue == nil {
  29. return errors.New(schemaNodeTypeObject + "类型的yaml节点的" + schema.Name + "字段没有赋值")
  30. }
  31. if schema.AsArray {
  32. sliceParamValues, ok := yamlValue.([]any)
  33. if !ok {
  34. return errors.New(schemaNodeTypeObject + "类型节点" + schema.Name + "的as_array为true,但是值不是数组")
  35. }
  36. for _, sliceParamValue := range sliceParamValues {
  37. sliceParamMapValue, ok := sliceParamValue.(map[string]any)
  38. if !ok {
  39. return errors.New(schemaNodeTypeObject + "类型节点" + schema.Name + "的值类型转换失败")
  40. }
  41. err := schema.validateValue(sliceParamMapValue)
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. yamlMapValue, ok := yamlValue.(map[string]any)
  49. if !ok {
  50. return errors.New(schemaNodeTypeObject + "类型节点不是键值对")
  51. }
  52. err := schema.validateValue(yamlMapValue)
  53. if err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. func (schema *ObjectSchemaNode) validateValue(yamlMap map[string]any) error {
  59. for _, field := range schema.Fields {
  60. err := field.Validate(yamlMap)
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }
  67. type FieldSchemaNode struct {
  68. BaseNamingSchemaNode
  69. AsArray bool
  70. FieldType string
  71. }
  72. func (schema *FieldSchemaNode) Validate(yamlMap map[string]any) error {
  73. yamlValue, ok := yamlMap[schema.Name]
  74. if !ok {
  75. if schema.Required {
  76. return errors.New(schemaNodeTypeField + "类型的yaml节点必须有" + schema.Name)
  77. }
  78. return nil
  79. }
  80. if yamlValue == nil {
  81. return errors.New(schemaNodeTypeField + "类型的yaml节点的" + schema.Name + "字段没有赋值")
  82. }
  83. if schema.AsArray {
  84. sliceParamValues, ok := yamlValue.([]any)
  85. if !ok {
  86. return errors.New(schemaNodeTypeField + "类型节点as_array为true,但是值不是数组")
  87. }
  88. for _, sliceParamValue := range sliceParamValues {
  89. err := schema.validateValue(sliceParamValue)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. }
  96. err := schema.validateValue(yamlValue)
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func (schema *FieldSchemaNode) validateValue(paramValue any) error {
  103. valueType := reflect.TypeOf(paramValue).String()
  104. if valueType != schema.FieldType {
  105. return errors.New(schemaNodeTypeField + "类型节点的" + schema.Name + "字段期望类型为: " + schema.FieldType + " 实际类型为: " + valueType)
  106. }
  107. return nil
  108. }