Skip to content

Commit

Permalink
Add TypeScript Zod support for JSON Schema string with date-time form…
Browse files Browse the repository at this point in the history
  • Loading branch information
danwsong authored and iamwavecut committed Aug 17, 2023
1 parent 0ade930 commit 6cb7571
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 20 additions & 9 deletions packages/quicktype-core/src/language/TypeScriptZod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { StringTypeMapping } from "TypeBuilder";
import { arrayIntercalate } from "collection-utils";
import { ClassProperty, EnumType, ObjectType, Type } from "../Type";
import { matchType } from "../TypeUtils";
import { funPrefixNamer, Name, Namer } from "../Naming";
import { ConvenienceRenderer } from "../ConvenienceRenderer";
import { Name, Namer, funPrefixNamer } from "../Naming";
import { RenderContext } from "../Renderer";
import { BooleanOption, getOptionValues, Option, OptionValues } from "../RendererOptions";
import { acronymStyle, AcronymStyleOptions } from "../support/Acronyms";
import { BooleanOption, Option, OptionValues, getOptionValues } from "../RendererOptions";
import { Sourcelike } from "../Source";
import { TargetLanguage } from "../TargetLanguage";
import { ClassProperty, EnumType, ObjectType, PrimitiveStringTypeKind, TransformedStringTypeKind, Type } from "../Type";
import { matchType } from "../TypeUtils";
import { AcronymStyleOptions, acronymStyle } from "../support/Acronyms";
import {
allLowerWordStyle,
capitalize,
Expand All @@ -15,11 +19,8 @@ import {
stringEscape,
utf16StringEscape
} from "../support/Strings";
import { TargetLanguage } from "../TargetLanguage";
import { legalizeName } from "./JavaScript";
import { Sourcelike } from "../Source";
import { panic } from "../support/Support";
import { ConvenienceRenderer } from "../ConvenienceRenderer";
import { legalizeName } from "./JavaScript";

export const typeScriptZodOptions = {
justSchema: new BooleanOption("just-schema", "Schema only", false)
Expand All @@ -38,6 +39,13 @@ export class TypeScriptZodTargetLanguage extends TargetLanguage {
super(displayName, names, extension);
}

get stringTypeMapping(): StringTypeMapping {
const mapping: Map<TransformedStringTypeKind, PrimitiveStringTypeKind> = new Map();
const dateTimeType = "date-time";
mapping.set("date-time", dateTimeType);
return mapping;
}

protected makeRenderer(
renderContext: RenderContext,
untypedOptionValues: { [name: string]: any }
Expand Down Expand Up @@ -132,6 +140,9 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer {
return ["z.union([", ...arrayIntercalate(", ", children), "])"];
},
_transformedStringType => {
if (_transformedStringType.kind === "date-time") {
return "z.coerce.date()";
}
return "z.string()";
}
);
Expand Down
6 changes: 4 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ export const TypeScriptZodLanguage: Language = {
"e8b04.json"
],
allowMissingNull: false,
features: ["enum", "union", "no-defaults"],
features: ["enum", "union", "no-defaults", "date-time"],
output: "TopLevel.ts",
topLevel: "TopLevel",
skipJSON: [
Expand Down Expand Up @@ -1485,7 +1485,9 @@ export const TypeScriptZodLanguage: Language = {
"bug427.json",
"nst-test-suite.json",
"keywords.json",
"ed095.json"
"ed095.json",
"7681c.json",
"32d5c.json"
],
skipMiscJSON: false,
skipSchema: [
Expand Down

0 comments on commit 6cb7571

Please sign in to comment.