diff --git a/CHANGELOG.md b/CHANGELOG.md index fb378e2..fe7d68c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Support for refactored schema where root-level properties are moved to be under global property. + ## [2.2.1] - 2023-11-09 ## [2.2.0] - 2023-11-09 diff --git a/pkg/lint/rules/adheres_to_common_schema_structure_requirements.go b/pkg/lint/rules/adheres_to_common_schema_structure_requirements.go index 4817632..c865fb7 100644 --- a/pkg/lint/rules/adheres_to_common_schema_structure_requirements.go +++ b/pkg/lint/rules/adheres_to_common_schema_structure_requirements.go @@ -13,11 +13,19 @@ func (r AdheresToCommonSchemaStructureRequirements) Verify( requiredProperties := getRequiredRootProperties() - schemaProperties := s.GetProperties() + rootSchemaProperties := s.GetProperties() + var globalSchemaProperties map[string]*schema.ExtendedSchema + if globalSchema, ok := rootSchemaProperties["global"]; ok { + globalSchemaProperties = globalSchema.GetProperties() + } + for _, requiredProperty := range requiredProperties { - if _, ok := schemaProperties[requiredProperty]; !ok { + _, requiredPropertyFound := rootSchemaProperties[requiredProperty] + _, globalRequiredPropertyFound := globalSchemaProperties[requiredProperty] + + if !requiredPropertyFound && !globalRequiredPropertyFound || requiredPropertyFound && globalRequiredPropertyFound { ruleResults.Add( - "Root-level property must be present", + "Either global or root-level property must be present (both are not allowed)", "/properties/"+requiredProperty, ) } @@ -25,7 +33,7 @@ func (r AdheresToCommonSchemaStructureRequirements) Verify( } allAllowedRootProperties := getAllAllowedRootPropertiesNamesSet() - for key, s := range schemaProperties { + for key, s := range rootSchemaProperties { if _, ok := allAllowedRootProperties[key]; !ok { ruleResults.Add( "Root-level property is not allowed", diff --git a/pkg/lint/rules/adheres_to_common_schema_structure_requirements_test.go b/pkg/lint/rules/adheres_to_common_schema_structure_requirements_test.go index d933d7e..bd46b25 100644 --- a/pkg/lint/rules/adheres_to_common_schema_structure_requirements_test.go +++ b/pkg/lint/rules/adheres_to_common_schema_structure_requirements_test.go @@ -57,6 +57,21 @@ func TestAdheheresToCommonSchemaStructureRequirements(t *testing.T) { schemaPath: "testdata/common_schema_structure_requirements/additional_properties_not_set_to_false.json", nViolations: 1, }, + { + name: "case 9: correct global properties", + schemaPath: "testdata/common_schema_structure_requirements/correct_global.json", + nViolations: 0, + }, + { + name: "case 10: correct global and root-level properties mix (allowed during schema refactoring, will be removed in the future)", + schemaPath: "testdata/common_schema_structure_requirements/correct_global_root_mix.json", + nViolations: 0, + }, + { + name: "case 11: incorrect global and root-level properties mix", + schemaPath: "testdata/common_schema_structure_requirements/incorrect_global_root_mix.json", + nViolations: 1, + }, } for _, tc := range testCases { diff --git a/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global.json b/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global.json new file mode 100644 index 0000000..a551026 --- /dev/null +++ b/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global.json @@ -0,0 +1,25 @@ +{ + "$id": "https://example.com/person.schema.json", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "additionalProperties": false, + "properties": { + "global": { + "type": "object", + "properties": { + "connectivity": { + "type": "object" + }, + "controlPlane": { + "type": "object" + }, + "metadata": { + "type": "object" + }, + "nodePools": { + "type": "array" + } + } + } + } +} diff --git a/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global_root_mix.json b/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global_root_mix.json new file mode 100644 index 0000000..7098de6 --- /dev/null +++ b/pkg/lint/rules/testdata/common_schema_structure_requirements/correct_global_root_mix.json @@ -0,0 +1,25 @@ +{ + "$id": "https://example.com/person.schema.json", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "additionalProperties": false, + "properties": { + "controlPlane": { + "type": "object" + }, + "global": { + "type": "object", + "properties": { + "connectivity": { + "type": "object" + }, + "metadata": { + "type": "object" + } + } + }, + "nodePools": { + "type": "array" + } + } +} diff --git a/pkg/lint/rules/testdata/common_schema_structure_requirements/incorrect_global_root_mix.json b/pkg/lint/rules/testdata/common_schema_structure_requirements/incorrect_global_root_mix.json new file mode 100644 index 0000000..62d0a5f --- /dev/null +++ b/pkg/lint/rules/testdata/common_schema_structure_requirements/incorrect_global_root_mix.json @@ -0,0 +1,28 @@ +{ + "$id": "https://example.com/person.schema.json", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "additionalProperties": false, + "properties": { + "controlPlane": { + "type": "object" + }, + "global": { + "type": "object", + "properties": { + "connectivity": { + "type": "object" + }, + "metadata": { + "type": "object" + } + } + }, + "metadata": { + "type": "object" + }, + "nodePools": { + "type": "array" + } + } +}