Skip to content

Commit 8bc8a4e

Browse files
committed
Validate cJSON string length constraints
1 parent cd31386 commit 8bc8a4e

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎packages/quicktype-core/src/language/CJSON/CJSONRenderer.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { arrayIntercalate } from "collection-utils";
55
/* eslint-disable @typescript-eslint/naming-convention */
66

77
import { getAccessorName } from "../../attributes/AccessorNames.js";
8-
import { minMaxValueForType } from "../../attributes/Constraints.js";
8+
import {
9+
minMaxLengthForType,
10+
minMaxValueForType,
11+
} from "../../attributes/Constraints.js";
912
import { enumCaseValues } from "../../attributes/EnumValues.js";
1013
import {
1114
ConvenienceRenderer,
@@ -2396,6 +2399,10 @@ export class CJSONRenderer extends ConvenienceRenderer {
23962399
minMaxValueForType(
23972400
property.type,
23982401
) ?? [];
2402+
const [minLength, maxLength] =
2403+
minMaxLengthForType(
2404+
property.type,
2405+
) ?? [];
23992406
if (!property.isOptional) {
24002407
this.emitLine(
24012408
`if (!cJSON_HasObjectItem(${object}, "${jsonName}")) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
@@ -2443,6 +2450,14 @@ export class CJSONRenderer extends ConvenienceRenderer {
24432450
`if (${value}->valuedouble > ${maximum}) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
24442451
);
24452452
}
2453+
if (minLength !== undefined)
2454+
this.emitLine(
2455+
`if (strlen(${value}->valuestring) < ${minLength}) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
2456+
);
2457+
if (maxLength !== undefined)
2458+
this.emitLine(
2459+
`if (strlen(${value}->valuestring) > ${maxLength}) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
2460+
);
24462461
if (
24472462
cJSON.cjsonType ===
24482463
"cJSON_Enum"

‎test/languages.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,6 @@ export const CJSONLanguage: Language = {
608608
"multi-type-enum.schema",
609609
"prefix-items.schema",
610610
/* Constraints (min/max and regex) are not supported (for the current implementation, can be added later, should abord parsing and return NULL) */
611-
"minmaxlength.schema",
612611
"schema-constraints.schema",
613612
"optional-const-ref.schema",
614613
/* Same unsupported min/max, length and regex constraints, applied to optional properties */

0 commit comments

Comments
 (0)