Skip to content

Commit 909e334

Browse files
doc: use Zod to validate the parsed files
1 parent 46c1d93 commit 909e334

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

README.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
#!/usr/bin/env -S deno run --allow-read --allow-run=bash,git,cargo --allow-env --allow-sys
22

33
import * as zx from 'npm:zx'
4+
import { z, ZodSchema } from "https://deno.land/x/[email protected]/mod.ts";
5+
6+
const CargoToml = z.object({
7+
package: z.object({
8+
name: z.string(),
9+
description: z.string(),
10+
metadata: z.object({
11+
details: z.object({
12+
title: z.string(),
13+
tagline: z.string(),
14+
summary: z.string(),
15+
})
16+
})
17+
})
18+
})
19+
20+
type CargoToml = z.infer<typeof CargoToml>;
421

5-
interface CargoToml {
6-
package: {
7-
name: string,
8-
description: string,
9-
metadata: {
10-
details: {
11-
title: string,
12-
tagline: string,
13-
summary: string
14-
}
15-
}
16-
}
17-
}
18-
19-
interface Repo {
20-
url: string
21-
}
22+
const Repo = z.object({
23+
url: z.string().url(),
24+
})
25+
26+
type Repo = z.infer<typeof Repo>;
2227

2328
const $ = zx.$({
2429
cwd: import.meta.dirname
2530
})
26-
27-
const parse = (input: { toString: () => string }) => JSON.parse(input.toString())
31+
const parse = <T>(schema: ZodSchema<T>, input: { toString: () => string }) => schema.parse(JSON.parse(input.toString()))
2832
const renderMarkdownList = (items: string[]) => items.map(bin => `* ${bin}`).join('\n')
2933

30-
const theCargoToml: CargoToml = parse(await $`yj -t < Cargo.toml`)
34+
const theCargoToml: CargoToml = parse(CargoToml, await $`yj -t < Cargo.toml`)
3135
const { package: {name, description, metadata: {details: {title}}} } = theCargoToml
3236
const bin = name
3337
const help = await $`cargo run --quiet --bin ${bin} -- --help`
34-
const repo: Repo = parse(await $`gh repo view --json url`)
38+
const repo: Repo = parse(Repo, await $`gh repo view --json url`)
3539
const extraBins = (await $`find src/bin/*.rs -type f -exec basename {} .rs \\;`).valueOf().split("\n")
3640

3741
const autogenerated = `

0 commit comments

Comments
 (0)