Philiprehberger.JsonSchema
0.2.1
dotnet add package Philiprehberger.JsonSchema --version 0.2.1
NuGet\Install-Package Philiprehberger.JsonSchema -Version 0.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.JsonSchema" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.JsonSchema" Version="0.2.1" />
<PackageReference Include="Philiprehberger.JsonSchema" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.JsonSchema --version 0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Philiprehberger.JsonSchema, 0.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.JsonSchema@0.2.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.JsonSchema&version=0.2.1
#tool nuget:?package=Philiprehberger.JsonSchema&version=0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.JsonSchema
Validate JSON documents against JSON Schema with structured error paths using System.Text.Json.
Installation
dotnet add package Philiprehberger.JsonSchema
Usage
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
}
""");
var result = schema.Validate("""{ "name": "Alice", "age": 30 }""");
Console.WriteLine(result.IsValid); // True
Handling Validation Errors
var result = schema.Validate("""{ "age": -5 }""");
foreach (var error in result.Errors)
{
Console.WriteLine($"{error.Path}: {error.Message} ({error.Keyword})");
// $.name: Required property is missing (required)
// $.age: Value is less than minimum of 0 (minimum)
}
String Validation
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"type": "string",
"minLength": 1,
"maxLength": 100,
"pattern": "^[A-Za-z]+$",
"format": "email"
}
""");
Supported formats: email, date-time, uri, ipv4, ipv6.
Array Validation
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
""");
Enum and Const
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"type": "object",
"properties": {
"status": { "enum": ["active", "inactive", "pending"] },
"version": { "const": 2 }
}
}
""");
Schema Composition
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"oneOf": [
{ "type": "string", "maxLength": 5 },
{ "type": "integer", "minimum": 0 }
]
}
""");
Supports allOf (all must match), anyOf (at least one), oneOf (exactly one), and not (must not match).
$ref with $defs
using Philiprehberger.JsonSchema;
var schema = JsonSchema.Parse("""
{
"type": "object",
"properties": {
"home": { "$ref": "#/$defs/Address" },
"work": { "$ref": "#/$defs/Address" }
},
"$defs": {
"Address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
},
"required": ["street", "city"]
}
}
}
""");
Static Validation
using Philiprehberger.JsonSchema;
var result = JsonSchema.Validate(schemaJson, documentJson);
API
| Method | Description |
|---|---|
JsonSchema.Parse(string) |
Parse a JSON Schema string into a compiled schema |
JsonSchema.Validate(string, string) |
Validate a JSON document against a schema string |
Schema.Validate(string) |
Validate a JSON document string against a compiled schema |
Schema.Validate(JsonNode?) |
Validate a JsonNode against a compiled schema |
ValidationResult.IsValid |
Whether validation passed |
ValidationResult.Errors |
List of ValidationError with path, message, and keyword |
Supported Keywords
| Category | Keywords |
|---|---|
| Type | type |
| String | minLength, maxLength, pattern, format |
| Numeric | minimum, maximum |
| Array | items, minItems, maxItems, uniqueItems |
| Object | properties, required |
| Enum/Const | enum, const |
| Composition | allOf, anyOf, oneOf, not |
| References | $ref, $defs, definitions |
Development
dotnet build src/Philiprehberger.JsonSchema.csproj --configuration Release
Support
If you find this project useful:
License
| 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.