{"id":22065629,"url":"https://github.com/karenpayneoregon/appsettings-schema","last_synced_at":"2026-05-10T09:40:27.746Z","repository":{"id":195107376,"uuid":"692264010","full_name":"karenpayneoregon/appsettings-schema","owner":"karenpayneoregon","description":"Teaches using schema files for appsettings.json","archived":false,"fork":false,"pushed_at":"2023-09-18T13:24:51.000Z","size":1373,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-29T00:29:01.791Z","etag":null,"topics":["appsettings-json","appsettings-schema","appsettingson-configuration","csharp-core","visual-studio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karenpayneoregon.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2023-09-16T00:35:15.000Z","updated_at":"2023-11-14T10:04:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"ae2b12c6-61c9-402f-ad6d-6782c8c0cd7f","html_url":"https://github.com/karenpayneoregon/appsettings-schema","commit_stats":null,"previous_names":["karenpayneoregon/appsettings-schema"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Fappsettings-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Fappsettings-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Fappsettings-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Fappsettings-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karenpayneoregon","download_url":"https://codeload.github.com/karenpayneoregon/appsettings-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245144978,"owners_count":20568056,"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":["appsettings-json","appsettings-schema","appsettingson-configuration","csharp-core","visual-studio"],"created_at":"2024-11-30T19:20:58.068Z","updated_at":"2026-05-10T09:40:27.697Z","avatar_url":"https://github.com/karenpayneoregon.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Visual Studio Appsettings.json schemas\n\nLearn how to create custom schemas for appsettings.json with validation and intellisense.\n\nValidation works best for a team where **code reviews** are performed and that checking the configuration file is part of the review. This provides consistency that abides but team standards.\n\nTo appreciate [intellisense](https://learn.microsoft.com/en-us/visualstudio/ide/using-intellisense?view=vs-2022) aspect watch the video below.\n\n\u003ciframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/Ei_mIzsniLg?si=qxmQJ_TDWL3FJHEx\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen\u003e\u003c/iframe\u003e\n\n![figure 1](assets/Figure1.png)\n\n\n\nWhen creating a new web application, a file named [appsettings.json](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0) is included using the following schema.\n\n[https://json.schemastore.org/appsettings.json](https://json.schemastore.org/appsettings.json)\n\nWhich is length (in regards to properties e.g. like NLog is included) and in most cases more than needed yet using this schema is fine.\n\n## What is a schema file? \n\nA file for containing format and structure which\n\n- Describes your existing data format.\n- Clear, human- and machine-readable documentation.\n- Complete structural validation, useful for automated testing.\n- Complete structural validation, validating client-submitted data.\n\n\n## Creating custom schemas\n\nAlthough the following are done for console and ASP.NET Core what is presented will work for other project types as it is project agnostics. \n\n### Example 1 console project\n\nThe first step is for a developer to learn the basics of working with schemas.\n\nThis can be done by creating a custom configuration file e.g.\n\n\n```json\n{\n  \"ConnectionsConfiguration\": {\n    \"ActiveEnvironment\": \"Development\",\n    \"Development\": \"Data Source=.\\\\SQLEXPRESS;Initial Catalog=NorthWind2022;Integrated Security=True;Encrypt=False\", \n    \"Stage\": \"Stage connection string goes here\",\n    \"Production\": \"Prod connection string goes here\"\n  }\n}\n```\n\nNext figure out C# code to read settings from the configuration above, in this case the author of this article created an [NuGet package](https://www.nuget.org/packages/ConfigurationLibrary/1.0.4?_src=template) designed to write utility applications without dependency injection.\n\n**ActiveEnvironment** tells ConfigurationLibrary to read a connection string for either Development, Stage or Production. This is a required property in appsettings.json when using the schema in SimpleConnectionConsoleApp\\LocalSchemas\\connectionStrings1.json\n\n**Development** is also a reqirement but not Stage or Production.\n\nTo create a connection, in this case to a console project, add the following [NuGet package](https://www.nuget.org/packages/ConfigurationLibrary/1.0.4?_src=template) to the project.\n\n1. Add a static using\n1. Add Connection() method to the SqlConnection\n\n\n![figure 2](assets/Figure2.png)\n\nThis works with any managed data provider.\n\n\n### Example 2 ASP.NET Core/Razor Pages\n\nThe default appsettings.json file included with ASP.NET Core.\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Default\": \"Information\",\n      \"Microsoft.AspNetCore\": \"Warning\"\n    }\n  },\n  \"AllowedHosts\": \"*\"\n}\n```\n\nThen one for a connection string where most developers leave as DefaultConnection which is fine for one connection yet there may be causes for more connections.\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Default\": \"Information\",\n      \"Microsoft.AspNetCore\": \"Warning\"\n    }\n  },\n  \"AllowedHosts\": \"*\",\n  \"ConnectionStrings\": {\n    \"DefaultConnection\": \"Data Source=.\\\\SQLEXPRESS;Initial Catalog=NorthWind2022;Integrated Security=True;Encrypt=False\"\n  }\n}\n```\n\nUsing a custom schema an enforcement can be made to change from DefaultConnection to perhaps MainConnection.\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Microsoft.EntityFrameworkCore.Database.Command\": \"Information\",\n      \"Default\": \"Information\",\n      \"Microsoft.AspNetCore\": \"Warning\"\n    }\n  },\n  \"AllowedHosts\": \"*\",\n  \"ConnectionStrings\": {\n    \"MainConnection\": \"Data Source=.\\\\SQLEXPRESS;Initial Catalog=NorthWind2022;Integrated Security=True;Encrypt=False\"\n  }\n}\n```\n\nTo keep things simple add a second connection string named SecondaryConnection.\n\n```json\n{\n  \"Logging\": {\n    \"LogLevel\": {\n      \"Microsoft.EntityFrameworkCore.Database.Command\": \"Information\",\n      \"Default\": \"Information\",\n      \"Microsoft.AspNetCore\": \"Warning\"\n    }\n  },\n  \"AllowedHosts\": \"*\",\n  \"ConnectionStrings\": {\n    \"MainConnection\": \"Data Source=.\\\\SQLEXPRESS;Initial Catalog=NorthWind2022;Integrated Security=True;Encrypt=False\",\n    \"SecondaryConnection\": \"Server=(localdb)\\\\MSSQLLocalDB;Database=NorthWind;Trusted_Connection=True\"\n  }\n}\n```\n\nIn Program.cs\n\n```csharp\nbuilder.Services.AddDbContext\u003cContext\u003e(options =\u003e\n    options.UseSqlServer(builder.Configuration.GetConnectionString(\"MainConnection\"))\n        .EnableSensitiveDataLogging());\n```\n\n## Loose validation\n\nUsing the last appsetting.json above which uses MainConnection, a developer can use Connection or whatever they want until there is a code review to enforce the standard, otherwise match this with Program.cs and the connection is still valid.\n\n![figure 3](assets/figure3.png)\n\n## Providing options\n\nTo provide options specify **enum**.\n\n```json\n\"enum\": [\n  \"Server=(localdb)\\\\MSSQLLocalDB;Database=NorthWind;Trusted_Connection=True\",\n  \"Data Source=.\\\\SQLEXPRESS;Initial Catalog=NorthWind2022;Integrated Security=True;Encrypt=False\"\n]\n```\n\nBy providing options or one option\n\n- A developer does not have to type in the connection which could end up with a malformed connection string\n- Developers know which are valid connections\n\n## How to create a schema\n\n- First create a json file\n- Create class(es) to contain data if needed\n- Serialize and deserialize the file\n- Test\n- Review that the above meets requirements\n- Rather than create the schema by hand use a tool.\n\nRecommendation, itential [schema generator](https://json-to-schema.itential.io/).\n\nOnce there click on the blue question mark, top right corner of the page to get a walkthrough.\n\n1. Copy desired json in the input on the left side of the page\n1. Click infer Schema button\n1. A schema appears in the right pane\n1. Next open nodes in the left pane and edit as needed\n\n:x: in the top right corner of inputs when clicked removes data for the input.\n\nExample node taken from the json above.\n\n![figure 4](assets/figure4.png)\n\nIn a test project, create a folder **LocalSchemas** then create an empty json file and copy the schema above into this file and save. Let's call the file **connectionStrings1.json**\n\nAt the root folder of the same project, add a new appsettings.json file.\n\nIn the new appsettings.json file add\n\n```\nLocalSchemas\\connectionStrings1.json\n```\n\nTo the schema input.\n\n![figure 5](assets/figure5.png)\n\nType two Bob Hope brackets and the schema starts validition.\n\n![figure 6](assets/figure6.png)\n\nPlace the cursor between the brackets and press \u003ckbd\u003eCTRL\u003c/kbd\u003e, \u003ckbd\u003espace\u003c/kbd\u003e for Intellisense to kick in.\n\n![figure 7](assets/figure7.png)\n\nIn all is fine the next step is to upload the schema file online and ensure the schema file is available to whomever is going to use the schema.\n\nAt the very least a schema file can be hosted in a GitHub repository. Once pushed to the repository, go to raw view and copy then paste to a desired appsettings.json file.\n\n\n**Caveat** if a new version of a schema file is pushed to the repository and the schema does not validate in the appsettings.json file.\n\nIn the schema input at the end type in **?reload** and press enter.\n\n## Source code\n\nClone the following [GitHub repository](https://github.com/karenpayneoregon/appsettings-schema).\n\n## Resources\n\n- [Storing and reading values from appsettings.json](https://dev.to/karenpayneoregon/storing-and-reading-values-from-appsettingsjson-io)\n- [Intellisense for JSON Schema in the JSON Editor](https://devblogs.microsoft.com/dotnet/intellisense-for-json-schema-in-the-json-editor/)\n- [JSON Schema Validator](https://www.jsonschemavalidator.net/)\n- [Json.NET Schema](https://www.newtonsoft.com/jsonschema)\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Fappsettings-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarenpayneoregon%2Fappsettings-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Fappsettings-schema/lists"}