Skip to content

Commit bc53b0d

Browse files
committed
move a few types to zod validator
1 parent 571f100 commit bc53b0d

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

src/command/render/pandoc.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,6 @@ import {
197197
MarkdownPipelineHandler,
198198
} from "../../core/markdown-pipeline.ts";
199199
import { getenv } from "../../core/env.ts";
200-
import {
201-
BrandFontBunny,
202-
BrandFontFile,
203-
BrandFontGoogle,
204-
} from "../../resources/types/schema-types.ts";
205200
import { Zod } from "../../resources/types/zod/schema-types.ts";
206201
import { kFieldCategories } from "../../project/types/website/listing/website-listing-shared.ts";
207202
import { isWindows } from "../../deno_ral/platform.ts";
@@ -1487,13 +1482,13 @@ async function resolveExtras(
14871482
fontdirs.add(dirname(join(brand.brandDir, path)));
14881483
}
14891484
} else if (source === "bunny") {
1490-
const font = _font as BrandFontBunny;
1485+
const font = Zod.BrandFontBunny.parse(_font);
14911486
console.log(
14921487
"Font bunny is not yet supported for Typst, skipping",
14931488
font.family,
14941489
);
14951490
} else if (source === "google" /* || font.source === "bunny" */) {
1496-
const font = _font as BrandFontGoogle;
1491+
const font = Zod.BrandFontGoogle.parse(_font);
14971492
let { family, style, weight } = font;
14981493
const parts = [family!];
14991494
if (style) {

src/core/brand/brand.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
*/
88

99
import {
10-
Brand as BrandJson,
10+
// Brand as BrandJson,
1111
BrandFont,
12-
BrandLogoExplicitResource,
1312
BrandNamedThemeColor,
1413
BrandTypography,
1514
BrandTypographyOptionsBase,
1615
BrandTypographyOptionsHeadings,
1716
} from "../../resources/types/schema-types.ts";
17+
18+
import {
19+
Brand as BrandJson,
20+
BrandLogoExplicitResource,
21+
Zod,
22+
} from "../../resources/types/zod/schema-types.ts";
1823
import { InternalError } from "../lib/error.ts";
1924

2025
import { join, relative } from "../../deno_ral/path.ts";
@@ -68,7 +73,7 @@ export class Brand {
6873
processedData: ProcessedBrandData;
6974

7075
constructor(readonly brand: BrandJson, brandDir: string, projectDir: string) {
71-
this.data = brand;
76+
this.data = Zod.Brand.parse(brand);
7277
this.brandDir = brandDir;
7378
this.projectDir = projectDir;
7479
this.processedData = this.processData(brand);
@@ -261,8 +266,10 @@ export class Brand {
261266
if (typeof entry === "string") {
262267
return { path: join(pathPrefix, entry) };
263268
}
264-
entry.path = join(pathPrefix, entry.path);
265-
return entry;
269+
return {
270+
...entry,
271+
path: join(pathPrefix, entry.path),
272+
};
266273
}
267274

268275
getLogo(name: "small" | "medium" | "large"): CanonicalLogoInfo | undefined {

src/core/schema/zod-types-from-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ ${zodObject}
407407
baseObj = `${baseObj}.passthrough()`;
408408
}
409409
if (Array.isArray(required)) {
410-
baseObj = `${baseObj}.required({${
410+
baseObj = `${baseObj}.partial().required({${
411411
required.map((key: string) => {
412412
return `${key}: true`;
413413
}).join(", ")

src/resources/types/zod/schema-types.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export type JsonObject = { [key: string]: unknown };
2424

2525
export const ZodDate = z.union([
2626
z.string(),
27-
z.object({ value: z.string(), format: z.string() }).passthrough().required({
28-
value: true,
29-
}),
27+
z.object({ value: z.string(), format: z.string() }).passthrough().partial()
28+
.required({ value: true }),
3029
]);
3130

3231
export const ZodMathMethods = z.enum(
@@ -40,9 +39,8 @@ export const ZodPandocFormatOutputFile = z.union([z.string(), z.literal(null)]);
4039
export const ZodPandocFormatFilters = z.array(
4140
z.union([
4241
z.string(),
43-
z.object({ type: z.string(), path: z.string() }).passthrough().required({
44-
path: true,
45-
}),
42+
z.object({ type: z.string(), path: z.string() }).passthrough().partial()
43+
.required({ path: true }),
4644
z.object({
4745
type: z.string(),
4846
path: z.string(),
@@ -56,7 +54,7 @@ export const ZodPandocFormatFilters = z.array(
5654
"post-render",
5755
] as const,
5856
),
59-
}).passthrough().required({ path: true, at: true }),
57+
}).passthrough().partial().required({ path: true, at: true }),
6058
z.object({ type: z.enum(["citeproc"] as const) }).strict(),
6159
]),
6260
);
@@ -133,7 +131,7 @@ export const ZodGiscusConfiguration = z.object({
133131
}).strict().partial(),
134132
]),
135133
language: z.string(),
136-
}).strict().required({ repo: true });
134+
}).strict().partial().required({ repo: true });
137135

138136
export const ZodDocumentCommentsConfiguration = z.union([
139137
z.literal(false),
@@ -143,7 +141,7 @@ export const ZodDocumentCommentsConfiguration = z.union([
143141
label: z.string(),
144142
theme: z.string(),
145143
"issue-term": z.string(),
146-
}).strict().required({ repo: true }),
144+
}).strict().partial().required({ repo: true }),
147145
giscus: z.lazy(() => ZodGiscusConfiguration),
148146
hypothesis: z.union([
149147
z.boolean(),
@@ -169,7 +167,7 @@ export const ZodDocumentCommentsConfiguration = z.union([
169167
z.array(z.string()),
170168
]),
171169
icon: z.string(),
172-
}).passthrough().required({
170+
}).passthrough().partial().required({
173171
apiUrl: true,
174172
authority: true,
175173
grantToken: true,
@@ -189,7 +187,7 @@ export const ZodDocumentCommentsConfiguration = z.union([
189187
userid: z.string(),
190188
displayName: z.string(),
191189
}).passthrough().partial(),
192-
}).passthrough().required({ user: true }),
190+
}).passthrough().partial().required({ user: true }),
193191
requestConfigFromFrame: z.object({
194192
origin: z.string(),
195193
ancestorLevel: z.number(),
@@ -232,7 +230,7 @@ export const ZodProjectServe = z.object({
232230
args: z.string(),
233231
env: z.object({}).passthrough().partial(),
234232
ready: z.string(),
235-
}).strict().required({ cmd: true, ready: true });
233+
}).strict().partial().required({ cmd: true, ready: true });
236234

237235
export const ZodPublish = z.object({
238236
netlify: z.array(z.lazy(() => ZodPublishRecord)),
@@ -650,7 +648,7 @@ export const ZodChapterItem = z.union([
650648
z.object({
651649
part: z.string(),
652650
chapters: z.array(z.lazy(() => ZodNavigationItem)),
653-
}).passthrough().required({ part: true }),
651+
}).passthrough().partial().required({ part: true }),
654652
]);
655653

656654
export const ZodChapterList = z.array(z.lazy(() => ZodChapterItem));
@@ -662,7 +660,7 @@ export const ZodOtherLinks = z.array(
662660
icon: z.string(),
663661
rel: z.string(),
664662
target: z.string(),
665-
}).passthrough().required({ text: true, href: true }),
663+
}).passthrough().partial().required({ text: true, href: true }),
666664
);
667665

668666
export const ZodCrossrefLabelsSchema = z.string();
@@ -756,7 +754,7 @@ export const ZodWebsiteAbout = z.object({
756754
"image-width": z.string(),
757755
"image-shape": z.enum(["rectangle", "round", "rounded"] as const),
758756
links: z.array(z.lazy(() => ZodNavigationItem)),
759-
}).strict().required({ template: true });
757+
}).strict().partial().required({ template: true });
760758

761759
export const ZodWebsiteListing = z.object({
762760
id: z.string(),
@@ -1224,9 +1222,8 @@ export const ZodSemver = z.string().regex(
12241222

12251223
export const ZodQuartoDate = z.union([
12261224
z.string(),
1227-
z.object({ format: z.string(), value: z.string() }).strict().required({
1228-
value: true,
1229-
}),
1225+
z.object({ format: z.string(), value: z.string() }).strict().partial()
1226+
.required({ value: true }),
12301227
]);
12311228

12321229
export const ZodProjectProfile = z.object({
@@ -1248,7 +1245,7 @@ export const ZodNotebookViewSchema = z.object({
12481245
title: z.union([z.string(), z.boolean()]),
12491246
url: z.string(),
12501247
"download-url": z.string(),
1251-
}).passthrough().required({ notebook: true });
1248+
}).passthrough().partial().required({ notebook: true });
12521249

12531250
export const ZodCodeLinksSchema = z.union([
12541251
z.boolean(),
@@ -1317,7 +1314,7 @@ export const ZodBrandStringLightDark = z.union([
13171314
export const ZodBrandLogoExplicitResource = z.object({
13181315
path: z.string(),
13191316
alt: z.string(),
1320-
}).strict().required({ path: true });
1317+
}).strict().partial().required({ path: true });
13211318

13221319
export const ZodBrandLogoResource = z.union([
13231320
z.string(),
@@ -1564,10 +1561,10 @@ export const ZodBrandFontFile = z.object({
15641561
path: z.string(),
15651562
weight: z.lazy(() => ZodBrandFontWeight),
15661563
style: z.lazy(() => ZodBrandFontStyle),
1567-
}).passthrough().required({ path: true }),
1564+
}).passthrough().partial().required({ path: true }),
15681565
]),
15691566
),
1570-
}).strict().required({ files: true, family: true, source: true });
1567+
}).strict().partial().required({ files: true, family: true, source: true });
15711568

15721569
export const ZodBrandFontFamily = z.string();
15731570

0 commit comments

Comments
 (0)