MCPcopy Create free account
hub / github.com/astercloud/aster / validateType

Method validateType

pkg/memory/schema.go:125–165  ·  view source on GitHub ↗

validateType 验证值的类型

(value any)

Source from the content-addressed store, hash-verified

123
124// validateType 验证值的类型
125func (s *JSONSchema) validateType(value any) error {
126 switch s.Type {
127 case "object":
128 if _, ok := value.(map[string]any); !ok {
129 return fmt.Errorf("expected object, got %T", value)
130 }
131 case "array":
132 if _, ok := value.([]any); !ok {
133 return fmt.Errorf("expected array, got %T", value)
134 }
135 case "string":
136 if _, ok := value.(string); !ok {
137 return fmt.Errorf("expected string, got %T", value)
138 }
139 case "number":
140 switch value.(type) {
141 case float64, float32, int, int64, int32:
142 // OK
143 default:
144 return fmt.Errorf("expected number, got %T", value)
145 }
146 case "integer":
147 switch value.(type) {
148 case int, int64, int32, float64:
149 // Go JSON 解析会把整数解析为 float64,需要额外检查
150 if f, ok := value.(float64); ok {
151 if f != float64(int64(f)) {
152 return fmt.Errorf("expected integer, got float %v", f)
153 }
154 }
155 default:
156 return fmt.Errorf("expected integer, got %T", value)
157 }
158 case "boolean":
159 if _, ok := value.(bool); !ok {
160 return fmt.Errorf("expected boolean, got %T", value)
161 }
162 }
163
164 return nil
165}
166
167// validateObject 验证对象
168func (s *JSONSchema) validateObject(value any) error {

Callers 1

validateValueMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected