Last active
May 21, 2024 14:56
-
-
Save proppy/17b559c490fee8c41a85 to your computer and use it in GitHub Desktop.
validate yaml file with gojsonschema
This file contains hidden or 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
containers: | |
- image: "dockerfile/nginx" | |
ports: | |
- containerPort: 80 | |
hostPort: 8080 |
This file contains hidden or 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
diff --git a/validation.go b/validation.go | |
index 04f1227..053b4c5 100644 | |
--- a/validation.go | |
+++ b/validation.go | |
@@ -47,6 +47,24 @@ func (v *jsonSchema) Validate(document interface{}, context *jsonContext) *Valid | |
return result | |
} | |
+func convertDocumentNode(val interface{}) interface{} { | |
+ if lval, ok := val.([]interface{}); ok { | |
+ res := []interface{}{} | |
+ for _, v := range lval { | |
+ res = append(res, convertDocumentNode(v)) | |
+ } | |
+ return res | |
+ } | |
+ if mval, ok := val.(map[interface{}]interface{}); ok { | |
+ res := map[string]interface{}{} | |
+ for k, v := range mval { | |
+ res[k.(string)] = convertDocumentNode(v) | |
+ } | |
+ return res | |
+ } | |
+ return val | |
+} | |
+ | |
// Walker function to validate the json recursively against the schema | |
func (v *jsonSchema) validateRecursive(currentSchema *jsonSchema, currentNode interface{}, result *ValidationResult, context *jsonContext) { | |
@@ -98,7 +116,10 @@ func (v *jsonSchema) validateRecursive(currentSchema *jsonSchema, currentNode in | |
return | |
} | |
- castCurrentNode := currentNode.(map[string]interface{}) | |
+ castCurrentNode, ok := currentNode.(map[string]interface{}) | |
+ if !ok { | |
+ castCurrentNode = convertDocumentNode(currentNode).(map[string]interface{}) | |
+ } | |
currentSchema.validateSchema(currentSchema, castCurrentNode, result, context) | |
This file contains hidden or 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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"github.com/sigu-399/gojsonschema" | |
"gopkg.in/v1/yaml" | |
) | |
func main() { | |
// Loads a JSON schema locally | |
jsonSchema, err := gojsonschema.GetFileJson("manifest-schema.json") | |
schemaDocument, err := gojsonschema.NewJsonSchemaDocument(jsonSchema) | |
if err != nil { | |
panic(err.Error()) | |
} | |
var document map[interface{}]interface{} | |
data, err := ioutil.ReadFile("containers.yaml") | |
if err != nil { | |
log.Fatalf("unable to read file: %v", err) | |
} | |
if err := yaml.Unmarshal(data, &document); err != nil { | |
log.Fatalf("unable to parse yaml: %v", err) | |
} | |
// Try to validate the YAML against the schema | |
result := schemaDocument.Validate(document) | |
// Deal with result | |
if result.Valid() { | |
fmt.Printf("The document is valid\n") | |
} else { | |
fmt.Printf("The document is not valid. see errors :\n") | |
// Loop through errors | |
for _, desc := range result.Errors() { | |
fmt.Printf("- %s\n", desc) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment