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
335 lines (294 loc) · 13.9 KB
/
Copy pathTestJsonCommand.cs
File metadata and controls
335 lines (294 loc) · 13.9 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Net.Http;
using System.Security;
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.Schema;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This class implements Test-Json command.
/// </summary>
[Cmdlet(VerbsDiagnostic.Test, "Json", DefaultParameterSetName = JsonStringParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096609")]
[OutputType(typeof(bool))]
public class TestJsonCommand : PSCmdlet
{
#region Parameter Set Names
private const string JsonStringParameterSet = "JsonString";
private const string JsonStringWithSchemaStringParameterSet = "JsonStringWithSchemaString";
private const string JsonStringWithSchemaFileParameterSet = "JsonStringWithSchemaFile";
private const string JsonPathParameterSet = "JsonPath";
private const string JsonPathWithSchemaStringParameterSet = "JsonPathWithSchemaString";
private const string JsonPathWithSchemaFileParameterSet = "JsonPathWithSchemaFile";
private const string JsonLiteralPathParameterSet = "JsonLiteralPath";
private const string JsonLiteralPathWithSchemaStringParameterSet = "JsonLiteralPathWithSchemaString";
private const string JsonLiteralPathWithSchemaFileParameterSet = "JsonLiteralPathWithSchemaFile";
#endregion
#region Json Document Option Constants
private const string IgnoreCommentsOption = "IgnoreComments";
private const string AllowTrailingCommasOption = "AllowTrailingCommas";
#endregion
#region Parameters
/// <summary>
/// Gets or sets JSON string to be validated.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = JsonStringParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = JsonStringWithSchemaStringParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ParameterSetName = JsonStringWithSchemaFileParameterSet)]
public string Json { get; set; }
/// <summary>
/// Gets or sets JSON file path to be validated.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonPathParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonPathWithSchemaStringParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonPathWithSchemaFileParameterSet)]
public string Path { get; set; }
/// <summary>
/// Gets or sets JSON literal file path to be validated.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonLiteralPathParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonLiteralPathWithSchemaStringParameterSet)]
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = JsonLiteralPathWithSchemaFileParameterSet)]
[Alias("PSPath", "LP")]
public string LiteralPath
{
get
{
return _isLiteralPath ? Path : null;
}
set
{
_isLiteralPath = true;
Path = value;
}
}
/// <summary>
/// Gets or sets 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, Mandatory = true, ParameterSetName = JsonStringWithSchemaStringParameterSet)]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = JsonPathWithSchemaStringParameterSet)]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = JsonLiteralPathWithSchemaStringParameterSet)]
[ValidateNotNullOrEmpty]
public string Schema { get; set; }
/// <summary>
/// Gets or sets path to the file containing schema to validate the JSON string against.
/// This is optional parameter.
/// </summary>
[Parameter(Position = 1, Mandatory = true, ParameterSetName = JsonStringWithSchemaFileParameterSet)]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = JsonPathWithSchemaFileParameterSet)]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = JsonLiteralPathWithSchemaFileParameterSet)]
[ValidateNotNullOrEmpty]
public string SchemaFile { get; set; }
/// <summary>
/// Gets or sets JSON document options.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ValidateSet(IgnoreCommentsOption, AllowTrailingCommasOption)]
public string[] Options { get; set; } = Array.Empty<string>();
#endregion
#region Private Members
private bool _isLiteralPath = false;
private JsonSchema _jschema;
private JsonDocumentOptions _documentOptions;
#endregion
/// <summary>
/// Prepare a JSON schema.
/// </summary>
protected override void BeginProcessing()
{
// By default, a JSON Schema implementation isn't supposed to automatically fetch content.
// Instead JsonSchema.Net has been set up with a registry so that users can pre-register
// any schemas they may need to resolve.
// However, pre-registering schemas doesn't make sense in the context of a Powershell command,
// and automatically fetching referenced URIs is likely the preferred behavior. To do that,
// this property must be set with a method to retrieve and deserialize the content.
// For more information, see https://json-everything.net/json-schema#automatic-resolution
SchemaRegistry.Global.Fetch = static uri =>
{
try
{
string text;
switch (uri.Scheme)
{
case "http":
case "https":
{
using var client = new HttpClient();
text = client.GetStringAsync(uri).Result;
break;
}
case "file":
var filename = Uri.UnescapeDataString(uri.AbsolutePath);
text = File.ReadAllText(filename);
break;
default:
throw new FormatException(string.Format(TestJsonCmdletStrings.InvalidUriScheme, uri.Scheme));
}
return JsonSerializer.Deserialize<JsonSchema>(text);
}
catch (Exception e)
{
throw new JsonSchemaReferenceResolutionException(e);
}
};
string resolvedpath = string.Empty;
try
{
if (Schema != null)
{
try
{
_jschema = JsonSchema.FromText(Schema);
}
catch (JsonException e)
{
Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, e);
WriteError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, Schema));
}
}
else if (SchemaFile != null)
{
try
{
resolvedpath = Context.SessionState.Path.GetUnresolvedProviderPathFromPSPath(SchemaFile);
_jschema = JsonSchema.FromFile(resolvedpath);
}
catch (JsonException e)
{
Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, e);
WriteError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, SchemaFile));
}
}
}
catch (Exception e) when (
// Handle exceptions related to file access to provide more specific error message
// https://learn.microsoft.com/dotnet/standard/io/handling-io-errors
e is IOException ||
e is UnauthorizedAccessException ||
e is NotSupportedException ||
e is SecurityException
)
{
Exception exception = new(
string.Format(
CultureInfo.CurrentUICulture,
TestJsonCmdletStrings.JsonSchemaFileOpenFailure,
resolvedpath),
e);
ThrowTerminatingError(new ErrorRecord(exception, "JsonSchemaFileOpenFailure", ErrorCategory.OpenError, resolvedpath));
}
catch (Exception e)
{
Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, e);
ThrowTerminatingError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, resolvedpath));
}
_documentOptions = new JsonDocumentOptions
{
CommentHandling = Options.Contains(IgnoreCommentsOption, StringComparer.OrdinalIgnoreCase)
? JsonCommentHandling.Skip
: JsonCommentHandling.Disallow,
AllowTrailingCommas = Options.Contains(AllowTrailingCommasOption, StringComparer.OrdinalIgnoreCase)
};
}
/// <summary>
/// Validate a JSON.
/// </summary>
protected override void ProcessRecord()
{
bool result = true;
string jsonToParse = string.Empty;
if (Json != null)
{
jsonToParse = Json;
}
else if (Path != null)
{
string resolvedPath = PathUtils.ResolveFilePath(Path, this, _isLiteralPath);
if (!File.Exists(resolvedPath))
{
ItemNotFoundException exception = new(
Path,
"PathNotFound",
SessionStateStrings.PathNotFound);
ThrowTerminatingError(exception.ErrorRecord);
}
jsonToParse = File.ReadAllText(resolvedPath);
}
try
{
var parsedJson = JsonNode.Parse(jsonToParse, nodeOptions: null, _documentOptions);
if (_jschema != null)
{
EvaluationResults evaluationResults = _jschema.Evaluate(parsedJson, new EvaluationOptions { OutputFormat = OutputFormat.Hierarchical });
result = evaluationResults.IsValid;
if (!result)
{
ReportValidationErrors(evaluationResults);
}
}
}
catch (JsonSchemaReferenceResolutionException jsonExc)
{
result = false;
Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, jsonExc);
WriteError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, _jschema));
}
catch (Exception exc)
{
result = false;
Exception exception = new(TestJsonCmdletStrings.InvalidJson, exc);
WriteError(new ErrorRecord(exception, "InvalidJson", ErrorCategory.InvalidData, Json));
}
WriteObject(result);
}
/// <summary>
/// Recursively reports validation errors from hierarchical evaluation results.
/// Skips nodes (and their children) where IsValid is true to avoid false positives
/// from constructs like OneOf or AnyOf.
/// </summary>
/// <param name="evaluationResult">The evaluation result to process.</param>
private void ReportValidationErrors(EvaluationResults evaluationResult)
{
// Skip this node and all children if validation passed
if (evaluationResult.IsValid)
{
return;
}
// Report errors at this level
HandleValidationErrors(evaluationResult);
// Recursively process child results
if (evaluationResult.HasDetails)
{
foreach (var nestedResult in evaluationResult.Details)
{
ReportValidationErrors(nestedResult);
}
}
}
private void HandleValidationErrors(EvaluationResults evaluationResult)
{
if (!evaluationResult.HasErrors)
{
return;
}
foreach (var error in evaluationResult.Errors!)
{
Exception exception = new(string.Format(TestJsonCmdletStrings.InvalidJsonAgainstSchemaDetailed, error.Value, evaluationResult.InstanceLocation));
ErrorRecord errorRecord = new(exception, "InvalidJsonAgainstSchemaDetailed", ErrorCategory.InvalidData, null);
WriteError(errorRecord);
}
}
}
}