Skip to content

Commit 84d53f6

Browse files
committed
fix(ruby): validate string lengths
1 parent 7cc8d8a commit 84d53f6

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

packages/quicktype-core/src/language/Ruby/RubyRenderer.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ConvenienceRenderer,
33
type ForbiddenWordsInfo,
44
} from "../../ConvenienceRenderer.js";
5+
import { minMaxLengthForType } from "../../attributes/Constraints.js";
56
import { type Name, Namer } from "../../Naming.js";
67
import type { RenderContext } from "../../Renderer.js";
78
import type { OptionValues } from "../../RendererOptions/index.js";
@@ -103,14 +104,24 @@ export class RubyRenderer extends ConvenienceRenderer {
103104

104105
private dryType(t: Type, isOptional = false): Sourcelike {
105106
const optional = isOptional ? ".optional" : "";
107+
const length = (type: Type): string => {
108+
const [min, max] = minMaxLengthForType(type) ?? [];
109+
const constraints = [
110+
min === undefined ? undefined : `min_size: ${min}`,
111+
max === undefined ? undefined : `max_size: ${max}`,
112+
].filter((x): x is string => x !== undefined);
113+
return constraints.length === 0
114+
? ""
115+
: `.constrained(${constraints.join(", ")})`;
116+
};
106117
return matchType<Sourcelike>(
107118
t,
108119
(_anyType) => ["Types::Any", optional],
109120
(_nullType) => ["Types::Nil", optional],
110121
(_boolType) => ["Types::Bool", optional],
111122
(_integerType) => ["Types::Integer", optional],
112123
(_doubleType) => ["Types::Double", optional],
113-
(_stringType) => ["Types::String", optional],
124+
(stringType) => ["Types::String", length(stringType), optional],
114125
(arrayType) => [
115126
"Types.Array(",
116127
this.dryType(arrayType.items),

test/languages.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,14 @@ export const RubyLanguage: Language = {
464464
"e8b04.json",
465465
],
466466
allowMissingNull: true,
467-
features: ["enum", "union", "no-defaults", "integer", "strict-optional"],
467+
features: [
468+
"enum",
469+
"union",
470+
"no-defaults",
471+
"integer",
472+
"minmaxlength",
473+
"strict-optional",
474+
],
468475
output: "TopLevel.rb",
469476
topLevel: "TopLevel",
470477
skipJSON: [],

0 commit comments

Comments
 (0)