Created
December 3, 2024 22:16
-
-
Save brandtkeller/4b998b2630235b8c3787da9a4a44fb0f to your computer and use it in GitHub Desktop.
json schema validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"github.com/qri-io/jsonschema" | |
) | |
// ValidateJSONSchema validates if a given JSON schema file is valid. | |
func ValidateJSONSchema(filePath string) error { | |
// Read the schema file | |
schemaData, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
return fmt.Errorf("failed to read schema file: %w", err) | |
} | |
// Parse the schema into a jsonschema.Schema | |
var rawSchema map[string]interface{} | |
if err := json.Unmarshal(schemaData, &rawSchema); err != nil { | |
return fmt.Errorf("failed to parse schema: %w", err) | |
} | |
schemaLoader := &jsonschema.Schema{} | |
if err := schemaLoader.UnmarshalJSON(schemaData); err != nil { | |
return fmt.Errorf("invalid JSON schema: %w", err) | |
} | |
return nil | |
} | |
func main() { | |
// Path to a valid schema file | |
validSchemaPath := "../lula-nacx/src/pkg/common/schemas/validation.json" | |
if err := ValidateJSONSchema(validSchemaPath); err != nil { | |
fmt.Println("Schema is invalid:", err) | |
} else { | |
fmt.Println("Schema is valid") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment