Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Security;
using Newtonsoft.Json.Linq;
using System.Text.Json;

using NJsonSchema;

namespace Microsoft.PowerShell.Commands
Expand All @@ -26,7 +29,7 @@ public class TestJsonCommand : PSCmdlet
/// Gets or sets JSON string to be validated.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
public string Json { get; set; }
public string Json { get; set; } = default!;

/// <summary>
/// Gets or sets schema to validate the JSON against.
Expand All @@ -38,17 +41,17 @@ public class TestJsonCommand : PSCmdlet
/// </summary>
[Parameter(Position = 1, ParameterSetName = SchemaStringParameterSet)]
[ValidateNotNullOrEmpty]
public string Schema { get; set; }
public string? Schema { get; set; }

/// <summary>
/// Gets or sets path to the file containg schema to validate the JSON string against.
/// This is optional parameter.
/// </summary>
[Parameter(Position = 1, ParameterSetName = SchemaFileParameterSet)]
[ValidateNotNullOrEmpty]
public string SchemaFile { get; set; }
public string? SchemaFile { get; set; }

private JsonSchema _jschema;
private JsonSchema? _jschema;

/// <summary>
/// Process all exceptions in the AggregateException.
Expand All @@ -59,9 +62,9 @@ public class TestJsonCommand : PSCmdlet
/// <returns>Return value is unreachable since we always rethrow.</returns>
private static bool UnwrapException(Exception e)
{
if (e is TargetInvocationException)
if (e is TargetInvocationException && e.InnerException != null)
{
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
ExceptionDispatchInfo.Throw(e.InnerException);
}
else
{
Expand Down Expand Up @@ -135,16 +138,15 @@ e is SecurityException
/// </summary>
protected override void ProcessRecord()
{
JObject parsedJson = null;
bool result = true;

try
{
parsedJson = JObject.Parse(Json);
JsonDocument.Parse(Json);

if (_jschema != null)
{
var errorMessages = _jschema.Validate(parsedJson);
var errorMessages = _jschema.Validate(Json);
if (errorMessages != null && errorMessages.Count != 0)
{
result = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,57 @@ Describe "Test-Json" -Tags "CI" {

$validSchemaJson = @"
{
'description': 'A person',
'type': 'object',
'properties': {
'name': {'type': 'string'},
'hobbies': {
'type': 'array',
'items': {'type': 'string'}
"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'}
"description",
"type": "object",
"properties": {
"name": {"type": "string"},
"hobbies": {
"type": "array",
"items": {"type": "string"}
}
}
}
"@

$validJson = @"
{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
"name": "James",
"hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"]
}
"@

$invalidTypeInJson = @"
{
'name': 123,
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
"name": 123,
"hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"]
}
"@

$invalidTypeInJson2 = @"
{
'name': 123,
'hobbies': [456, 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
"name": 123,
"hobbies": [456, "Blogging", "Reading", "Xbox", "LOLCATS"]
}
"@

$invalidNodeInJson = @"
{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
"name": "James",
"hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"]
errorNode
}
"@
Expand Down Expand Up @@ -152,4 +152,20 @@ Describe "Test-Json" -Tags "CI" {
$errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
$errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand"
}

It "Test-Json recognizes primitives: <name>" -TestCases @(
@{ name = 'number'; value = 1 }
@{ name = '"true"'; value = '"true"' }
@{ name = 'true'; value = 'true' }
@{ name = '"false"'; value = '"false"' }
@{ name = 'false'; value = 'false' }
@{ name = '"null"'; value = '"null"' }
@{ name = 'null'; value = 'null' }
@{ name = 'string'; value = '"abc"' }
@{ name = 'array'; value = '[ 1, 2 ]' }
) {
param ($name, $value)

Test-Json -Json $value | Should -BeTrue
}
}