{"id":23584076,"url":"https://github.com/trippwill/sharp-schema","last_synced_at":"2026-02-14T00:03:46.907Z","repository":{"id":234919414,"uuid":"786356744","full_name":"trippwill/sharp-schema","owner":"trippwill","description":"An opiniated library for generating JSON Schema from a C# type hierarchy","archived":false,"fork":false,"pushed_at":"2025-02-10T19:36:11.000Z","size":1212,"stargazers_count":0,"open_issues_count":6,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T20:33:39.574Z","etag":null,"topics":["csharp","dotnet-tool","generator","json","json-schema","schema"],"latest_commit_sha":null,"homepage":"https://trippwill.github.io/sharp-schema/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trippwill.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-14T07:48:05.000Z","updated_at":"2025-01-21T02:29:07.000Z","dependencies_parsed_at":"2024-04-28T01:45:25.486Z","dependency_job_id":"1078d584-e7ca-4642-b659-a80bee9f6275","html_url":"https://github.com/trippwill/sharp-schema","commit_stats":{"total_commits":605,"total_committers":10,"mean_commits":60.5,"dds":0.2710743801652893,"last_synced_commit":"ec1f846c2ed11b018471d011d6bb65e96ef5eedd"},"previous_names":["trippwill/sharp-schema"],"tags_count":13,"template":false,"template_full_name":"AArnott/Library.Template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trippwill%2Fsharp-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trippwill%2Fsharp-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trippwill%2Fsharp-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trippwill%2Fsharp-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trippwill","download_url":"https://codeload.github.com/trippwill/sharp-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239403600,"owners_count":19632617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet-tool","generator","json","json-schema","schema"],"created_at":"2024-12-27T02:20:09.959Z","updated_at":"2026-02-14T00:03:41.874Z","avatar_url":"https://github.com/trippwill.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![CI](https://github.com/trippwill/sharp-schema/actions/workflows/libanvl-dotnet-ci.yml/badge.svg)](https://github.com/trippwill/sharp-schema/actions/workflows/libanvl-dotnet-ci.yml)\n[![NuGet Version](https://img.shields.io/nuget/vpre/SharpSchema.Tool)](https://www.nuget.org/packages/SharpSchema.Tool/)\n[![CodeQL](https://github.com/trippwill/sharp-schema/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/trippwill/sharp-schema/actions/workflows/github-code-scanning/codeql)\n[![codecov](https://codecov.io/gh/trippwill/sharp-schema/graph/badge.svg?token=GGB46BYHZ8)](https://codecov.io/gh/trippwill/sharp-schema)\n\n# SharpSchema\n\nSharpSchema 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.\n\nIt leverages the excellent [JsonSchema.NET library](https://github.com/gregsdennis/json-everything).\n\nThe CLI tool requires .NET SDK 8.\n\nThe `SharpSchema.Annotations` library is a `netstandard2.0` class library that provides a set\nof attributes for influencing the output of SharpSchema.\n\nIt is also available as source-only package, that will compile the attributes into your target assembly.\nUsing `SharpSchema.Annotations.Source` means you won't have to ship another dependency.\n\n## Examples\n\n### SimplePerson\n\n```csharp\nnamespace Contoso;\n\n[SchemaRoot(Filename = \"person.schema.json\")]\npublic record SimplePerson(\n    string Surname,\n    string? FamilyName,\n    DateTime DateOfBirth,\n    SimplePerson.RoleKind Role)\n{\n    public enum RoleKind\n    {\n        User,\n        SectionAdmin,\n        SystemAdmin,\n    }\n\n    [SchemaIgnore]\n    public int Age =\u003e (int)((DateTime.Now - this.DateOfBirth).TotalDays / 365.25);\n}\n\n```\n\n```json\n{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"surname\": {\n      \"$comment\": \"string\",\n      \"type\": \"string\",\n      \"title\": \"Surname\"\n    },\n    \"familyName\": {\n      \"oneOf\": [\n        {\n          \"$comment\": \"string\",\n          \"type\": \"string\"\n        },\n        {\n          \"type\": \"null\"\n        }\n      ],\n      \"title\": \"Family Name\"\n    },\n    \"dateOfBirth\": {\n      \"$comment\": \"DateTime\",\n      \"type\": \"string\",\n      \"format\": \"date-time\",\n      \"title\": \"Date of Birth\"\n    },\n    \"role\": {\n      \"$comment\": \"RoleKind\",\n      \"type\": \"string\",\n      \"enum\": [\n        \"user\",\n        \"section-admin\",\n        \"system-admin\"\n      ],\n      \"title\": \"Role\"\n    }\n  },\n  \"required\": [\n    \"surname\",\n    \"dateOfBirth\",\n    \"role\"\n  ],\n  \"additionalProperties\": false\n}\n```\n\n## Installation\n\nYou can install the CLI SharpSchema.Tool globally using the following command:\n\ndotnet tool install -g SharpSchema.Tool\n\nThis will make the `sharpschema` command available globally in your command line.\n\n[More about .NET tools packages](https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools)\n\n## Usage\n\nYou can use the `sharpschema` command to generate a JSON schema from a .NET type. Here's the basic usage:\n\n`sharpschema generate -a \u003cassembly-path\u003e -c \u003ctype-name\u003e`\n\n- `\u003cassembly-path\u003e`: The path to the .NET assembly (.dll file) that contains the type.\n- `\u003ctype-name\u003e`: The full name of the type (including the namespace).\n\nFor example, if you have a type `MyNamespace.MyType` in an assembly at `./bin/Debug/net8.0/MyAssembly.dll`, you would use the following command:\n\n`sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType`\n\nThis 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:\n\n`sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType  -o .`\n\n### Adding reference assemblies\n\nUse `-r \u003cassembly-path\u003e` to add a reference assembly required to load the primary assembly. Use one `-r` for each reference assembly.\n\nIf many reference assemblies are in a single directory, use `-d \u003cpath-to-directory\u003e` to add all the assemblies in that directory as reference assemblies. The `-t` option can be used to set the directory recursion depth.\n\nFor example, when the primary assembly is a .NET Framework assemply, you may want to reference all the assemblies in the BCL:\n\n`sharpschema generate -a ./bin/Debug/net8.0/MyAssembly.dll -c MyNamespace.MyType  -o . -d \"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\" -t 2`\n\n## Other options\n\nRun `sharpschema --help` for the full list of available options.\n\n## Integration into a Build\n\nYou can integrate SharpSchema.Tool into your build process by adding a post-build event that runs the `sharpschema` command.\n\nFirst, add `SharpSchema.Tool` as a [dotnet local tool](https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-local-tool) to your repository.\n\nThen, in your `.csproj` file, you can add a target similar to the following (this is for the example above):\n\n```xml\n\u003cTarget Name=\"GenerateSchema\" AfterTargets=\"PostBuildEvent\"\u003e\n  \u003cItemGroup\u003e\n    \u003c_Reference Include=\"@(ReferencePath)\" Condition=\"'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades'\" /\u003e\n  \u003c/ItemGroup\u003e\n  \u003cPropertyGroup\u003e\n    \u003c_SharpSchemaCommand\u003edotnet tool run sharpschema -- -a $(TargetPath) -c Contoso.SimplePerson -o . --overwrite\u003c/_SharpSchemaCommand\u003e\n  \u003c/PropertyGroup\u003e\n    \u003cExec Command=\"$(_SharpSchemaCommand) @(_Reference-\u003e' -r \u0026quot;%(FullPath)\u0026quot;', ' ')\" /\u003e\n\u003c/Target\u003e\n```\n\nThis 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`.\n\n## Library\n\nHere's a basic example of how to use the SharpSchema library to generate a JSON schema from a .NET type at runtime.\n\n```csharp\nusing Contoso;\nusing SharpSchema;\n\nvar rootTypeContext = RootTypeContext.FromType(typeof(SimplePerson));\n\nTypeConverter converter = new(new TypeConverter.Options\n{\n    EnumAsUnderlyingType = true,\n    MaxDepth = 10,\n});\n\nJsonSchema schema = converter\n    .Convert(rootTypeContext)\n    .Build();\n```\n\n## Contributing\n\nIf you have any issues or want to contribute, please open an issue or a pull request on the [GitHub repository](https://github.com/tripwill/sharp-schema). See the [CONTRIBUTING](CONTRIBUTING.md) file for more details.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrippwill%2Fsharp-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrippwill%2Fsharp-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrippwill%2Fsharp-schema/lists"}