Skip to content

Commit 7cc8d8a

Browse files
authored
Merge pull request #3276 from glideapps/fix/effect-schema-array-cardinality
Validate Effect Schema array cardinality
2 parents d58ef74 + 6def3a7 commit 7cc8d8a

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

packages/quicktype-core/src/language/TypeScriptEffectSchema/TypeScriptEffectSchemaRenderer.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { arrayIntercalate } from "collection-utils";
22

3+
import { minMaxItemsForType } from "../../attributes/Constraints.js";
34
import { ConvenienceRenderer } from "../../ConvenienceRenderer.js";
45
import { type Name, type Namer, funPrefixNamer } from "../../Naming.js";
56
import type { RenderContext } from "../../Renderer.js";
@@ -117,11 +118,22 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
117118
(_integerType) => "S.Int",
118119
(_doubleType) => "S.Number",
119120
(_stringType) => "S.String",
120-
(arrayType) => [
121-
"S.Array(",
122-
this.typeMapTypeFor(arrayType.items, false),
123-
")",
124-
],
121+
(arrayType) => {
122+
const [minItems, maxItems] =
123+
minMaxItemsForType(arrayType) ?? [];
124+
const schema: Sourcelike[] = [
125+
"S.Array(",
126+
this.typeMapTypeFor(arrayType.items, false),
127+
")",
128+
];
129+
if (minItems !== undefined && minItems > 0) {
130+
schema.push(".pipe(S.minItems(", minItems.toString(), "))");
131+
}
132+
if (maxItems !== undefined) {
133+
schema.push(".pipe(S.maxItems(", maxItems.toString(), "))");
134+
}
135+
return schema;
136+
},
125137
(_classType) => panic("Should already be handled."),
126138
(_mapType) => [
127139
"S.Record({ key: S.String, value: ",

test/languages.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,14 @@ export const TypeScriptEffectSchemaLanguage: Language = {
16851685
"26b49.json",
16861686
],
16871687
allowMissingNull: false,
1688-
features: ["enum", "union", "no-defaults", "integer", "strict-optional"],
1688+
features: [
1689+
"enum",
1690+
"union",
1691+
"no-defaults",
1692+
"integer",
1693+
"strict-optional",
1694+
"minmaxitems",
1695+
],
16891696
output: "TopLevel.ts",
16901697
topLevel: "TopLevel",
16911698
skipJSON: [],

0 commit comments

Comments
 (0)