diff --git a/Gatekeeper/Schemas/FeatureFlag.json b/Gatekeeper/Schemas/FeatureFlag.json index 0d2f1d0..9efe1ba 100644 --- a/Gatekeeper/Schemas/FeatureFlag.json +++ b/Gatekeeper/Schemas/FeatureFlag.json @@ -1,6 +1,6 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://example.com/schema/rule-schema.json", + "$id": "https://heyitsgilbert.github.io/Gatekeeper/schema/1.0.0/FeatureFlag.json", "title": "Evaluation Rule Set", "type": "object", "required": [ @@ -38,8 +38,7 @@ "type": "array", "items": { "$ref": "#/$defs/rule" - }, - "minItems": 1 + } } }, "$defs": { @@ -115,7 +114,11 @@ ], "properties": { "Not": { - "$ref": "#/$defs/condition" + "type": "array", + "items": { + "$ref": "#/$defs/condition" + }, + "minItems": 1 } }, "additionalProperties": false @@ -136,12 +139,18 @@ "Equals", "NotEquals", "GreaterThan", + "GreaterThanOrEqual", "LessThan", + "LessThanOrEqual", "In", "NotIn" ] }, - "Value": {} + "Value": { + "not": { + "type": "null" + } + } }, "additionalProperties": false } diff --git a/Gatekeeper/Schemas/Properties.json b/Gatekeeper/Schemas/Properties.json index 3c10fcd..d4c2485 100644 --- a/Gatekeeper/Schemas/Properties.json +++ b/Gatekeeper/Schemas/Properties.json @@ -1,5 +1,6 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://heyitsgilbert.github.io/Gatekeeper/schema/1.0.0/Properties.json", "title": "Property Metadata Schema", "type": "object", "properties": { @@ -29,10 +30,10 @@ "type": "object", "properties": { "Minimum": { - "type": "number" + "type": "integer" }, "Maximum": { - "type": "number" + "type": "integer" }, "MinLength": { "type": "integer" @@ -48,6 +49,57 @@ "additionalProperties": false } }, - "additionalProperties": false - } + "additionalProperties": false, + "if": { + "properties": { + "Type": { + "const": "integer" + } + } + }, + "then": { + "properties": { + "Validation": { + "properties": { + "MinLength": false, + "MaxLength": false, + "Pattern": false + } + } + } + }, + "else": { + "if": { + "properties": { + "Type": { + "const": "string" + } + } + }, + "then": { + "properties": { + "Validation": { + "properties": { + "Minimum": false, + "Maximum": false + } + } + } + }, + "else": { + "properties": { + "Validation": { + "properties": { + "Minimum": false, + "Maximum": false, + "MinLength": false, + "MaxLength": false, + "Pattern": false + } + } + } + } + } + }, + "$comment": "Minimum > Maximum on the same property is not enforced here: JSON Schema cannot compare two sibling values declaratively (no $data support in Test-Json or the default jsonschema validators). That check is bind-time PowerShell work tracked by ADR 0007 / issue #103 item 21." } diff --git a/tests/Schemas.tests.ps1 b/tests/Schemas.tests.ps1 new file mode 100644 index 0000000..24ed778 --- /dev/null +++ b/tests/Schemas.tests.ps1 @@ -0,0 +1,167 @@ +# Regression tests for the schema corrections made to Gatekeeper/Schemas/*.json +# (see issue #105). These assert the schema *files* directly via Test-Json, so a +# revert of either file - or of any single corrected rule inside it - fails here, +# independent of whatever any particular fixture under tests/fixtures/ happens to +# exercise. +BeforeAll { + $script:featureFlagSchema = Join-Path $PSScriptRoot '..\Gatekeeper\Schemas\FeatureFlag.json' + $script:propertiesSchema = Join-Path $PSScriptRoot '..\Gatekeeper\Schemas\Properties.json' + + function Test-AgainstSchema { + param( + [Parameter(Mandatory)][string]$Json, + [Parameter(Mandatory)][string]$SchemaFile + ) + try { + Test-Json -Json $Json -SchemaFile $SchemaFile -ErrorAction Stop + } catch { + $false + } + } + + # Minimal otherwise-valid FeatureFlag document; $Condition is spliced into the + # single rule's Condition so each case only has to describe the piece under test. + function New-FeatureFlagJson { + param([Parameter(Mandatory)][hashtable]$Condition) + $rule = @{ + Name = 'Rule' + Effect = 'Allow' + Condition = $Condition + } | ConvertTo-Json -Depth 10 -Compress + @{ + Name = 'Flag' + Description = 'Desc' + Version = '1.0.0' + DefaultEffect = 'Deny' + Rules = @(ConvertFrom-Json $rule) + } | ConvertTo-Json -Depth 10 + } +} + +Describe 'FeatureFlag.json' -Skip:($PSVersionTable.PSVersion.Major -lt 7) { + Context 'Rules cardinality' { + It 'Validates an empty Rules array with only a DefaultEffect' { + $json = @{ + Name = 'Flag' + Description = 'Desc' + Version = '1.0.0' + DefaultEffect = 'Allow' + Rules = @() + } | ConvertTo-Json + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeTrue + } + } + + Context 'Operator enum' -ForEach @( + @{ Operator = 'Equals' } + @{ Operator = 'NotEquals' } + @{ Operator = 'GreaterThan' } + @{ Operator = 'GreaterThanOrEqual' } + @{ Operator = 'LessThan' } + @{ Operator = 'LessThanOrEqual' } + @{ Operator = 'In' } + @{ Operator = 'NotIn' } + ) { + It "Validates Operator ''" { + $json = New-FeatureFlagJson -Condition @{ Property = 'Tier'; Operator = $Operator; Value = 1 } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeTrue + } + } + + It 'Rejects an Operator outside the enum' { + $json = New-FeatureFlagJson -Condition @{ Property = 'Tier'; Operator = 'Contains'; Value = 1 } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeFalse + } + + Context 'Not shape' { + It 'Validates a nonempty array-shaped Not (NOR semantics, ADR 0003)' { + $json = New-FeatureFlagJson -Condition @{ + Not = @(@{ Property = 'Tier'; Operator = 'Equals'; Value = 1 }) + } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeTrue + } + + It 'Validates a multi-element array-shaped Not' { + $json = New-FeatureFlagJson -Condition @{ + Not = @( + @{ Property = 'Tier'; Operator = 'Equals'; Value = 1 } + @{ Property = 'Tier'; Operator = 'Equals'; Value = 2 } + ) + } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeTrue + } + + It 'Rejects the pre-correction single-object Not shape' { + $json = New-FeatureFlagJson -Condition @{ + Not = @{ Property = 'Tier'; Operator = 'Equals'; Value = 1 } + } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeFalse + } + + It 'Rejects an empty-array Not (minItems: 1)' { + $json = New-FeatureFlagJson -Condition @{ Not = @() } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeFalse + } + } + + Context 'Leaf Value' { + It 'Rejects an explicit JSON null Value' { + $json = New-FeatureFlagJson -Condition @{ Property = 'Tier'; Operator = 'Equals'; Value = $null } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeFalse + } + + It 'Validates a non-null Value' { + $json = New-FeatureFlagJson -Condition @{ Property = 'Tier'; Operator = 'Equals'; Value = 'Staging' } + Test-AgainstSchema -Json $json -SchemaFile $script:featureFlagSchema | Should -BeTrue + } + } +} + +Describe 'Properties.json' -Skip:($PSVersionTable.PSVersion.Major -lt 7) { + Context 'Validation keys applicable per Type' -ForEach @( + @{ Type = 'integer'; Validation = @{ Minimum = 0; Maximum = 10 }; Expected = $true } + @{ Type = 'integer'; Validation = @{ MinLength = 1 }; Expected = $false } + @{ Type = 'integer'; Validation = @{ MaxLength = 1 }; Expected = $false } + @{ Type = 'integer'; Validation = @{ Pattern = '^\d+$' }; Expected = $false } + @{ Type = 'string'; Validation = @{ MinLength = 1; MaxLength = 5; Pattern = '^\d+$' }; Expected = $true } + @{ Type = 'string'; Validation = @{ Minimum = 0 }; Expected = $false } + @{ Type = 'string'; Validation = @{ Maximum = 10 }; Expected = $false } + @{ Type = 'boolean'; Validation = @{ Minimum = 0 }; Expected = $false } + @{ Type = 'boolean'; Validation = @{ MinLength = 1 }; Expected = $false } + ) { + It "Type '' with Validation key(s) '' -> expected " { + $json = @{ + Prop = @{ Type = $Type; Validation = $Validation } + } | ConvertTo-Json -Depth 10 + Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -Be $Expected + } + } + + Context 'Minimum/Maximum bound type' { + It 'Validates integer-typed Minimum/Maximum' { + $json = @{ Prop = @{ Type = 'integer'; Validation = @{ Minimum = 0; Maximum = 10 } } } | ConvertTo-Json -Depth 10 + Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -BeTrue + } + + It 'Rejects a fractional Minimum' { + $json = @{ Prop = @{ Type = 'integer'; Validation = @{ Minimum = 0.5 } } } | ConvertTo-Json -Depth 10 + Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -BeFalse + } + + It 'Rejects a fractional Maximum' { + $json = @{ Prop = @{ Type = 'integer'; Validation = @{ Maximum = 10.5 } } } | ConvertTo-Json -Depth 10 + Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -BeFalse + } + } + + Context 'Enum applies to every Type (unchanged by this correction)' -ForEach @( + @{ Type = 'string' } + @{ Type = 'integer' } + @{ Type = 'boolean' } + ) { + It "Validates an Enum on a '' property" { + $json = @{ Prop = @{ Type = $Type; Enum = @() } } | ConvertTo-Json -Depth 10 + Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -BeTrue + } + } +}