fix(ts): added propertyNames support to ts - #3122
Open
carloscasellas wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds support for JSON Schema's
propertyNameskeyword when it restricts an object's keys to a finite set of strings (viaenumorconst). Previously this constraint was silently ignored.Partial<Record<"key1" | "key2", V>>instead of an unconstrained index signature.required, they're expanded into explicit (non-optional) class properties rather than left as an unconstrained map.Related Issue
Fixes #1959
Motivation and Context
propertyNamesrestricting keys to an enum was ignored entirely, so{ [key: string]: T }was generated with nothing indicating which keys are actually allowed — losing type safety and letting invalid keys through undetected at runtime in JS/TS.Previous Behaviour / Output
Given a schema like:
{ "languages": { "type": "object", "propertyNames": { "type": "string", "enum": ["de", "en"] }, "additionalProperties": { "type": "object", "properties": { "name": { "type": "string" } } } } }TypeScript generated:
with no indication that only
deandenare valid keys, and the JS runtime converter accepted any key.New Behaviour / Output
TypeScript now generates:
and the JavaScript runtime converter rejects any key outside
["de", "en"]with aninvalidValueerror.If a subset of the allowed keys is also
required, they become explicit properties, e.g.:How Has This Been Tested?
test/inputs/schema/property-names.schema, with a positive sample (property-names.1.json,property-names.2.json) and a negative sample (property-names.1.fail.property-names.json) that must be rejected at runtime by languages declaring the newproperty-namesfeature (TypeScript, JavaScript, Flow — enabled intest/languages.ts).test/unit/property-names-schema.test.ts(12 tests) covering:Partial<Record<...>>Record<...>)required+additionalProperties: false) still erroringpattern, mixed-typeenum,$ref) correctly leaving the map unconstrainedtsc --noEmitonquicktype-core— clean.npm run test:unit— all passing.