Skip to content

Add support for JSON Schema const keyword#1891

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/add-const-keyword-support
Open

Add support for JSON Schema const keyword#1891
Copilot wants to merge 3 commits intomasterfrom
copilot/add-const-keyword-support

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

JSON Schema draft 6 defines a const keyword that constrains a value to a single specific value. This was not parsed, validated, or reflected in generated C# or TypeScript code.

Changes

Core Schema (NJsonSchema)

  • JsonSchema: Added Const (object?) property serialized as "const", with HasConstValue and ConstValueType helpers — the latter infers the semantic JSON type (string/integer/number/boolean) from the runtime JValue type
  • ValidationErrorKind: Added ConstantValueMismatch
  • JsonSchemaValidator: Added ValidateConst using JToken.DeepEquals for type-aware comparison (avoids false mismatches like 42 vs 42.0)

C# Code Generation

  • CSharpTypeResolver: Infers C# type from const value when no type is specified (bypasses the IsAnyType short-circuit)
  • PropertyModel: HasSetter returns false for const properties — they generate read-only auto-properties
  • ValueGeneratorBase: GetDefaultValue uses const value as initializer regardless of useSchemaDefault; new GetConstantValue virtual method formats values with invariant-culture numeric precision

TypeScript Code Generation

  • TypeScriptTypeResolver: Returns a TypeScript literal type for const schemas (bypasses the IsAnyType short-circuit)

Example

Schema:

{ "properties": { "cmdType": { "const": "person" } } }

Generated C#:

public string CmdType { get; } = "person";

Generated TypeScript (interface):

cmdType: "person";

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://json-schema.org:80/draft-04/schema
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpw5orSR.tmp 5359f0.ts k/NJ�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_5b71db70-6e0b-4323-8a58-0462a95fe18a.ts p_e1d355b2-5b5f-4f7c-be4a-5845322a7084.ts sonSchema/src/NJsonSchema.CodeGeneration.TypeScript.Tests/node_modules/.bin/sh Script.Tests/temp_942af1c4-c91b-49a7-9495-6337341e00bb.ts f07547.ts odules/.bin/sh d0838ca85c58.ts /hom�� sonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratiosh -6931-4676-ac64-/home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGenerati-c sh n.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts cts/bin/NJsonSch-c sonSchema.CodeGe"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_58df9e39-82c6-432d-9414-0617d679ff74.ts 17993a.ts (http block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests @@ /tmp/tmp98DHIF.tmp 5359f0.ts ema.�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_5b71db70-6e0b-4323-8a58-0462a95fe18a.ts 4ad6-af65-04c325728f28.ts sonSchema/src/NJsonSchema.CodeGeneration.TypeScript.Tests/node_modules/.bin/sh Script.Tests/temp_e8f714c7-8ad3-4469-91de-9a091ecd703c.ts f07547.ts odules/.bin/sh d0838ca85c58.ts /hom�� sonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratiosh c7b773.ts /../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_3801dcef-dea8-4af2-be90-68aef6"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratio n.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts 42bd196a038c.ts modules/@npmcli/"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_58df9e39-82c6-432d-9414-0617d679ff74.ts 17993a.ts (http block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpaOsh8a.tmp 4551-a834-46c97d8f3d6d.ts sonS�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts 28ad04d6570f.ts ode Script.Tests/temp_e8f714c7-8ad3-4469-91de-9a091ecd703c.ts eGeneration.Type-c odules/.bin/node"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_dc48cadc-4c17-4913-8b1a-964ae10c5db4.ts 5631235fea31.ts sonS�� ema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.Typetail 66665d2b6ece.ts (http block)
  • schemas.sportradar.com
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpw5orSR.tmp 5359f0.ts k/NJ�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_5b71db70-6e0b-4323-8a58-0462a95fe18a.ts p_e1d355b2-5b5f-4f7c-be4a-5845322a7084.ts sonSchema/src/NJsonSchema.CodeGeneration.TypeScript.Tests/node_modules/.bin/sh Script.Tests/temp_942af1c4-c91b-49a7-9495-6337341e00bb.ts f07547.ts odules/.bin/sh d0838ca85c58.ts /hom�� sonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratiosh -6931-4676-ac64-/home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGenerati-c sh n.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts cts/bin/NJsonSch-c sonSchema.CodeGe"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_58df9e39-82c6-432d-9414-0617d679ff74.ts 17993a.ts (dns block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests @@ /tmp/tmp98DHIF.tmp 5359f0.ts ema.�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_5b71db70-6e0b-4323-8a58-0462a95fe18a.ts 4ad6-af65-04c325728f28.ts sonSchema/src/NJsonSchema.CodeGeneration.TypeScript.Tests/node_modules/.bin/sh Script.Tests/temp_e8f714c7-8ad3-4469-91de-9a091ecd703c.ts f07547.ts odules/.bin/sh d0838ca85c58.ts /hom�� sonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratiosh c7b773.ts /../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_3801dcef-dea8-4af2-be90-68aef6"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneratio n.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts 42bd196a038c.ts modules/@npmcli/"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_58df9e39-82c6-432d-9414-0617d679ff74.ts 17993a.ts (dns block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpaOsh8a.tmp 4551-a834-46c97d8f3d6d.ts sonS�� neration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_3d90c537-de83-4880-a971-2e38dd07403d.ts 28ad04d6570f.ts ode Script.Tests/temp_e8f714c7-8ad3-4469-91de-9a091ecd703c.ts eGeneration.Type-c odules/.bin/node"tsc" /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.TypeScript.Tests/temp_dc48cadc-4c17-4913-8b1a-964ae10c5db4.ts 5631235fea31.ts sonS�� ema.CodeGeneration.TypeScript.Tests/debug_net8.0/../../../../src/NJsonSchema.CodeGeneration.Typetail 66665d2b6ece.ts (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Support of const keyword</issue_title>
<issue_description>I'm not a Json schema deep diver, but somehow I miss support for the const keyword (at least for C# code generation):

{
"title": "Person",
"type": "object",
"properties": {
"cmdType": {
"$comment": "This results in a string template literal when generating TS",
"const": "person"
},
...
}</issue_description>

Comments on the Issue (you are @copilot in this section)

@RicoSuter Ref: http://json-schema.org/understanding-json-schema/reference/generic.html#constant-values

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI force-pushed the copilot/add-const-keyword-support branch from 33f0762 to 795e2f3 Compare March 1, 2026 20:29
Copilot AI changed the title [WIP] Support const keyword for C# code generation Add support for JSON Schema const keyword Mar 1, 2026
Copilot finished work on behalf of lahma March 1, 2026 20:32
@lahma lahma marked this pull request as ready for review March 2, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support of const keyword

2 participants