Skip to content

Commit

Permalink
feat(scrape): validate against quote record schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Dec 15, 2024
1 parent 40e4516 commit 01192f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/content.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { docsSchema } from "@astrojs/starlight/schema";
import { glob } from "astro/loaders";
import { defineCollection, z } from "astro:content";
import { defineCollection } from "astro:content";

import { QuoteRecordSchema } from "./data/quoteRecords/_schema";

const docs = defineCollection({
loader:
Expand All @@ -15,7 +17,7 @@ const docs = defineCollection({

const quoteRecords = defineCollection({
loader: glob({ pattern: "[^_]*.json", base: "./src/data/quoteRecords" }),
schema: z.record(z.string(), z.number()),
schema: QuoteRecordSchema,
});

export const collections = {
Expand Down
4 changes: 4 additions & 0 deletions src/data/quoteRecords/_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { z } from "astro/zod";

export const QuoteRecordSchema = z.record(z.string(), z.number());
export type QuoteRecord = z.output<typeof QuoteRecordSchema>;
6 changes: 4 additions & 2 deletions src/data/quoteRecords/_scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as util from "node:util";

import * as XLSX from "xlsx";

import { type QuoteRecord, QuoteRecordSchema } from "./_schema";
import {
currencyPairSymbol,
isCurrency,
Expand Down Expand Up @@ -96,7 +97,7 @@ for (const [currency, rateByDate] of rateByDateByCurrency) {

const prevContents = await file.readFile("utf8");
const quotes = prevContents
? Object.entries<number>(JSON.parse(prevContents))
? Object.entries(QuoteRecordSchema.parse(JSON.parse(prevContents)))
: [];

for (const [date, rate] of rateByDate) {
Expand All @@ -106,7 +107,8 @@ for (const [currency, rateByDate] of rateByDateByCurrency) {

await file.truncate();
await file.write(
JSON.stringify(Object.fromEntries(quotes), null, 2) + "\n",
JSON.stringify(Object.fromEntries(quotes) satisfies QuoteRecord, null, 2) +
"\n",
0,
);
}

0 comments on commit 01192f3

Please sign in to comment.