Validate 验证 Schema 本身是否合法
()
| 22 | |
| 23 | // Validate 验证 Schema 本身是否合法 |
| 24 | func (s *JSONSchema) Validate() error { |
| 25 | if s == nil { |
| 26 | return errors.New("schema cannot be nil") |
| 27 | } |
| 28 | |
| 29 | validTypes := map[string]bool{ |
| 30 | "object": true, "string": true, "number": true, |
| 31 | "integer": true, "boolean": true, "array": true, |
| 32 | } |
| 33 | |
| 34 | if s.Type != "" && !validTypes[s.Type] { |
| 35 | return fmt.Errorf("invalid type: %s", s.Type) |
| 36 | } |
| 37 | |
| 38 | // 验证 object 类型的 properties |
| 39 | if s.Type == "object" && s.Properties != nil { |
| 40 | for name, prop := range s.Properties { |
| 41 | if err := prop.Validate(); err != nil { |
| 42 | return fmt.Errorf("invalid property %s: %w", name, err) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // 验证 array 类型的 items |
| 48 | if s.Type == "array" && s.Items != nil { |
| 49 | if err := s.Items.Validate(); err != nil { |
| 50 | return fmt.Errorf("invalid items: %w", err) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // ValidateContent 验证内容是否符合 Schema |
| 58 | // content: JSON 字符串或 Markdown 字符串 |
no test coverage detected