Skip to content

Commit 4476b05

Browse files
authored
Optimize schema validation by eliminating redundant JSON roundtrip (#4871)
1 parent 2b4bb5f commit 4476b05

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

pkg/workflow/schema_validation.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// - sync.Once ensures schema is compiled only once
1818
// - Schema is embedded in the binary as githubWorkflowSchema
1919
// - Cached compiled schema is reused across all validations
20-
// - YAML is converted to JSON for schema validation
20+
// - YAML is parsed directly and validated without JSON conversion
2121
//
2222
// # Schema Source
2323
//
@@ -93,31 +93,23 @@ func getCompiledSchema() (*jsonschema.Schema, error) {
9393
// validateGitHubActionsSchema validates the generated YAML content against the GitHub Actions workflow schema
9494
func (c *Compiler) validateGitHubActionsSchema(yamlContent string) error {
9595
schemaValidationLog.Print("Validating workflow YAML against GitHub Actions schema")
96-
// Convert YAML to any for JSON conversion
96+
97+
// Parse YAML directly into any type for schema validation
98+
// The jsonschema library accepts any type directly, no JSON conversion needed
9799
var workflowData any
98100
if err := yaml.Unmarshal([]byte(yamlContent), &workflowData); err != nil {
99101
return fmt.Errorf("failed to parse YAML for schema validation: %w", err)
100102
}
101103

102-
// Convert to JSON for schema validation
103-
jsonData, err := json.Marshal(workflowData)
104-
if err != nil {
105-
return fmt.Errorf("failed to convert YAML to JSON for validation: %w", err)
106-
}
107-
108104
// Get the cached compiled schema
109105
schema, err := getCompiledSchema()
110106
if err != nil {
111107
return err
112108
}
113109

114-
// Validate the JSON data against the schema
115-
var jsonObj any
116-
if err := json.Unmarshal(jsonData, &jsonObj); err != nil {
117-
return fmt.Errorf("failed to unmarshal JSON for validation: %w", err)
118-
}
119-
120-
if err := schema.Validate(jsonObj); err != nil {
110+
// Validate the parsed YAML data directly against the schema
111+
// No JSON roundtrip required - the schema.Validate() accepts any type
112+
if err := schema.Validate(workflowData); err != nil {
121113
// Enhance error message with field-specific examples
122114
enhancedErr := enhanceSchemaValidationError(err)
123115
schemaValidationLog.Printf("Schema validation failed: %v", enhancedErr)

0 commit comments

Comments
 (0)