diff --git a/packages/quicktype-core/src/language/Kotlin/KotlinKlaxonRenderer.ts b/packages/quicktype-core/src/language/Kotlin/KotlinKlaxonRenderer.ts index 5820f80ab..b982f5f9a 100644 --- a/packages/quicktype-core/src/language/Kotlin/KotlinKlaxonRenderer.ts +++ b/packages/quicktype-core/src/language/Kotlin/KotlinKlaxonRenderer.ts @@ -1,6 +1,7 @@ import { arrayIntercalate, iterableSome } from "collection-utils"; import { + minMaxItemsForType, minMaxLengthForType, minMaxValueForType, } from "../../attributes/Constraints.js"; @@ -413,6 +414,17 @@ export class KotlinKlaxonRenderer extends KotlinRenderer { minMaxLengthForType(p.type) ?? []; if (minLength !== undefined) emitLength(">=", minLength); if (maxLength !== undefined) emitLength("<=", maxLength); + const array = p.isOptional + ? `${value}?.let { require(it.size` + : `require(${value}.size`; + const emitItems = (operator: string, bound: number) => + emitValidation( + `${array} ${operator} ${bound}${p.isOptional ? ") }" : ")"}`, + ); + const [minItems, maxItems] = + minMaxItemsForType(p.type) ?? []; + if (minItems !== undefined) emitItems(">=", minItems); + if (maxItems !== undefined) emitItems("<=", maxItems); }); this.emitLine( "public fun toJson() = klaxon.toJsonString(this)", diff --git a/test/languages.ts b/test/languages.ts index 6120bb258..6a8dc9e63 100644 --- a/test/languages.ts +++ b/test/languages.ts @@ -1255,6 +1255,7 @@ export const KotlinLanguage: Language = { "minmax", "minmaxInteger", "minmaxlength", + "minmaxitems", ], output: "TopLevel.kt", topLevel: "TopLevel",