validateServerJSONSchema validates the server JSON against the schema version specified in $schema using jsonschema Empty/missing schema always produces an error. If performValidation is true, performs full JSON Schema validation. If performValidation is false, only checks for empty schema (always a
(serverJSON *apiv0.ServerJSON, performValidation bool, nonCurrentPolicy SchemaVersionPolicy)
| 54 | // If performValidation is false, only checks for empty schema (always an error) and handles non-current schemas per policy. |
| 55 | // nonCurrentPolicy determines how non-current (but valid) schema versions are handled when performValidation is true. |
| 56 | func validateServerJSONSchema(serverJSON *apiv0.ServerJSON, performValidation bool, nonCurrentPolicy SchemaVersionPolicy) *ValidationResult { |
| 57 | result := &ValidationResult{Valid: true, Issues: []ValidationIssue{}} |
| 58 | ctx := &ValidationContext{} |
| 59 | |
| 60 | // Empty/missing schema is always an error |
| 61 | if serverJSON.Schema == "" { |
| 62 | issue := NewValidationIssue( |
| 63 | ValidationIssueTypeSemantic, |
| 64 | ctx.Field("schema").String(), |
| 65 | "$schema field is required", |
| 66 | ValidationIssueSeverityError, |
| 67 | "schema-field-required", |
| 68 | ) |
| 69 | result.AddIssue(issue) |
| 70 | return result |
| 71 | } |
| 72 | |
| 73 | // Extract version from the schema URL |
| 74 | version, err := extractVersionFromSchemaURL(serverJSON.Schema) |
| 75 | if err != nil { |
| 76 | issue := NewValidationIssue( |
| 77 | ValidationIssueTypeSchema, |
| 78 | ctx.Field("schema").String(), |
| 79 | fmt.Sprintf("failed to extract schema version from URL: %v", err), |
| 80 | ValidationIssueSeverityError, |
| 81 | "schema-version-extraction-error", |
| 82 | ) |
| 83 | result.AddIssue(issue) |
| 84 | return result |
| 85 | } |
| 86 | |
| 87 | // Check if the schema version is the current one and handle based on policy |
| 88 | currentSchemaURL, err := GetCurrentSchemaVersion() |
| 89 | if err == nil && serverJSON.Schema != currentSchemaURL { |
| 90 | // Extract current version for the message |
| 91 | currentVersion, _ := extractVersionFromSchemaURL(currentSchemaURL) |
| 92 | |
| 93 | switch nonCurrentPolicy { |
| 94 | case SchemaVersionPolicyError: |
| 95 | issue := NewValidationIssue( |
| 96 | ValidationIssueTypeSemantic, |
| 97 | ctx.Field("schema").String(), |
| 98 | fmt.Sprintf("schema version %s is not the current version (%s). Use the current schema version", version, currentVersion), |
| 99 | ValidationIssueSeverityError, |
| 100 | "schema-version-deprecated", |
| 101 | ) |
| 102 | result.AddIssue(issue) |
| 103 | case SchemaVersionPolicyWarn: |
| 104 | issue := NewValidationIssue( |
| 105 | ValidationIssueTypeSemantic, |
| 106 | ctx.Field("schema").String(), |
| 107 | fmt.Sprintf("schema version %s is not the current version (%s). Consider updating to the latest schema version", version, currentVersion), |
| 108 | ValidationIssueSeverityWarning, |
| 109 | "schema-version-deprecated", |
| 110 | ) |
| 111 | result.AddIssue(issue) |
| 112 | case SchemaVersionPolicyAllow: |
| 113 | // No issue added - allow non-current schemas silently |
no test coverage detected
searching dependent graphs…