Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { arrayIntercalate } from "collection-utils";

import { minMaxItemsForType } from "../../attributes/Constraints.js";
import {
minMaxItemsForType,
minMaxLengthForType,
patternForType,
} from "../../attributes/Constraints.js";
import { ConvenienceRenderer } from "../../ConvenienceRenderer.js";
import { type Name, type Namer, funPrefixNamer } from "../../Naming.js";
import type { RenderContext } from "../../Renderer.js";
Expand Down Expand Up @@ -99,6 +103,28 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer {
return p.isOptional ? [typeMap, ".optional()"] : typeMap;
}

protected renderString(type: Type, base: Sourcelike): Sourcelike {
const constraints: Sourcelike[] = [base];
const [minimumLength, maximumLength] = minMaxLengthForType(type) ?? [];
const pattern = patternForType(type);

if (minimumLength !== undefined) {
constraints.push(".min(", minimumLength.toString(10), ")");
}
if (maximumLength !== undefined) {
constraints.push(".max(", maximumLength.toString(10), ")");
}
if (pattern !== undefined) {
constraints.push(
".regex(new RegExp(",
JSON.stringify(pattern),
"))",
);
}

return constraints;
}

protected typeMapTypeFor(t: Type, required = true): Sourcelike {
if (["class", "object", "enum"].includes(t.kind)) {
return [this.nameForNamedType(t), "Schema"];
Expand All @@ -111,7 +137,7 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer {
(_boolType) => "z.boolean()",
(_integerType) => "z.number()",
(_doubleType) => "z.number()",
(_stringType) => "z.string()",
(stringType) => this.renderString(stringType, "z.string()"),
(arrayType) => {
const [minItems, maxItems] =
minMaxItemsForType(arrayType) ?? [];
Expand Down
1 change: 1 addition & 0 deletions test/inputs/schema/minmaxlength.1.fail.minmaxlength.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"minlength": "aa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
Expand Down
1 change: 1 addition & 0 deletions test/inputs/schema/minmaxlength.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
Expand Down
11 changes: 11 additions & 0 deletions test/inputs/schema/minmaxlength.2.fail.minmaxlength.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "",
"union": "dd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
"intersection": "ffff",
"minMaxIntersection": "gggg"
}
11 changes: 11 additions & 0 deletions test/inputs/schema/minmaxlength.3.fail.minmaxlength.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
"intersection": "fff",
"minMaxIntersection": "gggg"
}
11 changes: 11 additions & 0 deletions test/inputs/schema/minmaxlength.4.fail.minmaxlength.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
"intersection": "ffff",
"minMaxIntersection": "gggggg"
}
11 changes: 11 additions & 0 deletions test/inputs/schema/minmaxlength.5.fail.minmaxlength.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "cccc",
"emptyOnly": "x",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
"intersection": "ffff",
"minMaxIntersection": "gggg"
}
11 changes: 11 additions & 0 deletions test/inputs/schema/minmaxlength.6.fail.pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"minlength": "aaaa",
"maxlength": "bbb",
"minmaxlength": "1234",
"emptyOnly": "",
"union": "dddddd",
"inUnion": "cccc",
"minMaxUnion": "eeeee",
"intersection": "ffff",
"minMaxIntersection": "gggg"
}
8 changes: 7 additions & 1 deletion test/inputs/schema/minmaxlength.schema
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
"minmaxlength": {
"type": "string",
"minLength": 3,
"maxLength": 5
"maxLength": 5,
"pattern": "^[a-z]+$"
},
"emptyOnly": {
"type": "string",
"maxLength": 0
},
"union": {
"oneOf": [
Expand Down Expand Up @@ -71,6 +76,7 @@
"minlength",
"maxlength",
"minmaxlength",
"emptyOnly",
"union",
"inUnion",
"minMaxUnion",
Expand Down
2 changes: 1 addition & 1 deletion test/inputs/schema/optional-constraints.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"reqZeroMin": 0,
"optInt": 0,
"optDouble": 0.5,
"optString": "abc",
"optString": "abcdefghij",
"optPattern": "abc"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"reqZeroMin": 0,
"optString": "abcdefghijk"
}
1 change: 1 addition & 0 deletions test/inputs/schema/pattern.1.fail.pattern.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"pattern1": "qqq",
"pattern2": "ba",
"escaped": "123.abc",
"union": "ccc"
}
1 change: 1 addition & 0 deletions test/inputs/schema/pattern.1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"pattern1": "aa",
"pattern2": "ba",
"escaped": "123.abc",
"union": "ccc"
}
6 changes: 6 additions & 0 deletions test/inputs/schema/pattern.2.fail.pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pattern1": "aa",
"pattern2": "ba",
"escaped": "123.abc",
"union": "qqq"
}
6 changes: 6 additions & 0 deletions test/inputs/schema/pattern.3.fail.pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pattern1": "aa",
"pattern2": "ba",
"escaped": "abc.def",
"union": "ccc"
}
6 changes: 5 additions & 1 deletion test/inputs/schema/pattern.schema
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"type": "string",
"pattern": "b[.]*"
},
"escaped": {
"type": "string",
"pattern": "^\\d+\\.[a-z]+$"
},
"union": {
"oneOf": [
{
Expand All @@ -21,5 +25,5 @@
]
}
},
"required": ["pattern1", "pattern2", "union"]
"required": ["pattern1", "pattern2", "escaped", "union"]
}
10 changes: 9 additions & 1 deletion test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,15 @@ export const TypeScriptZodLanguage: Language = {
"e8b04.json",
],
allowMissingNull: false,
features: ["enum", "union", "no-defaults", "date-time", "minmaxitems"],
features: [
"enum",
"union",
"no-defaults",
"date-time",
"minmaxitems",
"minmaxlength",
"pattern",
],
output: "TopLevel.ts",
topLevel: "TopLevel",
skipJSON: [
Expand Down