Skip to content

Commit

Permalink
Add support for root-level properties being moved to be under global
Browse files Browse the repository at this point in the history
  • Loading branch information
nprokopic committed Nov 12, 2023
1 parent 98972a9 commit c3c322b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions pkg/lint/rules/adheres_to_common_schema_structure_requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@ 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,
)
}

}

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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit c3c322b

Please sign in to comment.