From 0ec02a885f798dc5c713fa165553069dfb7809b5 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 7 Sep 2017 06:38:48 +0300 Subject: [PATCH 01/11] Add Test-Json --- ...crosoft.PowerShell.Commands.Utility.csproj | 1 + .../commands/utility/TestJsonCommand.cs | 100 ++++++++++++++ .../resources/TestJsonCmdletStrings.resx | 129 ++++++++++++++++++ .../Microsoft.PowerShell.Utility.psd1 | 3 +- .../Microsoft.PowerShell.Utility.psd1 | 3 +- .../Test-Json.Tests.ps1 | 102 ++++++++++++++ .../engine/Basic/DefaultCommands.Tests.ps1 | 1 + 7 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs create mode 100644 src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx create mode 100644 test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 diff --git a/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj b/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj index 526ab1f361d..1c5919dfa18 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj +++ b/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj @@ -78,6 +78,7 @@ + diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs new file mode 100644 index 00000000000..a67986dbc02 --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Management.Automation; +using System.Management.Automation.Internal; + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NJsonSchema; + +namespace Microsoft.PowerShell.Commands +{ + /// + /// This class implements Test-Json command. + /// + [Cmdlet(VerbsDiagnostic.Test, "Json", HelpUri = "")] + public class TestJsonCommand : PSCmdlet + { + /// + /// An Json to be validated. + /// + [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] + public String Json { get; set; } + + /// + /// A schema to validate the Json against. + /// It is optional parameter. + /// If the parameter is absent the cmdlet only try to parse the Json. + /// If the parameter present the cmdlet try to parse the Json and + /// then check the Json against the schema. Before the check + /// the cmdlet parse the schema doing implicitly check the schema too. + /// + [Parameter(Position = 1)] + [ValidateNotNullOrEmpty()] + public String Schema { get; set; } + + private JsonSchema4 _jschema; + + /// + /// Prepare an Json schema. + /// + protected override void BeginProcessing() + { + if (Schema != null) + { + try + { + _jschema = JsonSchema4.FromJsonAsync(Schema).Result; + } + catch (Exception exc) + { + Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonSchema, exc); + ThrowTerminatingError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, null)); + } + } + } + + /// + /// Validate an Json. + /// + protected override void ProcessRecord() + { + JObject parsedJson = null; + bool result = true; + + try + { + parsedJson = JObject.Parse(Json); + + if (_jschema != null) + { + var errorMessages = _jschema.Validate(parsedJson); + if (errorMessages.Count != 0) + { + result = false; + + Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonAgainistSchema); + if (errorMessages != null) + { + foreach (var message in errorMessages) + { + ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainistSchema", ErrorCategory.InvalidData, null); + errorRecord.ErrorDetails = new ErrorDetails(message.ToString()); + WriteError(errorRecord); + } + } + } + } + } + catch (Exception exc) + { + result = false; + + Exception exception = new Exception(TestJsonCmdletStrings.InvalidJson, exc); + WriteError(new ErrorRecord(exception, "InvalidJson", ErrorCategory.InvalidData, Json)); + } + + WriteObject(result); + } + } +} diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx new file mode 100644 index 00000000000..de2dff8565d --- /dev/null +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Cannot parse the Json schema. + + + Cannot parse the Json. + + + The Json is not valid with the schema. + + diff --git a/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 index b6ae4542427..0785132ffd2 100644 --- a/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 +++ b/src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 @@ -22,7 +22,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide", "Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", "Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", - "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex", "Remove-Alias" + "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex", + "Test-Json", "Remove-Alias" FunctionsToExport= "Import-PowerShellDataFile" AliasesToExport= "fhx" NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1" diff --git a/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 b/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 index 35a987c1c8b..50f785f87b1 100644 --- a/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 +++ b/src/Modules/Windows-Core/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1 @@ -22,7 +22,8 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide", "Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack", "Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash", "Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug", - "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex", "Remove-Alias" + "Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex", + "Test-Json", "Remove-Alias" FunctionsToExport= "ConvertFrom-SddlString" AliasesToExport= "fhx" NestedModules="Microsoft.PowerShell.Commands.Utility.dll","Microsoft.PowerShell.Utility.psm1" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 new file mode 100644 index 00000000000..f3c64dd1baa --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -0,0 +1,102 @@ +Describe "Test-Json" -Tags "CI" { + BeforeAll { + $validSchemaJson = @" + { + 'description': 'A person', + 'type': 'object', + 'properties': { + 'name': {'type': 'string'}, + 'hobbies': { + 'type': 'array', + 'items': {'type': 'string'} + } + } + } +"@ + + $invalidSchemaJson = @" + { + 'description', + 'type': 'object', + 'properties': { + 'name': {'type': 'string'}, + 'hobbies': { + 'type': 'array', + 'items': {'type': 'string'} + } + } + } +"@ + + $validJson = @" + { + 'name': 'James', + 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + } +"@ + + $invalidTypeInJson = @" + { + 'name': 123, + 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + } +"@ + + $invalidTypeInJson2 = @" + { + 'name': 123, + 'hobbies': [456, 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + } +"@ + + $invalidNodeInJson = @" + { + 'name': 'James', + 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + errorNode + } +"@ +} + + It "Json is valid" { + Test-Json -Json $validJson | Should Be $true + } + + It "Json is valid aganist a valid schema" { + Test-Json -Json $validJson -Schema $validSchemaJson | Should Be $true + } + + It "Json is invalid" { + Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should Be $false + } + + It "Json is invalid aganist a valid schema" { + Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should Be $false + Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should Be $false + } + + It "Test-Json throw if a schema is invalid" { + { Test-Json -Json $validJson -Schema $invalidSchemaJson -ErrorAction Stop } | ShouldBeErrorId "InvalidJsonSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + } + + It "Test-Json write an error on invalid () Json aganist a valid schema" -TestCases @( + @{ name = "type"; json = $invalidTypeInJson; error = "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } + @{ name = "node"; json = $invalidNodeInJson; error = "InvalidJson,Microsoft.PowerShell.Commands.TestJsonCommand" } + ) { + param($json, $error) + + $errorVar = $null + Test-Json -Json $json -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue + + $errorVar.FullyQualifiedErrorId | Should BeExactly $error + } + + It "Test-Json return all errors when check invalid Json aganist a valid schema" { + $errorVar = $null + Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue + + # '$invalidTypeInJson2' contains two errors in property types. + $errorVar.Count | Should Be 2 + $errorVar.FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + } +} diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index 3171e6f5870..1907240b6a0 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -440,6 +440,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Test-ComputerSecureChannel", , $($FullCLR ) "Cmdlet", "Test-FileCatalog", , $($FullCLR -or $CoreWindows ) "Cmdlet", "Test-ModuleManifest", , $($FullCLR -or $CoreWindows -or $CoreUnix) +"Cmdlet", "Test-Json", , $( $CoreWindows -or $CoreUnix) "Cmdlet", "Test-Path", , $($FullCLR -or $CoreWindows -or $CoreUnix) "Cmdlet", "Test-PSSessionConfigurationFile", , $($FullCLR -or $CoreWindows ) "Cmdlet", "Test-WSMan", , $($FullCLR -or $CoreWindows ) From 77d6bbc9ca21692dbc7d925c8754308882c21600 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 9 Nov 2017 06:09:18 +0300 Subject: [PATCH 02/11] Add NJsonSchema license in ThirdPartyNotices .txt --- ThirdPartyNotices.txt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ThirdPartyNotices.txt b/ThirdPartyNotices.txt index 971899c4e05..755c5912d40 100644 --- a/ThirdPartyNotices.txt +++ b/ThirdPartyNotices.txt @@ -76,3 +76,30 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + + +------------------------------------------------- +File: NJsonSchema +------------------------------------------------- + +The MIT License (MIT) + +Copyright (c) 2016 Rico Suter + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + From 1b42fb2dfccad34b98715751e23e116040e9b1e0 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 16 Mar 2018 16:25:33 +0300 Subject: [PATCH 03/11] Fix test --- .../Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index f3c64dd1baa..4cdbfa305bd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -97,6 +97,7 @@ Describe "Test-Json" -Tags "CI" { # '$invalidTypeInJson2' contains two errors in property types. $errorVar.Count | Should Be 2 - $errorVar.FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar[0].FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar[1].FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } } From b664a3d4c3aaf7aa8437ae1562645a283bca975e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 16 Mar 2018 16:30:23 +0300 Subject: [PATCH 04/11] Use Pester 4 syntax --- .../Test-Json.Tests.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index 4cdbfa305bd..4da94be5496 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -59,20 +59,20 @@ Describe "Test-Json" -Tags "CI" { } It "Json is valid" { - Test-Json -Json $validJson | Should Be $true + Test-Json -Json $validJson | Should -Be $true } It "Json is valid aganist a valid schema" { - Test-Json -Json $validJson -Schema $validSchemaJson | Should Be $true + Test-Json -Json $validJson -Schema $validSchemaJson | Should -Be $true } It "Json is invalid" { - Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should Be $false + Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should -Be $false } It "Json is invalid aganist a valid schema" { - Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should Be $false - Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should Be $false + Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -Be $false + Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -Be $false } It "Test-Json throw if a schema is invalid" { @@ -88,7 +88,7 @@ Describe "Test-Json" -Tags "CI" { $errorVar = $null Test-Json -Json $json -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue - $errorVar.FullyQualifiedErrorId | Should BeExactly $error + $errorVar.FullyQualifiedErrorId | Should -BeExactly $error } It "Test-Json return all errors when check invalid Json aganist a valid schema" { @@ -96,8 +96,8 @@ Describe "Test-Json" -Tags "CI" { Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorVariable errorVar -ErrorAction SilentlyContinue # '$invalidTypeInJson2' contains two errors in property types. - $errorVar.Count | Should Be 2 - $errorVar[0].FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" - $errorVar[1].FullyQualifiedErrorId | Should BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar.Count | Should -Be 2 + $errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } } From b72d638da874e059689563e84a7f80ec9e8fb6b1 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 20 Mar 2018 11:22:36 +0300 Subject: [PATCH 05/11] Fix Files.wxs --- assets/files.wxs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/assets/files.wxs b/assets/files.wxs index 1f3e7c00e90..4c03b334d20 100644 --- a/assets/files.wxs +++ b/assets/files.wxs @@ -4,6 +4,9 @@ + + + @@ -1818,6 +1821,7 @@ + From 4d3b51e9634b5008f6283027292bccf6b2a5497f Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 21 Mar 2018 15:33:31 +0300 Subject: [PATCH 06/11] Add Copyright --- .../commands/utility/TestJsonCommand.cs | 3 +++ .../Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index a67986dbc02..5fc2f8132bc 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + using System; using System.Collections.Generic; using System.Management.Automation; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index 4da94be5496..eb53c2520f2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -1,3 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + Describe "Test-Json" -Tags "CI" { BeforeAll { $validSchemaJson = @" From 9a024366fd6db45ff770b26a58a6508b1f7e417c Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 21 Mar 2018 15:36:41 +0300 Subject: [PATCH 07/11] Use Should -BeTrue and Should -BeFalse --- .../Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index eb53c2520f2..ba73b9cf1b2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -62,20 +62,20 @@ Describe "Test-Json" -Tags "CI" { } It "Json is valid" { - Test-Json -Json $validJson | Should -Be $true + Test-Json -Json $validJson | Should -BeTrue } It "Json is valid aganist a valid schema" { - Test-Json -Json $validJson -Schema $validSchemaJson | Should -Be $true + Test-Json -Json $validJson -Schema $validSchemaJson | Should -BeTrue } It "Json is invalid" { - Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should -Be $false + Test-Json -Json $invalidNodeInJson -ErrorAction SilentlyContinue | Should -BeFalse } It "Json is invalid aganist a valid schema" { - Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -Be $false - Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -Be $false + Test-Json -Json $invalidTypeInJson2 -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse + Test-Json -Json $invalidNodeInJson -Schema $validSchemaJson -ErrorAction SilentlyContinue | Should -BeFalse } It "Test-Json throw if a schema is invalid" { From 285dc4b28ecf926233f2a1ffd5a2ffbb6429b866 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 23 Mar 2018 13:54:05 +0300 Subject: [PATCH 08/11] Replace Json with JSON --- .../commands/utility/TestJsonCommand.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 5fc2f8132bc..4f8c0e0f1aa 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -19,17 +19,17 @@ namespace Microsoft.PowerShell.Commands public class TestJsonCommand : PSCmdlet { /// - /// An Json to be validated. + /// An JSON to be validated. /// [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] public String Json { get; set; } /// - /// A schema to validate the Json against. + /// A schema to validate the JSON against. /// It is optional parameter. - /// If the parameter is absent the cmdlet only try to parse the Json. - /// If the parameter present the cmdlet try to parse the Json and - /// then check the Json against the schema. Before the check + /// If the parameter is absent the cmdlet only try to parse the JSON. + /// If the parameter present the cmdlet try to parse the JSON and + /// then check the JSON against the schema. Before the check /// the cmdlet parse the schema doing implicitly check the schema too. /// [Parameter(Position = 1)] @@ -39,7 +39,7 @@ public class TestJsonCommand : PSCmdlet private JsonSchema4 _jschema; /// - /// Prepare an Json schema. + /// Prepare an JSON schema. /// protected override void BeginProcessing() { @@ -58,7 +58,7 @@ protected override void BeginProcessing() } /// - /// Validate an Json. + /// Validate an JSON. /// protected override void ProcessRecord() { From aa1e2b4bebc9e2ab956e16334257712c71ac07ff Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 23 Mar 2018 14:13:06 +0300 Subject: [PATCH 09/11] Fix comments and resources --- .../commands/utility/TestJsonCommand.cs | 14 +++++++------- .../resources/TestJsonCmdletStrings.resx | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 4f8c0e0f1aa..5bff976e888 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -26,11 +26,11 @@ public class TestJsonCommand : PSCmdlet /// /// A schema to validate the JSON against. - /// It is optional parameter. - /// If the parameter is absent the cmdlet only try to parse the JSON. - /// If the parameter present the cmdlet try to parse the JSON and - /// then check the JSON against the schema. Before the check - /// the cmdlet parse the schema doing implicitly check the schema too. + /// This is optional parameter. + /// If the parameter is absent the cmdlet only attempts to parse the JSON string. + /// If the parameter present the cmdlet attempts to parse the JSON string and + /// then validates the JSON against the schema. Before testing the JSON string, + /// the cmdlet parses the schema doing implicitly check the schema too. /// [Parameter(Position = 1)] [ValidateNotNullOrEmpty()] @@ -76,12 +76,12 @@ protected override void ProcessRecord() { result = false; - Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonAgainistSchema); + Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonAgainstSchema); if (errorMessages != null) { foreach (var message in errorMessages) { - ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainistSchema", ErrorCategory.InvalidData, null); + ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainstSchema", ErrorCategory.InvalidData, null); errorRecord.ErrorDetails = new ErrorDetails(message.ToString()); WriteError(errorRecord); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx index de2dff8565d..a5c8d5d24d9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/TestJsonCmdletStrings.resx @@ -118,12 +118,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Cannot parse the Json schema. + Cannot parse the JSON schema. - Cannot parse the Json. + Cannot parse the JSON. - - The Json is not valid with the schema. + + The JSON is not valid with the schema. From ebc1a53df3e47a9c83514c450419613f29ebd8a8 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 23 Mar 2018 14:16:54 +0300 Subject: [PATCH 10/11] Fix null check --- .../commands/utility/TestJsonCommand.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 5bff976e888..8b08da6427d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -72,19 +72,17 @@ protected override void ProcessRecord() if (_jschema != null) { var errorMessages = _jschema.Validate(parsedJson); - if (errorMessages.Count != 0) + if (errorMessages != null && errorMessages.Count != 0) { result = false; Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonAgainstSchema); - if (errorMessages != null) + + foreach (var message in errorMessages) { - foreach (var message in errorMessages) - { - ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainstSchema", ErrorCategory.InvalidData, null); - errorRecord.ErrorDetails = new ErrorDetails(message.ToString()); - WriteError(errorRecord); - } + ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainstSchema", ErrorCategory.InvalidData, null); + errorRecord.ErrorDetails = new ErrorDetails(message.ToString()); + WriteError(errorRecord); } } } From 17c2cebad2c859b5688478dea041e0190a6fb01e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 23 Mar 2018 16:31:52 +0300 Subject: [PATCH 11/11] Fix tests --- .../Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index ba73b9cf1b2..e7bc5c6106a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -83,7 +83,7 @@ Describe "Test-Json" -Tags "CI" { } It "Test-Json write an error on invalid () Json aganist a valid schema" -TestCases @( - @{ name = "type"; json = $invalidTypeInJson; error = "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } + @{ name = "type"; json = $invalidTypeInJson; error = "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } @{ name = "node"; json = $invalidNodeInJson; error = "InvalidJson,Microsoft.PowerShell.Commands.TestJsonCommand" } ) { param($json, $error) @@ -100,7 +100,7 @@ Describe "Test-Json" -Tags "CI" { # '$invalidTypeInJson2' contains two errors in property types. $errorVar.Count | Should -Be 2 - $errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" - $errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainistSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" + $errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } }