Skip to content

Commit b10df2f

Browse files
authored
Merge pull request #3326 from glideapps/fix/cjson-number-constraints
cJSON: validate numeric constraints
2 parents 38c05d9 + f178de7 commit b10df2f

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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";
89
import { enumCaseValues } from "../../attributes/EnumValues.js";
910
import {
1011
ConvenienceRenderer,
@@ -2391,6 +2392,10 @@ export class CJSONRenderer extends ConvenienceRenderer {
23912392
);
23922393
const object = `j${level > 0 ? level.toString() : ""}`;
23932394
const value = `cJSON_GetObjectItemCaseSensitive(${object}, "${jsonName}")`;
2395+
const [minimum, maximum] =
2396+
minMaxValueForType(
2397+
property.type,
2398+
) ?? [];
23942399
if (!property.isOptional) {
23952400
this.emitLine(
23962401
`if (!cJSON_HasObjectItem(${object}, "${jsonName}")) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
@@ -2428,6 +2433,16 @@ export class CJSONRenderer extends ConvenienceRenderer {
24282433
`if (!${this.sourcelikeToString(cJSON.isType)}(${value})) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
24292434
);
24302435
}
2436+
if (minimum !== undefined) {
2437+
this.emitLine(
2438+
`if (${value}->valuedouble < ${minimum}) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
2439+
);
2440+
}
2441+
if (maximum !== undefined) {
2442+
this.emitLine(
2443+
`if (${value}->valuedouble > ${maximum}) { cJSON_Delete${this.sourcelikeToString(className)}(x); return NULL; }`,
2444+
);
2445+
}
24312446
if (
24322447
cJSON.cjsonType ===
24332448
"cJSON_Enum"

test/languages.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ export const CJSONLanguage: Language = {
569569
allowMissingNull: false,
570570
features: [
571571
"minmax",
572+
"minmaxInteger",
572573
"minmaxlength",
573574
"pattern",
574575
"enum",
@@ -595,7 +596,9 @@ export const CJSONLanguage: Language = {
595596
/* Enum as TopLevel is not supported */
596597
"top-level-enum.schema",
597598
/* Union with Number and Integer are not supported; min/max constraints on numbers rely on the same distinction */
598-
...skipsIntFloatUnions,
599+
...skipsIntFloatUnions.filter(
600+
(schema) => schema !== "minmax-integer.schema",
601+
),
599602
/* Union, Map and Arrays with invalid types are not checked (for the current implementation, can be added later, should abord parsing and return NULL) */
600603
...skipsMapValueValidation.filter(
601604
(schema) => schema !== "go-schema-pattern-properties.schema",

0 commit comments

Comments
 (0)