Skip to content

feat: Add Standard Schema validator to qwik-city #7518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-gorillas-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@builder.io/qwik-city": minor
---

feat: Add Standard Schema validator to qwik-city
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"@node-rs/helper": "1.6.0",
"@octokit/action": "6.1.0",
"@playwright/test": "1.50.1",
"@standard-schema/spec": "1.0.0",
"@types/brotli": "1.3.4",
"@types/bun": "1.1.6",
"@types/cross-spawn": "6.0.6",
Expand Down Expand Up @@ -181,7 +182,7 @@
"vitest": "2.0.5",
"watchlist": "0.3.1",
"which-pm-runs": "1.1.0",
"zod": "3.22.4"
"zod": "3.24.2"
},
"engines": {
"node": ">=16.8.0 <18.0.0 || >=18.11",
Expand Down
34 changes: 31 additions & 3 deletions packages/docs/src/routes/api/qwik-city/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
}
],
"kind": "TypeAlias",
"content": "```typescript\nexport type GetValidatorInputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferInput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z.input<TYPE> : never;\n```\n**References:** [TypedDataValidator](#typeddatavalidator)",
"content": "```typescript\nexport type GetValidatorInputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends StandardSchemaDataValidator<infer TYPE> ? StandardSchemaV1.InferInput<TYPE> : VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferInput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z.input<TYPE> : never;\n```\n**References:** [TypedDataValidator](#typeddatavalidator)",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts",
"mdFile": "qwik-city.getvalidatorinputtype.md"
},
Expand All @@ -306,7 +306,7 @@
}
],
"kind": "TypeAlias",
"content": "```typescript\nexport type GetValidatorOutputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferOutput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z.output<TYPE> : never;\n```\n**References:** [TypedDataValidator](#typeddatavalidator)",
"content": "```typescript\nexport type GetValidatorOutputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends StandardSchemaDataValidator<infer TYPE> ? StandardSchemaV1.InferOutput<TYPE> : VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferOutput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z.output<TYPE> : never;\n```\n**References:** [TypedDataValidator](#typeddatavalidator)",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts",
"mdFile": "qwik-city.getvalidatoroutputtype.md"
},
Expand Down Expand Up @@ -716,6 +716,20 @@
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/router-outlet-component.tsx",
"mdFile": "qwik-city.routeroutlet.md"
},
{
"name": "schema$",
"id": "schema_",
"hierarchy": [
{
"name": "schema$",
"id": "schema_"
}
],
"kind": "Variable",
"content": "```typescript\nschema$: StandardSchemaConstructor\n```",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/server-functions.ts",
"mdFile": "qwik-city.schema_.md"
},
{
"name": "server$",
"id": "server_",
Expand Down Expand Up @@ -786,6 +800,20 @@
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/sw-component.tsx",
"mdFile": "qwik-city.serviceworkerregister.md"
},
{
"name": "standardSchemaQrl",
"id": "standardschemaqrl",
"hierarchy": [
{
"name": "standardSchemaQrl",
"id": "standardschemaqrl"
}
],
"kind": "Variable",
"content": "```typescript\nstandardSchemaQrl: StandardSchemaConstructorQRL\n```",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/server-functions.ts",
"mdFile": "qwik-city.standardschemaqrl.md"
},
{
"name": "StaticGenerate",
"id": "staticgenerate",
Expand Down Expand Up @@ -838,7 +866,7 @@
}
],
"kind": "TypeAlias",
"content": "```typescript\nexport type TypedDataValidator = ValibotDataValidator | ZodDataValidator;\n```",
"content": "```typescript\nexport type TypedDataValidator = StandardSchemaDataValidator | ValibotDataValidator | ZodDataValidator;\n```",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts",
"mdFile": "qwik-city.typeddatavalidator.md"
},
Expand Down
45 changes: 34 additions & 11 deletions packages/docs/src/routes/api/qwik-city/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1388,11 +1388,13 @@ T

```typescript
export type GetValidatorInputType<VALIDATOR extends TypedDataValidator> =
VALIDATOR extends ValibotDataValidator<infer TYPE>
? v.InferInput<TYPE>
: VALIDATOR extends ZodDataValidator<infer TYPE>
? z.input<TYPE>
: never;
VALIDATOR extends StandardSchemaDataValidator<infer TYPE>
? StandardSchemaV1.InferInput<TYPE>
: VALIDATOR extends ValibotDataValidator<infer TYPE>
? v.InferInput<TYPE>
: VALIDATOR extends ZodDataValidator<infer TYPE>
? z.input<TYPE>
: never;
```

**References:** [TypedDataValidator](#typeddatavalidator)
Expand All @@ -1403,11 +1405,13 @@ export type GetValidatorInputType<VALIDATOR extends TypedDataValidator> =

```typescript
export type GetValidatorOutputType<VALIDATOR extends TypedDataValidator> =
VALIDATOR extends ValibotDataValidator<infer TYPE>
? v.InferOutput<TYPE>
: VALIDATOR extends ZodDataValidator<infer TYPE>
? z.output<TYPE>
: never;
VALIDATOR extends StandardSchemaDataValidator<infer TYPE>
? StandardSchemaV1.InferOutput<TYPE>
: VALIDATOR extends ValibotDataValidator<infer TYPE>
? v.InferOutput<TYPE>
: VALIDATOR extends ZodDataValidator<infer TYPE>
? z.output<TYPE>
: never;
```

**References:** [TypedDataValidator](#typeddatavalidator)
Expand Down Expand Up @@ -2173,6 +2177,14 @@ RouterOutlet: import("@builder.io/qwik").Component<unknown>;

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/router-outlet-component.tsx)

## schema$

```typescript
schema$: StandardSchemaConstructor;
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/server-functions.ts)

## server$

```typescript
Expand Down Expand Up @@ -2306,6 +2318,14 @@ JSXOutput

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/sw-component.tsx)

## standardSchemaQrl

```typescript
standardSchemaQrl: StandardSchemaConstructorQRL;
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/server-functions.ts)

## StaticGenerate

```typescript
Expand Down Expand Up @@ -2373,7 +2393,10 @@ export type StrictUnion<T> = Prettify<StrictUnionHelper<T, T>>;
## TypedDataValidator

```typescript
export type TypedDataValidator = ValibotDataValidator | ZodDataValidator;
export type TypedDataValidator =
| StandardSchemaDataValidator
| ValibotDataValidator
| ZodDataValidator;
```

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik-city/src/runtime/src/types.ts)
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@
}
],
"kind": "Function",
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\nJSXNode&lt;'script'&gt;",
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\n[JSXNode](#jsxnode)<!-- -->&lt;'script'&gt;",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts",
"mdFile": "qwik.prefetchserviceworker.md"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3651,7 +3651,7 @@ opts
</tbody></table>
**Returns:**

JSXNode&lt;'script'&gt;
[JSXNode](#jsxnode)&lt;'script'&gt;

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts)

Expand Down
2 changes: 1 addition & 1 deletion packages/insights/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"vite": "5.3.5",
"vite-tsconfig-paths": "4.3.2",
"vitest": "2.0.5",
"zod": "3.22.4"
"zod": "3.24.2"
},
"engines": {
"node": ">=16.8.0 <18.0.0 || >=18.11"
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"vfile": "6.0.2",
"vite": "^5",
"vite-imagetools": "^7",
"zod": "3.22.4"
"zod": "3.24.2"
},
"devDependencies": {
"@azure/functions": "3.5.1",
Expand Down
18 changes: 15 additions & 3 deletions packages/qwik-city/src/runtime/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RequestEventCommon } from '@builder.io/qwik-city/middleware/request-han
import { RequestEventLoader } from '@builder.io/qwik-city/middleware/request-handler';
import { RequestHandler } from '@builder.io/qwik-city/middleware/request-handler';
import type { ResolveSyncValue } from '@builder.io/qwik-city/middleware/request-handler';
import type { StandardSchemaV1 } from '@standard-schema/spec';
import type * as v from 'valibot';
import type { ValueOrPromise } from '@builder.io/qwik';
import { z } from 'zod';
Expand Down Expand Up @@ -240,14 +241,15 @@ export interface FormSubmitSuccessDetail<T> {
value: T;
}

// Warning: (ae-forgotten-export) The symbol "StandardSchemaDataValidator" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ValibotDataValidator" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ZodDataValidator" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type GetValidatorInputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferInput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z_2.input<TYPE> : never;
export type GetValidatorInputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends StandardSchemaDataValidator<infer TYPE> ? StandardSchemaV1.InferInput<TYPE> : VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferInput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z_2.input<TYPE> : never;

// @public (undocumented)
export type GetValidatorOutputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferOutput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z_2.output<TYPE> : never;
export type GetValidatorOutputType<VALIDATOR extends TypedDataValidator> = VALIDATOR extends StandardSchemaDataValidator<infer TYPE> ? StandardSchemaV1.InferOutput<TYPE> : VALIDATOR extends ValibotDataValidator<infer TYPE> ? v.InferOutput<TYPE> : VALIDATOR extends ZodDataValidator<infer TYPE> ? z_2.output<TYPE> : never;

// @public (undocumented)
export type GetValidatorType<VALIDATOR extends TypedDataValidator> = GetValidatorOutputType<VALIDATOR>;
Expand Down Expand Up @@ -429,6 +431,11 @@ export type RouteNavigate = QRL<(path?: string | number | URL, options?: {
// @public (undocumented)
export const RouterOutlet: Component<unknown>;

// Warning: (ae-forgotten-export) The symbol "StandardSchemaConstructor" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export const schema$: StandardSchemaConstructor;

// Warning: (ae-forgotten-export) The symbol "ServerConfig" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
Expand All @@ -451,6 +458,11 @@ export const ServiceWorkerRegister: (props: {
nonce?: string;
}) => JSXOutput;

// Warning: (ae-forgotten-export) The symbol "StandardSchemaConstructorQRL" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export const standardSchemaQrl: StandardSchemaConstructorQRL;

// @public (undocumented)
export interface StaticGenerate {
// (undocumented)
Expand All @@ -469,7 +481,7 @@ export type StaticGenerateHandler = ({ env, }: {
export type StrictUnion<T> = Prettify<StrictUnionHelper<T, T>>;

// @public (undocumented)
export type TypedDataValidator = ValibotDataValidator | ZodDataValidator;
export type TypedDataValidator = StandardSchemaDataValidator | ValibotDataValidator | ZodDataValidator;

// Warning: (ae-forgotten-export) The symbol "ContentState" needs to be exported by the entry point index.d.ts
//
Expand Down
1 change: 1 addition & 0 deletions packages/qwik-city/src/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export { routeAction$, routeActionQrl } from './server-functions';
export { globalAction$, globalActionQrl } from './server-functions';
export { routeLoader$, routeLoaderQrl } from './server-functions';
export { server$, serverQrl } from './server-functions';
export { schema$, standardSchemaQrl } from './server-functions';
export { valibot$, valibotQrl } from './server-functions';
export { zod$, zodQrl } from './server-functions';
export { validator$, validatorQrl } from './server-functions';
Expand Down
59 changes: 59 additions & 0 deletions packages/qwik-city/src/runtime/src/server-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type ValueOrPromise,
} from '@builder.io/qwik';

import type { StandardSchemaV1 } from '@standard-schema/spec';
import * as v from 'valibot';
import { z } from 'zod';
import type { RequestEventLoader } from '../../middleware/request-handler/types';
Expand Down Expand Up @@ -47,6 +48,9 @@ import type {
ZodConstructor,
ZodConstructorQRL,
ZodDataValidator,
StandardSchemaDataValidator,
StandardSchemaConstructorQRL,
StandardSchemaConstructor,
} from './types';
import { useAction, useLocation, useQwikCityEnv } from './use-functions';

Expand Down Expand Up @@ -253,6 +257,61 @@ const flattenValibotIssues = (issues: v.GenericIssue[]) => {
}, {});
};

/** @public */
export const standardSchemaQrl: StandardSchemaConstructorQRL = (
qrl: QRL<StandardSchemaV1 | ((ev: RequestEvent) => StandardSchemaV1)>
): StandardSchemaDataValidator => {
if (isServer) {
return {
__brand: 'standard-schema',
async validate(ev, inputData) {
const schema: StandardSchemaV1 = await qrl
.resolve()
.then((obj) => ('~standard' in obj ? obj : obj(ev)));
const data = inputData ?? (await ev.parseBody());
const result = await schema['~standard'].validate(data);
if (!result.issues) {
return {
success: true,
data: result.value,
};
} else {
if (isDev) {
console.error('ERROR: Standard Schema validation failed', result.issues);
}
const formErrors: string[] = [];
const fieldErrors: Partial<Record<string, string[]>> = {};
for (const issue of result.issues) {
const dotPath = issue.path
?.map((item) => (typeof item === 'object' ? item.key : item))
.join('.');
if (dotPath) {
const sub = fieldErrors[dotPath];
if (sub) {
sub.push(issue.message);
} else {
fieldErrors[dotPath] = [issue.message];
}
} else {
formErrors.push(issue.message);
}
}
return {
success: false,
status: 400,
error: { formErrors, fieldErrors },
};
}
},
};
}
return undefined as never;
};

/** @public */
export const schema$: StandardSchemaConstructor =
/*#__PURE__*/ implicit$FirstArg(standardSchemaQrl);

/** @alpha */
export const valibotQrl: ValibotConstructorQRL = (
qrl: QRL<
Expand Down
Loading