Skip to content

Commit de1e225

Browse files
authored
Merge pull request #3319 from glideapps/fix/objective-c-array-size-constraints
Objective-C: validate array size constraints
2 parents 123f3e4 + 7abe0de commit de1e225

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎packages/quicktype-core/src/language/Objective-C/ObjectiveCRenderer.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
mapSome,
77
} from "collection-utils";
88

9-
import { minMaxValueForType } from "../../attributes/Constraints.js";
9+
import {
10+
minMaxItemsForType,
11+
minMaxValueForType,
12+
} from "../../attributes/Constraints.js";
1013
import {
1114
ConvenienceRenderer,
1215
type ForbiddenWordsInfo,
@@ -873,6 +876,18 @@ export class ObjectiveCRenderer extends ConvenienceRenderer {
873876
`if (dict[@"${objectiveCStringEscape(jsonName)}"] && [dict[@"${objectiveCStringEscape(jsonName)}"] doubleValue] > ${max}) return nil;`,
874877
);
875878
}
879+
const [minItems, maxItems] =
880+
minMaxItemsForType(property.type) ?? [];
881+
if (minItems !== undefined) {
882+
this.emitLine(
883+
`if (dict[@"${objectiveCStringEscape(jsonName)}"] && [(NSArray *)dict[@"${objectiveCStringEscape(jsonName)}"] count] < ${minItems}) return nil;`,
884+
);
885+
}
886+
if (maxItems !== undefined) {
887+
this.emitLine(
888+
`if ([(NSArray *)dict[@"${objectiveCStringEscape(jsonName)}"] count] > ${maxItems}) return nil;`,
889+
);
890+
}
876891
if (
877892
!property.isOptional &&
878893
property.type.isNullable

‎test/languages.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,13 @@ export const ObjectiveCLanguage: Language = {
927927
diffViaSchema: false,
928928
skipDiffViaSchema: [],
929929
allowMissingNull: true,
930-
features: ["enum", "minmax", "minmaxInteger", "strict-optional"],
930+
features: [
931+
"enum",
932+
"minmax",
933+
"minmaxInteger",
934+
"minmaxitems",
935+
"strict-optional",
936+
],
931937
output: "QTTopLevel.m",
932938
topLevel: "QTTopLevel",
933939
skipJSON: [

0 commit comments

Comments
 (0)