ts-json-schema-generator
ts-json-schema-generator copied to clipboard
Support patternProperties
Typescript-restricted key (fail)
// test_str.ts
export type A = `wifi${number}`;
// actual_str.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"type": "string"
}
}
}
// expected_str.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"type": "string",
"pattern": "^wifi[0-9]+$"
}
}
}
Annotated string (pass)
// test_annotated_str.ts
/**
* @pattern ^wifi[0-9]+$
*/
export type A = `wifi${number}`;
// actual_annotated_str.json
// expected_annotated_str.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"pattern": "^wifi[0-9]+$",
"type": "string"
}
}
}
Annotated key (kinda pass)
// test_annotated_key.ts
/**
* @pattern ^wifi[0-9]+$
*/
type WifiDevName = string;
export type A = Record<WifiDevName, number>;
// actual_annotated_key.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"additionalProperties": {
"type": "number"
},
"propertyNames": {
"pattern": "^wifi[0-9]+$"
},
"type": "object"
}
}
}
// expected_annotated_key.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"patternProperties": {
"^wifi[0-9]+$": {
"type": "number"
}
},
"type": "object"
}
}
}
Real life object (fail)
// test_real_life.ts
/**
* @pattern ^lan[0-9]+$
*/
type LedLanKey = `lan${number}`;
/**
* @pattern ^radio[0-9]+$
*/
type LedRadioKey = `radio${number}`;
type LedsLan = Record<LedLanKey, object>;
type LedsRadio = Record<LedRadioKey, object>;
export type A = LedsLan &
LedsRadio & {
power?: object;
};
// actual_real_life.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"additionalProperties": {
"type": "object"
},
"properties": {
"power": {
"type": "object"
}
},
"type": "object"
}
}
}
// expected_real_life.json
{
"$ref": "#/definitions/A",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"properties": {
"power": {
"type": "object"
}
},
"patternProperties": {
"^lan[0-9]+$": {
"type": "object"
},
"^radio[0-9]+$": {
"type": "object"
}
},
"type": "object"
}
}
}
I tried to read the code to contribute fix but didn't understand where to start.
Honestly, this was my question - how to use pattern for regexp. Would be glad to see this working