Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Gatekeeper/Schemas/FeatureFlag.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -38,8 +38,7 @@
"type": "array",
"items": {
"$ref": "#/$defs/rule"
},
"minItems": 1
}
}
},
"$defs": {
Expand Down Expand Up @@ -115,7 +114,11 @@
],
"properties": {
"Not": {
"$ref": "#/$defs/condition"
"type": "array",
Comment thread
HeyItsGilbert marked this conversation as resolved.
Resolved
"items": {
"$ref": "#/$defs/condition"
},
"minItems": 1
}
},
"additionalProperties": false
Expand All @@ -136,12 +139,18 @@
"Equals",
"NotEquals",
"GreaterThan",
"GreaterThanOrEqual",
"LessThan",
"LessThanOrEqual",
"In",
"NotIn"
]
},
"Value": {}
"Value": {
"not": {
"type": "null"
}
}
},
"additionalProperties": false
}
Expand Down
62 changes: 57 additions & 5 deletions Gatekeeper/Schemas/Properties.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -29,10 +30,10 @@
"type": "object",
"properties": {
"Minimum": {
"type": "number"
"type": "integer"
},
"Maximum": {
"type": "number"
"type": "integer"
},
"MinLength": {
"type": "integer"
Expand All @@ -48,6 +49,57 @@
"additionalProperties": false
}
},
"additionalProperties": false
}
"additionalProperties": false,
"if": {
Comment thread
HeyItsGilbert marked this conversation as resolved.
Resolved
"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."
}
167 changes: 167 additions & 0 deletions tests/Schemas.tests.ps1
Original file line number Diff line number Diff line change
@@ -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 '<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 '<Type>' with Validation key(s) '<Validation.Keys>' -> expected <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 '<Type>' property" {
$json = @{ Prop = @{ Type = $Type; Enum = @() } } | ConvertTo-Json -Depth 10
Test-AgainstSchema -Json $json -SchemaFile $script:propertiesSchema | Should -BeTrue
}
}
}
Loading