(jsonSchema string)
| 156 | } |
| 157 | |
| 158 | func parseJSONSchema(jsonSchema string) (*jsonschema.Schema, error) { |
| 159 | c := jsonschema.NewCompiler() |
| 160 | c.DefaultDraft(jsonschema.Draft2020) |
| 161 | c.AssertFormat() |
| 162 | |
| 163 | s, err := jsonschema.UnmarshalJSON(strings.NewReader(jsonSchema)) |
| 164 | if err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | |
| 168 | if err := c.AddResource("schema.json", s); err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | |
| 172 | sc, err := c.Compile("schema.json") |
| 173 | if err != nil { |
| 174 | var se *jsonschema.SchemaValidationError |
| 175 | if errors.As(err, &se); se != nil && se.Err != nil { |
| 176 | // We add resource as `file`, but there's none, actually. |
| 177 | // So, we need to prettify message a bit. |
| 178 | return nil, fmt.Errorf("jsonschema compilation failed: %w", |
| 179 | errors.New(strings.Replace(se.Err.Error(), "jsonschema: '' ", "", 1))) |
| 180 | } |
| 181 | return nil, err |
| 182 | } |
| 183 | |
| 184 | return sc, nil |
| 185 | } |
| 186 | |
| 187 | func marshalSpec(spec map[string]any) ([]byte, error) { |
| 188 | // All nil or empty values to be marshaled as null |
no test coverage detected