Skip to content

Commit 7e6b0ca

Browse files
committed
demo standard schema impl
1 parent 3c214ce commit 7e6b0ca

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@
5757
},
5858
"peerDependencies": {
5959
"solid-js": "^1.8.6"
60+
},
61+
"dependencies": {
62+
"@standard-schema/spec": "^1.0.0"
6063
}
6164
}

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/data/query.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ import {
1010
import { getRequestEvent, isServer } from "solid-js/web";
1111
import { useNavigate, getIntent, getInPreloadFn } from "../routing.js";
1212
import type { CacheEntry, NarrowResponse } from "../types.js";
13+
import type { StandardSchemaV1 } from "@standard-schema/spec";
14+
15+
// from https://github.com/standard-schema/standard-schema/#how-do-i-accept-standard-schemas-in-my-library
16+
async function standardValidate<T extends StandardSchemaV1>(
17+
schema: T,
18+
input: StandardSchemaV1.InferInput<T>
19+
): Promise<StandardSchemaV1.InferOutput<T>> {
20+
let result = schema["~standard"].validate(input);
21+
if (result instanceof Promise) result = await result;
22+
23+
// if the `issues` field exists, the validation failed
24+
if (result.issues) {
25+
throw new Error(JSON.stringify(result.issues, null, 2));
26+
}
27+
28+
return result.value;
29+
}
1330

1431
const LocationHeader = "Location";
1532
const PRELOAD_TIMEOUT = 5000;
@@ -70,10 +87,16 @@ export type CachedFunction<T extends (...args: any) => any> = T extends (
7087
}
7188
: never;
7289

73-
export function query<T extends (...args: any) => any>(fn: T, name: string): CachedFunction<T> {
90+
export function query<T extends (...args: any) => any>(
91+
fn: T,
92+
name: string,
93+
opts: {
94+
schemas: StandardSchemaV1[];
95+
}
96+
): CachedFunction<T> {
7497
// prioritize GET for server functions
7598
if ((fn as any).GET) fn = (fn as any).GET;
76-
const cachedFn = ((...args: Parameters<T>) => {
99+
const cachedFn = async (...args: Parameters<T>) => {
77100
const cache = getCache();
78101
const intent = getIntent();
79102
const inPreloadFn = getInPreloadFn();
@@ -133,6 +156,9 @@ export function query<T extends (...args: any) => any>(fn: T, name: string): Cac
133156
res = sharedConfig.load!(key) // hydrating
134157
// @ts-ignore at least until we add a delete method to sharedConfig
135158
delete globalThis._$HY.r[key];
159+
} else if (opts.schemas.length > 0) {
160+
let validated = await Promise.all(opts.schemas.map((schema, i) => standardValidate(schema, args[i])));
161+
res = fn(...(validated as any));
136162
} else res = fn(...(args as any));
137163

138164
if (cached) {

0 commit comments

Comments
 (0)