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
| function Test-JsonDeep { param( [Parameter(Mandatory)] [object]$Data,
[Parameter(Mandatory)] [hashtable]$Schema,
[string]$Path = "" )
$errors = @()
if ($Schema.required) { foreach ($field in $Schema.required) { if (-not ($Data.PSObject.Properties.Name -contains $field)) { $errors += "${Path}.$field : 缺少必填字段" } } }
if ($Schema.properties) { foreach ($prop in $Schema.properties.GetEnumerator()) { $name = $prop.Key $rules = $prop.Value $fullPath = if ($Path) { "$Path.$name" } else { $name }
if (-not ($Data.PSObject.Properties.Name -contains $name)) { continue } $value = $Data.$name
if ($rules.type -eq "object" -and $rules.properties -and $value -is [PSCustomObject]) { $nestedErrors = Test-JsonDeep -Data $value -Schema $rules -Path $fullPath $errors += $nestedErrors continue }
if ($rules.type -eq "array" -and $value -is [array]) { if ($rules.minItems -and $value.Count -lt $rules.minItems) { $errors += "$fullPath : 数组元素不足 $($rules.minItems) 个" } if ($rules.items -and $rules.items.type -eq "object") { foreach ($item in $value) { if ($item -is [PSCustomObject]) { $nestedErrors = Test-JsonDeep -Data $item -Schema $rules.items -Path "$fullPath[]" $errors += $nestedErrors } } } }
if ($rules.type -eq "string" -and $rules.pattern -and $value -is [string]) { if ($value -notmatch $rules.pattern) { $errors += "$fullPath : 值 '$value' 不匹配模式 $($rules.pattern)" } }
if ($rules.type -eq "integer" -and $rules.minimum) { if ([int]$value -lt $rules.minimum) { $errors += "$fullPath : 值 $value 小于最小值 $($rules.minimum)" } } } }
return $errors }
$configJson = @" { "app": { "name": "MyApp", "version": "2.5.0" }, "database": { "host": "prod-db01", "port": 5432, "name": "myapp" }, "servers": [ { "name": "SRV01", "role": "web" }, { "name": "SRV02", "role": "db" } ] } "@
$config = $configJson | ConvertFrom-Json
$configSchema = @{ required = @("app", "database", "servers") properties = @{ app = @{ type = "object" required = @("name", "version") properties = @{ name = @{ type = "string" } version = @{ type = "string"; pattern = "^\d+\.\d+\.\d+$" } } } database = @{ type = "object" required = @("host", "port") properties = @{ host = @{ type = "string" } port = @{ type = "integer"; minimum = 1; maximum = 65535 } } } servers = @{ type = "array" minItems = 1 items = @{ type = "object" required = @("name", "role") properties = @{ name = @{ type = "string" } role = @{ type = "string" } } } } } }
$errors = Test-JsonDeep -Data $config -Schema $configSchema if ($errors) { Write-Host "验证错误:" -ForegroundColor Red $errors | ForEach-Object { Write-Host " $_" -ForegroundColor Red } } else { Write-Host "配置验证通过" -ForegroundColor Green }
|