GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - trippwill/sharp-schema: An opiniated library for generating JSON Schema from a C# type hierarchy

BranchesTags
Open more actions menu

Latest commit

 

History

791 Commits
791 Commits

Folders and files

NameName
Last commit message
Last commit date
.config
.config
 
 
.devcontainer
.devcontainer
 
 
.github
.github
 
 
.tools
.tools
 
 
.vscode
.vscode
 
 
docfx
docfx
 
 
src
src
 
 
test
test
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
Directory.Build.props
Directory.Build.props
 
 
Directory.Build.rsp
Directory.Build.rsp
 
 
Directory.Build.targets
Directory.Build.targets
 
 
Directory.Packages.props
Directory.Packages.props
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
SharpSchema.sln
SharpSchema.sln
 
 
global.json
global.json
 
 
nuget.config
nuget.config
 
 
settings.VisualStudio.json
settings.VisualStudio.json
 
 
version.json
version.json
 
 

Repository files navigation

CI NuGet Version CodeQL codecov

SharpSchema

SharpSchema is a .NET library and tool for generating JSON schemas from .NET types. It provides a flexible and extensible API for customizing the generated schemas, and includes a command-line tool for generating schemas from compiled .NET assemblies.

It leverages the excellent JsonSchema.NET library.

The CLI tool requires .NET SDK 8.

The SharpSchema.Annotations library is a netstandard2.0 class library that provides a set of attributes for influencing the output of SharpSchema.

It is also available as source-only package, that will compile the attributes into your target assembly. Using SharpSchema.Annotations.Source means you won't have to ship another dependency.

Examples

SimplePerson

namespace Contoso;

[SchemaRoot(Filename = "person.schema.json")]
public record SimplePerson(
    string Surname,
    string? FamilyName,
    DateTime DateOfBirth,
    SimplePerson.RoleKind Role)
{
    public enum RoleKind
    {
        User,
        SectionAdmin,
        SystemAdmin,
    }

    [SchemaIgnore]
    public int Age => (int)((DateTime.Now - this.DateOfBirth).TotalDays / 365.25);
}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "surname": {
      "$comment": "string",
      "type": "string",
      "title": "Surname"
    },
    "familyName": {
      "oneOf": [
        {
          "$comment": "string",
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Family Name"
    },
    "dateOfBirth": {
      "$comment": "DateTime",
      "type": "string",
      "format": "date-time",
      "title": "Date of Birth"
    },
    "role": {
      "$comment": "RoleKind",
      "type": "string",
      "enum": [
        "user",
        "section-admin",
        "system-admin"
      ],
      "title": "Role"
    }
  },
  "required": [
    "surname",
    "dateOfBirth",
    "role"
  ],
  "additionalProperties": false
}

Installation

You can install the CLI SharpSchema.Tool globally using the following command:

dotnet tool install -g SharpSchema.Tool

This will make the sharpschema command available globally in your command line.

More about .NET tools packages

Usage

You can use the sharpschema command to generate a JSON schema from a .NET type. Here's the basic usage:

sharpschema generate -a <assembly-path> -c <type-name>

  • <assembly-path>: The path to the .NET assembly (.dll file) that contains the type.
  • <type-name>: The full name of the type (including the namespace).

For example, if you have a type MyNamespace.MyType in an assembly at ./bin/Debug/net8.0/MyAssembly.dll, you would use the following command:

sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType

This will output the JSON schema to the console. If you want to save the schema to a file, you can redirect the output like this:

sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType -o .

Adding reference assemblies

Use -r <assembly-path> to add a reference assembly required to load the primary assembly. Use one -r for each reference assembly.

If many reference assemblies are in a single directory, use -d <path-to-directory> to add all the assemblies in that directory as reference assemblies. The -t option can be used to set the directory recursion depth.

For example, when the primary assembly is a .NET Framework assemply, you may want to reference all the assemblies in the BCL:

sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType -o . -d "C:\Windows\Microsoft.NET\Framework\v4.0.30319" -t 2

Other options

Run sharpschema --help for the full list of available options.

Integration into a Build

You can integrate SharpSchema.Tool into your build process by adding a post-build event that runs the sharpschema command.

First, add SharpSchema.Tool as a dotnet local tool to your repository.

Then, in your .csproj file, you can add a target similar to the following (this is for the example above):

<Target Name="GenerateSchema" AfterTargets="PostBuildEvent">
  <ItemGroup>
    <_Reference Include="@(ReferencePath)" Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades'" />
  </ItemGroup>
  <PropertyGroup>
    <_SharpSchemaCommand>dotnet tool run sharpschema -- -a $(TargetPath) -c Contoso.SimplePerson -o . --overwrite</_SharpSchemaCommand>
  </PropertyGroup>
    <Exec Command="$(_SharpSchemaCommand) @(_Reference->' -r &quot;%(FullPath)&quot;', ' ')" />
</Target>

This will run the sharpschema command after every build, generating a JSON schema for Contoso.SimplePerson and saving it to the current directory with the filename person.schema.json.

Library

Here's a basic example of how to use the SharpSchema library to generate a JSON schema from a .NET type at runtime.

using Contoso;
using SharpSchema;

var rootTypeContext = RootTypeContext.FromType(typeof(SimplePerson));

TypeConverter converter = new(new TypeConverter.Options
{
    EnumAsUnderlyingType = true,
    MaxDepth = 10,
});

JsonSchema schema = converter
    .Convert(rootTypeContext)
    .Build();

Contributing

If you have any issues or want to contribute, please open an issue or a pull request on the GitHub repository. See the CONTRIBUTING file for more details.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

An opiniated library for generating JSON Schema from a C# type hierarchy

trippwill.github.io/sharp-schema/

Topics

csharpdotnet-toolgeneratorjsonjson-schemaschema

Resources

Readme
MIT license

Contributing

Contributing
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

Generated from AArnott/Library.Template
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.