-
-
Notifications
You must be signed in to change notification settings - Fork 428
Description
Description
The propertyNames support added in #1302 generates Record<K, V>, which implies all keys are required. However, per the JSON Schema 2020-12 specification, propertyNames only constrains which keys are allowed, it does not make them required.
JSON Schema spec reference
propertyNames (Section 10.3.2.4):
If the instance is an object, this keyword validates if every property name in the instance validates against the provided schema.
It only validates names of properties that exist. It says nothing about requiring those properties to be present.
Compare with required (Section 6.5.3)
An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
Example
Given this schema:
LocalizedString:
type: object
additionalProperties:
type: string
maxLength: 128
propertyNames:
$ref: '#/components/schemas/TranslationLanguage'
TranslationLanguage:
type: string
enum: [en, fr, de, es]Current output
export type LocalizedString = Record<TranslationLanguage, string>;This requires all enum values to be present as keys, which is incorrect.
Expected output
export type LocalizedString = Partial<Record<TranslationLanguage, string>>;This correctly means: keys are optional, but when present, must be a valid TranslationLanguage.
Root cause
In src/configuration.ts, RecordType always generates Record<$A1, $A2>. There is no Partial<> wrapper. Since propertyNames (without required) means keys are allowed but not mandatory, the output should be wrapped in Partial<>.
Happy to open a PR!