CompileSchema compiles a JSON schema from a JSON string.
(schemaJSON, schemaURL string)
| 119 | |
| 120 | // CompileSchema compiles a JSON schema from a JSON string. |
| 121 | func CompileSchema(schemaJSON, schemaURL string) (*jsonschema.Schema, error) { |
| 122 | schemaCompilerLog.Printf("Compiling JSON schema: %s", schemaURL) |
| 123 | |
| 124 | // Create a new compiler |
| 125 | compiler := jsonschema.NewCompiler() |
| 126 | |
| 127 | // Parse the schema JSON first |
| 128 | var schemaDoc any |
| 129 | if err := json.Unmarshal([]byte(schemaJSON), &schemaDoc); err != nil { |
| 130 | return nil, fmt.Errorf("failed to parse schema JSON: %w", err) |
| 131 | } |
| 132 | |
| 133 | // Add the schema as a resource |
| 134 | if err := compiler.AddResource(schemaURL, schemaDoc); err != nil { |
| 135 | return nil, fmt.Errorf("failed to add schema resource: %w", err) |
| 136 | } |
| 137 | |
| 138 | // Compile the schema |
| 139 | schema, err := compiler.Compile(schemaURL) |
| 140 | if err != nil { |
| 141 | return nil, fmt.Errorf("failed to compile schema: %w", err) |
| 142 | } |
| 143 | |
| 144 | return schema, nil |
| 145 | } |
| 146 | |
| 147 | // compileSchema preserves existing package-local call sites. |
| 148 | func compileSchema(schemaJSON, schemaURL string) (*jsonschema.Schema, error) { |