|
| 1 | +########################################################################### |
| 2 | +# # |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. # |
| 4 | +# # |
| 5 | +# This code is licensed under the MIT License (MIT). # |
| 6 | +# # |
| 7 | +########################################################################### |
| 8 | + |
| 9 | +<# |
| 10 | +.Synopsis |
| 11 | + Validates JSON content against a specified schema. |
| 12 | +
|
| 13 | +.Description |
| 14 | + This module provides a function to validate JSON strings against a JSON schema. |
| 15 | + It uses Newtonsoft.Json and Newtonsoft.Json.Schema libraries for parsing and validation. |
| 16 | + It is used only when running in a PowerShell v5.1 or earlier environments that |
| 17 | + do not have the built-in `Test-Json` cmdlet available. |
| 18 | + `Test-Json` cmdlet is available in PowerShell 7+. |
| 19 | +#> |
| 20 | + |
| 21 | + |
| 22 | +class JsonValidator { |
| 23 | + # Static method for validating JSON against a schema |
| 24 | + static [bool] Validate([String]$jsonContent, [String]$schemaContent) { |
| 25 | + try { |
| 26 | + # Parse the JSON string into a JObject. |
| 27 | + # $jToken = [Newtonsoft.Json.Linq.JToken]::Parse($jsonContent) |
| 28 | + $jToken = [Newtonsoft.Json.Linq.JToken]::Parse($jsonContent) |
| 29 | + |
| 30 | + # Parses the JSON schema. |
| 31 | + # $jSchema = [Newtonsoft.Json.Schema.JSchema]::Parse($schemaContent) |
| 32 | + $jSchema = [Newtonsoft.Json.Schema.JsonSchema]::Parse($schemaContent) |
| 33 | + |
| 34 | + # Validate the JSON against the schema. |
| 35 | + $errors = New-Object System.Collections.Generic.List[string] |
| 36 | + # $isValid = [Newtonsoft.Json.Schema.SchemaExtensions]::IsValid($jToken, $jSchema, [ref]$errors) |
| 37 | + $isValid = [Newtonsoft.Json.Schema.Extensions]::IsValid($jToken, $jSchema, [ref]$errors) |
| 38 | + |
| 39 | + if ($isValid) { |
| 40 | + return $true |
| 41 | + } |
| 42 | + else { |
| 43 | + # Write-Host "JSON is invalid according to the schema." |
| 44 | + # $errors | ForEach-Object { Write-Host $_ } |
| 45 | + Write-Error "The JSON is not valid with the schema: $($errors -join ', ')" |
| 46 | + return $false |
| 47 | + } |
| 48 | + } |
| 49 | + catch { |
| 50 | + Write-Error "An error occurred during validation: $_" |
| 51 | + return $false |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +function Test-Json { |
| 58 | + [CmdletBinding( |
| 59 | + SupportsShouldProcess = $true, |
| 60 | + ConfirmImpact = 'Low' |
| 61 | + )] |
| 62 | + param( |
| 63 | + [Parameter(Mandatory = $true, HelpMessage = "JSON string to test for validity.")] |
| 64 | + [String]$Json, |
| 65 | + [Parameter(Mandatory = $true, HelpMessage = "A schema to validate the JSON input against.")] |
| 66 | + [String]$Schema |
| 67 | + ) |
| 68 | + |
| 69 | + begin { |
| 70 | + $WhatIfMessage = "Validate JSON against schema." |
| 71 | + } |
| 72 | + |
| 73 | + process { |
| 74 | + if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) { |
| 75 | + return [JsonValidator]::Validate($Json, $Schema) |
| 76 | + } |
| 77 | + else { |
| 78 | + # Code that should be processed if doing a WhatIf operation |
| 79 | + # Must NOT change anything outside of the function / script |
| 80 | + return |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +Export-ModuleMember -Function Test-Json |
0 commit comments