GripDevJsonSchemaValidator 1.0.0
dotnet tool install --global GripDevJsonSchemaValidator --version 1.0.0
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local GripDevJsonSchemaValidator --version 1.0.0
#tool dotnet:?package=GripDevJsonSchemaValidator&version=1.0.0
nuke :add-package GripDevJsonSchemaValidator --version 1.0.0
NewtonsoftJsonSchemaTool
Overview
It's a simple wrapper around Newtonsoft.Json.Schema as CLI and PowerShell Module.
Installation
To install the NewtonsoftJsonSchemaTool, use the following command:
dotnet tool install --global GripDevJsonSchemaValidator
Usage
To use the NewtonsoftJsonSchemaTool, run the following command:
GripDevJsonSchemaValidator <schema-file> <json-file>
Replace <schema-file>
with the path to your JSON schema file and <json-file>
with the path to your JSON file to validate.
Example
Here is an example of how to use the NewtonsoftJsonSchemaTool:
- Create a JSON schema file (
schema.json
):
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": ["name", "age"]
}
- Create a JSON file to validate (
data.json
):
{
"name": "John Doe",
"age": "twenty-five"
}
- Run the GripDevJsonSchemaValidator:
GripDevJsonSchemaValidator schema.json data.json
- The output will be a JSON array of errors with full details, including line numbers for each error:
{
"Valid": false,
"Errors": [
{
"Message": "Invalid type. Expected Integer but got String.",
"LineNumber": 3,
"LinePosition": 12,
"Path": "age"
}
]
}
Powershell Module
In addition to the dotnet CLI tool, a Powershell module is also available for running Newtonsoft schema validation on input JSON schema and files to validate. The Powershell module returns a well-typed array of error objects exposing the JSON schema validation error type.
Installation
To install the Powershell module, use the following command:
Install-Module -Name GripDevJsonSchemaValidator
Usage
To use the Powershell module, import the module and run the Test-JsonSchema
cmdlet:
Import-Module GripDevJsonSchemaValidator
$schema = Get-Content -Path "schema.json" -Raw
$json = Get-Content -Path "data.json" -Raw
$errors = Test-JsonSchema -Schema $schema -Json $json
if ($errors.Count -eq 0) {
Write-Output "JSON is valid."
} else {
Write-Output "JSON is invalid. Errors:"
$errors | ForEach-Object {
Write-Output "Message: $_.Message"
Write-Output "LineNumber: $_.LineNumber"
Write-Output "LinePosition: $_.LinePosition"
Write-Output "Path: $_.Path"
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
This package has no dependencies.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 47 | 1/30/2025 |