Skip to content

Commit 424f33f

Browse files
Natively support const keyword (#31)
* Update json-schema-to-typescript version * Use deep clone before compiling
1 parent 0130a08 commit 424f33f

File tree

2 files changed

+5
-35
lines changed

2 files changed

+5
-35
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"url": "https://github.com/serverless/typescript/issues"
2626
},
2727
"homepage": "https://github.com/serverless/typescript#readme",
28-
"dependencies": {},
2928
"devDependencies": {
3029
"@types/json-schema": "^7.0.6",
3130
"@typescript-eslint/eslint-plugin": "^4.10.0",
@@ -35,7 +34,8 @@
3534
"eslint-config-prettier": "^7.1.0",
3635
"eslint-plugin-import": "^2.22.1",
3736
"eslint-plugin-prettier": "^3.3.0",
38-
"json-schema-to-typescript": "^10.0.2",
37+
"json-schema-to-typescript": "^10.1.3",
38+
"lodash": "^4.17.21",
3939
"prettier": "^2.2.1",
4040
"serverless": "*",
4141
"typescript": "^4.0.5"

src/plugin.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { compile } from "json-schema-to-typescript";
22
import fs from "fs";
33
import type { JSONSchema4 } from "json-schema";
4+
import * as _ from "lodash";
45

56
interface Serverless {
67
configSchemaHandler: {
@@ -12,7 +13,7 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
1213
private schema: JSONSchema4;
1314

1415
constructor(serverless: Serverless) {
15-
this.schema = serverless.configSchemaHandler.schema;
16+
this.schema = _.cloneDeep(serverless.configSchemaHandler.schema);
1617
}
1718

1819
commands = {
@@ -27,45 +28,14 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
2728
};
2829

2930
async generateSchema() {
30-
/**
31-
* https://github.com/serverless/typescript/issues/4
32-
* JSON Schema v6 `const` keyword converted to `enum`
33-
*/
34-
const normalizedSchema = replaceAllConstForEnum(this.schema);
35-
3631
/**
3732
* ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples
3833
*/
39-
const compiledDefinitions = await compile(normalizedSchema, "AWS", {
34+
const compiledDefinitions = await compile(this.schema, "AWS", {
4035
ignoreMinAndMaxItems: true,
41-
unreachableDefinitions: true,
4236
});
4337
fs.writeFileSync("index.d.ts", compiledDefinitions);
4438
}
4539
}
4640

47-
const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => {
48-
if ("object" !== typeof schema) {
49-
return schema;
50-
}
51-
52-
return Object.fromEntries(
53-
Object.entries(schema).map(([key, value]) => {
54-
if (key === "const") {
55-
return ["enum", [value]];
56-
}
57-
58-
if (Array.isArray(value)) {
59-
return [key, value.map(replaceAllConstForEnum)];
60-
}
61-
62-
if ("object" === typeof value && value !== null) {
63-
return [key, replaceAllConstForEnum(value)];
64-
}
65-
66-
return [key, value];
67-
})
68-
);
69-
};
70-
7141
module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;

0 commit comments

Comments
 (0)