Skip to content

Commit 0f7fb28

Browse files
committed
more validation fixes
1 parent 38c87cc commit 0f7fb28

File tree

10 files changed

+75
-61
lines changed

10 files changed

+75
-61
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"root": true,
33
"env": { "browser": true, "es2020": true, "node": true },
4-
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-requiring-type-checking", "plugin:react-hooks/recommended"],
4+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:react-hooks/recommended"],
55
"ignorePatterns": ["dist", "package.json", "yarn.lock", "src/styles", "src/router.ts"],
66
"parser": "@typescript-eslint/parser",
77
"parserOptions": {
@@ -13,5 +13,6 @@
1313
"@typescript-eslint/no-non-null-assertion": "off",
1414
"@typescript-eslint/ban-ts-comment": "off",
1515
"@typescript-eslint/no-unused-vars": "warn",
16+
"@typescript-eslint/no-explicit-any": "warn",
1617
},
1718
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"react-dom": "^18.2.0",
3030
"react-router-dom": "^6.21.3",
3131
"slugify": "^1.6.6",
32-
"valibot": "^0.27.1"
32+
"valibot": "^0.30.0"
3333
},
3434
"devDependencies": {
3535
"@pandacss/dev": "^0.17.5",

src/components/context/storage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ const reducer: Reducer<State, Actions> = (state, action) => {
4040
};
4141

4242
const storage = Object.entries(localStorage).reduce((record: State, [key, value]: [string, string]) => {
43-
if (!is(schemas.dataset, value)) return record;
4443
return { ...record, [key]: JSON.parse(value) as T };
4544
}, {});
4645

@@ -51,6 +50,7 @@ function StorageProvider({ children }: PropsWithChildren) {
5150
useEffect(() => {
5251
Object.entries(datasets).forEach(([key, contents]) => {
5352
const name = key.split("/")[key.split("/").length - 1].split(".")[0];
53+
if (!is(schemas.dataset, contents.default)) return;
5454
dispatch({ type: "UPDATE", payload: { id: name, dataset: contents.default, overwrite: false } });
5555
});
5656
}, []);

src/hooks/context.hooks.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { StorageContext } from "$/components/context/storage";
2-
import { useContext } from "react";
2+
import { schemas } from "$/types";
3+
import { useContext, useMemo } from "react";
4+
import { is } from "valibot";
35

46
export function useDatasets() {
57
const { state, dispatch } = useContext(StorageContext);
6-
return { state, dispatch };
8+
const valid = useMemo(() => {
9+
const filtered = Object.entries(state).filter(([_, contents]) => is(schemas.dataset, contents));
10+
return filtered.reduce((acc, [key, contents]) => ({ ...acc, [key]: contents }), {});
11+
}, [state]);
12+
return { state: valid, dispatch };
713
}
814
export function useDataset(key: string) {
915
const { state, dispatch } = useContext(StorageContext);

src/templates/form/level.tsx

+35-35
Large diffs are not rendered by default.

src/templates/form/template.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { css, cva } from "$/styles/css";
22
import { vstack } from "$/styles/patterns";
3-
import { Fragment, PropsWithChildren } from "react";
3+
import { FormHTMLAttributes, Fragment, PropsWithChildren } from "react";
44

55
interface Props {
66
title?: string;
77
}
88

9-
function Template({ title, children }: PropsWithChildren<Props>) {
9+
function Template({ title, children, ...rest }: PropsWithChildren<Props> & FormHTMLAttributes<HTMLElement>) {
1010
return (
1111
<Fragment>
1212
<h2 className={styles.title}>{title}</h2>
1313
<hr />
14-
<div className={styles.contents}>{children}</div>
14+
<form className={styles.contents} {...rest}>
15+
{children}
16+
</form>
1517
</Fragment>
1618
);
1719
}

src/types/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ElementType } from "react";
2+
23
import { default as beatmap } from "./beatmap";
34
import { default as data } from "./dataset";
45
import { default as artificial } from "./input";
@@ -8,8 +9,10 @@ export * from "./beatmap";
89
export * from "./dataset";
910
export * from "./shared";
1011

11-
export type AsChildProps<T extends ElementType> = React.ComponentPropsWithoutRef<T> & { asChild?: boolean };
12+
export type Predicate<T, Result> = (value: T, index: number, array: T[]) => Result;
1213
export type Entry<T> = { name: string; contents: T };
14+
15+
export type AsChildProps<T extends ElementType> = React.ComponentPropsWithoutRef<T> & { asChild?: boolean };
1316
export type PayloadAction<T, K = string> = { type: K; payload: T };
1417

1518
export const schemas = { artificial, ...beatmap, ...data, ...shared };

src/utils/array.utils.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1+
import { Predicate } from "$/types";
2+
13
export const predicates = {
24
unique: <T>(value: T, index: number, array: T[]) => array.indexOf(value) === index,
3-
};
4-
5-
export function pick<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
6-
const draft = { ...obj };
7-
keys.forEach((key) => (draft[key] = obj[key]));
8-
return draft;
9-
}
10-
export function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K> {
11-
const draft = { ...obj };
12-
keys.forEach((key) => delete draft[key]);
13-
return draft;
14-
}
5+
} satisfies Record<string, Predicate<unknown, unknown>>;

src/utils/object.utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
export function pick<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
2+
const draft = { ...obj };
3+
keys.forEach((key) => (draft[key] = obj[key]));
4+
return draft;
5+
}
6+
export function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K> {
7+
const draft = { ...obj };
8+
keys.forEach((key) => delete draft[key]);
9+
return draft;
10+
}
11+
112
export function common<T extends Record<string, unknown>>(a: T, b: T): T {
213
const result = Object.keys(a).reduce((result: T, key: keyof typeof a) => {
314
const value = a[key];

yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -3527,7 +3527,7 @@ __metadata:
35273527
react-router-dom: "npm:^6.21.3"
35283528
slugify: "npm:^1.6.6"
35293529
typescript: "npm:^5.3.3"
3530-
valibot: "npm:^0.27.1"
3530+
valibot: "npm:^0.30.0"
35313531
vite: "npm:^5.0.12"
35323532
vitest: "npm:^1.2.2"
35333533
languageName: unknown
@@ -8782,10 +8782,10 @@ __metadata:
87828782
languageName: node
87838783
linkType: hard
87848784

8785-
"valibot@npm:^0.27.1":
8786-
version: 0.27.1
8787-
resolution: "valibot@npm:0.27.1"
8788-
checksum: 9def52a9069c691eb74d48db91c2ce8d5e8094483fd1dd396753584be6e0e7d1148de3d2cd94ef0b2267e28bb49752704e1bcdafb345557294766d0c211918b5
8785+
"valibot@npm:^0.30.0":
8786+
version: 0.30.0
8787+
resolution: "valibot@npm:0.30.0"
8788+
checksum: 59a1d4201459dfc7b173a8e2cc1490d03115cf8f2140e9aa176fb1945131e1b37c499acab5e2d6c60684f78995792e24dd5adb87b8e7946bfec3a0c90529f091
87898789
languageName: node
87908790
linkType: hard
87918791

0 commit comments

Comments
 (0)