forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestJsonCommand.cs
More file actions
101 lines (88 loc) · 3.37 KB
/
Copy pathTestJsonCommand.cs
File metadata and controls
101 lines (88 loc) · 3.37 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
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
{
/// <summary>
/// This class implements Test-Json command.
/// </summary>
[Cmdlet(VerbsDiagnostic.Test, "Json", HelpUri = "")]
public class TestJsonCommand : PSCmdlet
{
/// <summary>
/// An JSON to be validated.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)]
public string Json { get; set; }
/// <summary>
/// A schema to validate the JSON against.
/// 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.
/// </summary>
[Parameter(Position = 1)]
[ValidateNotNullOrEmpty()]
public string Schema { get; set; }
private JsonSchema _jschema;
/// <summary>
/// Prepare an JSON schema.
/// </summary>
protected override void BeginProcessing()
{
if (Schema != null)
{
try
{
_jschema = JsonSchema.FromJsonAsync(Schema).Result;
}
catch (Exception exc)
{
Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonSchema, exc);
ThrowTerminatingError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, null));
}
}
}
/// <summary>
/// Validate an JSON.
/// </summary>
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 != null && errorMessages.Count != 0)
{
result = false;
Exception exception = new Exception(TestJsonCmdletStrings.InvalidJsonAgainstSchema);
foreach (var message in errorMessages)
{
ErrorRecord errorRecord = new ErrorRecord(exception, "InvalidJsonAgainstSchema", 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);
}
}
}