Corvus.JsonSchema
Support for Json Schema validation and entity generation
Install / Use
/learn @corvus-dotnet/Corvus.JsonSchemaREADME
Corvus.JsonSchema
Build-time code generation for Json Schema validation, and serialization.
It supports serialization of every feature of JSON schema from draft4 to draft2020-12, including the OpenApi3.0 variant of draft4. (i.e. it doesn't give up on complex structure and lapse back to 'anonymous JSON objects' like most .NET tooling.)
Supported platforms
.NET 4.8.1 (Windows)
It now works with .NET 4.8.1 and later by providing netstandard2.0 packages.
.NET 8.0, 9.0 (Windows, Linux, MacOs)
We take advantage of features in .NET 8.0 and later, by providing net80 packages. These are supported on Windows, Linux, and MacOS.
Note that if you are building libraries using Corvus.Json.ExtendedTypes, and generated schema types, you should ensure
that you target both netstandard2.0 and net80 (or later) to ensure that your library can be consumed
by the widest possible range of projects
If you build your library against netstandard2.0 only, and are consumed by a net80 or later project, you will see type load errors.
Support schema dialects
In V4 we have full support for the following schema dialects:
- Draft 4
- OpenAPI 3.0
- Draft 6
- Draft 7
- 2019-09
- 2020-12 (Including OpenAPI 3.1)
You can see full details of the supported schema dialects on the bowtie website.
Bowtie is tool for understanding and comparing implementations of the JSON Schema specification across all programming languages.
It uses the official JSON Schema Test Suite to display bugs or functionality gaps in implementations.
Project Sponsor
This project is sponsored by endjin, a UK based Microsoft Gold Partner for Cloud Platform, Data Platform, Data Analytics, DevOps, and a Power BI Partner.
For more information about our products and services, or for commercial support of this project, please contact us.
We produce two free weekly newsletters; Azure Weekly for all things about the Microsoft Azure Platform, and Power BI Weekly.
Keep up with everything that's going on at endjin via our blog, follow us on Twitter, or LinkedIn.
Our other Open Source projects can be found at https://endjin.com/open-source
Code of conduct
This project has adopted a code of conduct adapted from the Contributor Covenant to clarify expected behavior in our community. This code of conduct has been adopted by many other projects. For more information see the Code of Conduct FAQ or contact hello@endjin.com with any additional questions or comments.
Concepts
Introduction
For a quick introduction, you could read this blog post by Ian Griffiths (@idg10), a C# MVP and Technical Fellow at endjin.
There's also a talk by Ian on the techniques used in this library.
If you want to see some well-worn patterns with JSON Schema, and how they translate into common .NET idioms, then this series is very useful. The corresponding sample code is found here.
History
For a more detailed introduction to the concepts, take a look at this blog post.
What kind of things is Corvus.JsonSchema good for?
There are 2 key features:
Serialization
You use our generatejsonschematypes tool to generate code (on Windows, Linux or MacOS) from an existing JSON Schema document, and compile it in a standard .NET assembly.
The generated code provides object models for JSON Schema documents that give you rich, idiomatic C# types with strongly typed properties, pattern matching and efficient cast operations.
You can operate directly over the JSON data, or mix-and-match building new JSON models from .NET primitive types.
string jsonText =
"""
{
"name": {
"familyName": "Oldroyd",
"givenName": "Michael",
"otherNames": ["Francis", "James"]
},
"dateOfBirth": "1944-07-14"
}
""";
var person = Person.Parse(jsonText);
Console.WriteLine($"{person.Name.FamilyName}"});
Validation
The same object-model provides ultra-fast, zero/low allocation validation of JSON data against a JSON Schema.
Having "deserialized" (really 'mapped') the JSON into the object model you can make use of the validation:
string jsonText =
"""
{
"name": {
"familyName": "Oldroyd",
"givenName": "Michael",
"otherNames": ["Francis", "James"]
},
"dateOfBirth": "1944-07-14"
}
""";
var person = Person.Parse(jsonText);
Console.WriteLine($"The person {person.IsValid() ? "is" : "is not"} valid JSON");
Or you can retrieve detailed validation results including JSON Schema output format location information:
var result = person.Validate(ValidationContext.ValidContext, ValidationLevel.Detailed);
if (!result.IsValid)
{
foreach (ValidationResult error in result.Results)
{
Console.WriteLine(error);
}
}
Getting started
To get started, install the dotnet global tool.
dotnet tool install --global Corvus.Json.JsonSchema.TypeGeneratorTool
[On Linux/MacOS you may need to ensure that the .dotnet/tools folder is in your path.]
Validate it is installed correctly
generatejsonschematypes -h
This should produce output similar to the following:
USAGE:
generatejsonschematypes <schemaFile> [OPTIONS]
ARGUMENTS:
<schemaFile> The path to the schema file to process
OPTIONS:
DEFAULT
-h, --help Prints help information
--rootNamespace The default root namespace for generated types
--rootPath The path in the document for the root type
--useSchema NotSpecified Override the fallback schema variant to use. If
NotSpecified, and it cannot be inferred from the schema
itself, it will use Draft2020-12
--outputMapFile The name to use for a map file which includes details of
the files that were written
--outputPath The path to which to write the generated code
--outputRootTypeName The .NET type name for the root type
--rebaseToRootPath If a --rootPath is specified, rebase the document as if it
was rooted on the specified element
--assertFormat True If --assertFormat is specified, assert format
specifications
--disableOptionalNamingHeuristics Disables optional naming heuristics
--optionalAsNullable None If NullOrUndefined, optional properties are emitted as .NET
nullable values
To run it against a JSON Schema file in the local file system:
e.g. Create a JSON schema file called person-from-api.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "JSON Schema for a Person entity coming back from a 3rd party API (e.g. a storage format in a database)",
"$defs": {
"Person": {
"type": "object",
"required": ["name"],
"properties": {
"name": { "$ref": "#/$defs/PersonName" },
"dateOfBirth": {
"type": "string",
"format": "date"
}
}
},
"PersonName": {
"type": "object",
"description": "A name of a person.",
"required": [ "familyName" ],
"properties": {
"givenName": {
"$ref": "#/$defs/PersonNameElement",
"description": "The person's given name."
},
"familyName": {
"$ref": "#/$defs/PersonNameElement",
"description": "The person's family name."
},
"otherNames": {
"$ref": "#/$defs/OtherNames",
"description": "Other (middle) names for the person"
}
}
},
"OtherNames": {
"oneOf": [
{ "$ref": "#/$defs/PersonNameElement" },
{ "$ref": "#/$defs/PersonNameElementArray" }
]
},
"PersonNameElementArray": {
"type": "array",
"items": {
"$ref": "#/$defs/PersonNameElement"
}
},
"PersonNameElement": {
"type": "string",
"minLength": 1,
"maxLength": 256
},
"Link":
{
"required": [
"href"
],
"type": "object",
"properties": {
"href": {
"title": "URI of the target resource",
"type": "string",
"description": "Either
Related Skills
openhue
349.2kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
349.2kElevenLabs text-to-speech with mac-style say UX.
weather
349.2kGet current weather and forecasts via wttr.in or Open-Meteo
tradingview-mcp
682Advanced TradingView MCP Server for AI-powered market analysis. Real-time crypto & stock screening, technical indicators, Bollinger Band intelligence, and candlestick patterns. Works with Claude Desktop & AI assistants. Multi-exchange support (Binance, KuCoin, Bybit+). Open source trading toolkit.
