aboutsummaryrefslogtreecommitdiff
path: root/validators/powershell/Validate-GitInfo.ps1
blob: e84606c3081acff7bbebda4f0ac874d02e265702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<#
.SYNOPSIS
    Validates .gitinfo files against the gitinfo JSON Schema.

.DESCRIPTION
    A PowerShell validator for .gitinfo files. Parses JSONC (strips comments)
    and validates against the schema.

.PARAMETER Path
    Path to the .gitinfo file. Defaults to ".gitinfo" in the current directory.

.EXAMPLE
    .\Validate-GitInfo.ps1
    Validates .gitinfo in the current directory.

.EXAMPLE
    .\Validate-GitInfo.ps1 -Path "path/to/.gitinfo"
    Validates a specific .gitinfo file.
#>

param(
    [Parameter(Position = 0)]
    [string]$Path = ".gitinfo"
)

$ErrorActionPreference = "Stop"

# Schema path (two levels up from validators/powershell/)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$SchemaPath = Join-Path $ScriptDir "..\..\gitinfo.schema.json"

function Remove-JsonComments {
    param([string]$Jsonc)
    
    $result = New-Object System.Text.StringBuilder
    $i = 0
    $inString = $false
    $stringChar = $null
    
    while ($i -lt $Jsonc.Length) {
        $char = $Jsonc[$i]
        $next = if ($i + 1 -lt $Jsonc.Length) { $Jsonc[$i + 1] } else { $null }
        
        # Track string state
        if (-not $inString -and ($char -eq '"' -or $char -eq "'")) {
            $inString = $true
            $stringChar = $char
            [void]$result.Append($char)
            $i++
            continue
        }
        
        if ($inString) {
            if ($char -eq '\' -and $i + 1 -lt $Jsonc.Length) {
                [void]$result.Append($char)
                [void]$result.Append($Jsonc[$i + 1])
                $i += 2
                continue
            }
            if ($char -eq $stringChar) {
                $inString = $false
                $stringChar = $null
            }
            [void]$result.Append($char)
            $i++
            continue
        }
        
        # Single-line comment
        if ($char -eq '/' -and $next -eq '/') {
            while ($i -lt $Jsonc.Length -and $Jsonc[$i] -ne "`n") {
                $i++
            }
            continue
        }
        
        # Multi-line comment
        if ($char -eq '/' -and $next -eq '*') {
            $i += 2
            while ($i -lt $Jsonc.Length - 1 -and -not ($Jsonc[$i] -eq '*' -and $Jsonc[$i + 1] -eq '/')) {
                $i++
            }
            $i += 2
            continue
        }
        
        [void]$result.Append($char)
        $i++
    }
    
    return $result.ToString()
}

function Test-Uri {
    param([string]$Value)
    try {
        $uri = [System.Uri]::new($Value)
        return $uri.Scheme -eq "http" -or $uri.Scheme -eq "https"
    }
    catch {
        return $false
    }
}

function Test-Email {
    param([string]$Value)
    return $Value -match '^[^\s@]+@[^\s@]+\.[^\s@]+$'
}

function Test-Schema {
    param(
        $Data,
        $Schema,
        [string]$JsonPath = ""
    )
    
    $errors = @()
    
    if ($Schema.type -eq "object") {
        if ($Data -isnot [System.Collections.IDictionary] -and $Data.GetType().Name -ne "PSCustomObject") {
            $errors += "$(if ($JsonPath) { $JsonPath } else { 'root' }): expected object"
            return $errors
        }
        
        # Convert PSCustomObject to hashtable for easier handling
        $dataHash = @{}
        $Data.PSObject.Properties | ForEach-Object { $dataHash[$_.Name] = $_.Value }
        
        # Check additionalProperties
        if ($Schema.additionalProperties -eq $false -and $Schema.properties) {
            $allowed = $Schema.properties.PSObject.Properties.Name
            foreach ($key in $dataHash.Keys) {
                if ($key -notin $allowed) {
                    $errors += "$(if ($JsonPath) { $JsonPath } else { 'root' }): unknown property `"$key`""
                }
            }
        }
        
        # Validate each property
        if ($Schema.properties) {
            foreach ($prop in $Schema.properties.PSObject.Properties) {
                $key = $prop.Name
                $propSchema = $prop.Value
                if ($dataHash.ContainsKey($key)) {
                    $errors += Test-Schema -Data $dataHash[$key] -Schema $propSchema -JsonPath "$JsonPath.$key"
                }
            }
        }
    }
    elseif ($Schema.type -eq "array") {
        if ($Data -isnot [System.Array]) {
            $errors += "${JsonPath}: expected array"
            return $errors
        }
        
        if ($Schema.items) {
            for ($i = 0; $i -lt $Data.Count; $i++) {
                $itemSchema = if ($Schema.items -is [System.Array]) { $Schema.items[$i] } else { $Schema.items }
                if ($itemSchema) {
                    $errors += Test-Schema -Data $Data[$i] -Schema $itemSchema -JsonPath "$JsonPath[$i]"
                }
            }
            
            if ($Schema.minItems -and $Data.Count -lt $Schema.minItems) {
                $errors += "${JsonPath}: expected at least $($Schema.minItems) items"
            }
            if ($Schema.maxItems -and $Data.Count -gt $Schema.maxItems) {
                $errors += "${JsonPath}: expected at most $($Schema.maxItems) items"
            }
        }
    }
    elseif ($Schema.type -eq "string") {
        if ($Data -isnot [string]) {
            $errors += "${JsonPath}: expected string"
            return $errors
        }
        
        if ($Schema.minLength -and $Data.Length -lt $Schema.minLength) {
            $errors += "${JsonPath}: string too short (min $($Schema.minLength))"
        }
        
        if ($Schema.format -eq "uri") {
            if (-not (Test-Uri $Data)) {
                $errors += "${JsonPath}: invalid URI `"$Data`""
            }
        }
        
        if ($Schema.format -eq "email") {
            if (-not (Test-Email $Data)) {
                $errors += "${JsonPath}: invalid email `"$Data`""
            }
        }
        
        if ($Schema.pattern) {
            if ($Data -notmatch $Schema.pattern) {
                $errors += "${JsonPath}: does not match pattern $($Schema.pattern)"
            }
        }
    }
    
    return $errors
}

# Main
if (-not (Test-Path $Path)) {
    Write-Error "Error: File not found: $Path"
    exit 1
}

if (-not (Test-Path $SchemaPath)) {
    Write-Error "Error: Schema not found: $SchemaPath"
    exit 1
}

# Read and parse schema
try {
    $schemaContent = Get-Content -Path $SchemaPath -Raw
    $schema = $schemaContent | ConvertFrom-Json
}
catch {
    Write-Error "Error parsing schema: $_"
    exit 1
}

# Read and parse .gitinfo file
try {
    $content = Get-Content -Path $Path -Raw
    $stripped = Remove-JsonComments -Jsonc $content
    $data = $stripped | ConvertFrom-Json
}
catch {
    Write-Error "Error parsing JSONC: $_"
    exit 1
}

# Validate against schema
$errors = Test-Schema -Data $data -Schema $schema

if ($errors.Count -gt 0) {
    Write-Host "Validation failed for ${Path}:" -ForegroundColor Red
    foreach ($error in $errors) {
        Write-Host "  - $error" -ForegroundColor Red
    }
    exit 1
}

Write-Host "✓ $Path is valid" -ForegroundColor Green
exit 0