Add plant config validation

main
trivernis 1 year ago
parent 7745f13713
commit 7d10404744
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

17
package-lock.json generated

@ -7,6 +7,10 @@
"": { "": {
"name": "plantwiki-svelte", "name": "plantwiki-svelte",
"version": "0.0.1", "version": "0.0.1",
"dependencies": {
"fp-ts": "^2.16.1",
"io-ts": "^2.2.20"
},
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-node": "^1.3.1", "@sveltejs/adapter-node": "^1.3.1",
@ -1287,6 +1291,11 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/fp-ts": {
"version": "2.16.1",
"resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-2.16.1.tgz",
"integrity": "sha512-by7U5W8dkIzcvDofUcO42yl9JbnHTEDBrzu3pt5fKT+Z4Oy85I21K80EYJYdjQGC2qum4Vo55Ag57iiIK4FYuA=="
},
"node_modules/fs-constants": { "node_modules/fs-constants": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
@ -1476,6 +1485,14 @@
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==",
"dev": true "dev": true
}, },
"node_modules/io-ts": {
"version": "2.2.20",
"resolved": "https://registry.npmjs.org/io-ts/-/io-ts-2.2.20.tgz",
"integrity": "sha512-Rq2BsYmtwS5vVttie4rqrOCIfHCS9TgpRLFpKQCM1wZBBRY9nWVGmEvm2FnDbSE2un1UE39DvFpTR5UL47YDcA==",
"peerDependencies": {
"fp-ts": "^2.5.0"
}
},
"node_modules/is-arrayish": { "node_modules/is-arrayish": {
"version": "0.3.2", "version": "0.3.2",
"resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz",

@ -23,5 +23,9 @@
"vite": "^4.4.2", "vite": "^4.4.2",
"vite-plugin-toml": "^0.6.0" "vite-plugin-toml": "^0.6.0"
}, },
"type": "module" "type": "module",
"dependencies": {
"fp-ts": "^2.16.1",
"io-ts": "^2.2.20"
}
} }

@ -4,7 +4,7 @@ bin_name = "Monstera Deliciosa"
[image] [image]
remote = "https://upload.wikimedia.org/wikipedia/commons/d/d4/New_Monstera_Deliciosa_Leaf.jpg" remote = "https://upload.wikimedia.org/wikipedia/commons/d/d4/New_Monstera_Deliciosa_Leaf.jpg"
format = "json" format = "jpeg"
width = 1512 width = 1512
source = "https://commons.wikimedia.org/wiki/File:New_Monstera_Deliciosa_Leaf.jpg" source = "https://commons.wikimedia.org/wiki/File:New_Monstera_Deliciosa_Leaf.jpg"
alt = """ alt = """

@ -1,43 +1,54 @@
import aloeVera from "$lib/assets/plants/aloe-vera.toml"; import aloeVera from "$lib/assets/plants/aloe-vera.toml";
import monsteraDeliciosa from "$lib/assets/plants/monstera-deliciosa.toml"; import monsteraDeliciosa from "$lib/assets/plants/monstera-deliciosa.toml";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
import { PathReporter } from "io-ts/lib/PathReporter";
export type PlantData = { const PlantDataTS = t.type({
slug: string; slug: t.string,
name: string; name: t.string,
bin_name: string; bin_name: t.string,
image: { image: t.type({
local?: string; local: t.union([t.string, t.undefined]),
remote?: string; remote: t.union([t.string, t.undefined]),
format?: string; format: t.union([t.string, t.undefined]),
width?: number; width: t.union([t.number, t.undefined]),
source: string; source: t.string,
alt: string; alt: t.string,
}; }),
temp: { temp: t.type({
death: number; death: t.number,
lower: number; lower: t.number,
upper: number; upper: t.number,
}; }),
care: { care: t.type({
description: string; description: t.string,
water_schedule: string; water_schedule: t.string,
mist_schedule?: string; mist_schedule: t.union([t.string, t.undefined]),
fertilize_schedule?: string; fertilize_schedule: t.union([t.string, t.undefined]),
clean_schedule?: string; clean_schedule: t.union([t.string, t.undefined]),
}; }),
site: { site: t.type({
description: string; description: t.string,
light: string; light: t.string,
humidity?: string; humidity: t.union([t.string, t.undefined]),
}; }),
}; });
export type PlantData = t.TypeOf<typeof PlantDataTS>;
type PlantMap = { [key: PlantData["slug"]]: PlantData }; type PlantMap = { [key: PlantData["slug"]]: PlantData };
export const plants: PlantMap = [aloeVera, monsteraDeliciosa].reduce( export const plants: PlantMap = [aloeVera, monsteraDeliciosa].reduce(
(acc, cur) => { (acc, cur) => {
acc[cur.slug] = cur; const decoded = PlantDataTS.decode(cur);
if (isLeft(decoded)) {
throw Error(
`Invalid plant data ${PathReporter.report(decoded).join("\n")}`,
);
}
acc[cur.slug] = decoded.right;
return acc; return acc;
}, },
{} as PlantMap, {} as PlantMap,

@ -0,0 +1 @@
export const prerender = true;

@ -2,6 +2,8 @@ import { error } from "@sveltejs/kit";
import type { PageLoad } from "./$types"; import type { PageLoad } from "./$types";
import { plants, type PlantData } from "$lib/plants"; import { plants, type PlantData } from "$lib/plants";
export const prerender = "auto";
export const load: PageLoad = ({ params }) => { export const load: PageLoad = ({ params }) => {
if (plants[params.plant]) { if (plants[params.plant]) {
return plants[params.plant] as PlantData; return plants[params.plant] as PlantData;

Loading…
Cancel
Save