Skip to content

Commit 220805d

Browse files
committed
fix(php): validate string lengths
1 parent f24a393 commit 220805d

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎packages/quicktype-core/src/language/Php/PhpRenderer.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../../ConvenienceRenderer.js";
1111
import {
1212
minMaxItemsForType,
13+
minMaxLengthForType,
1314
minMaxValueForType,
1415
} from "../../attributes/Constraints.js";
1516
import {
@@ -1085,8 +1086,19 @@ export class PhpRenderer extends ConvenienceRenderer {
10851086
}
10861087
validateRange(doubleType);
10871088
},
1088-
(_stringType) => {
1089+
(stringType) => {
10891090
if (!skipPrimitiveTypeCheck) is("is_string");
1091+
const invalid = () =>
1092+
this.emitLine('throw new Exception("Attribute Error");');
1093+
const validateLength = (operator: string, bound: number) => {
1094+
this.emitBlock(
1095+
`if (preg_match_all('/./us', ${scopeAttrName}) ${operator} ${bound})`,
1096+
invalid,
1097+
);
1098+
};
1099+
const [min, max] = minMaxLengthForType(stringType) ?? [];
1100+
if (min !== undefined) validateLength("<", min);
1101+
if (max !== undefined) validateLength(">", max);
10901102
},
10911103
(arrayType) => {
10921104
is("is_array");

‎test/languages.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,7 @@ export const PHPLanguage: Language = {
15991599
"minmax",
16001600
"minmaxitems",
16011601
"date-time",
1602+
"minmaxlength",
16021603
],
16031604
output: "TopLevel.php",
16041605
topLevel: "TopLevel",

0 commit comments

Comments
 (0)