Skip to content

Commit

Permalink
Add support for async validation with Zod #233
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Sep 2, 2024
1 parent d15a366 commit c9700d3
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/preact/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add support for async validation with Zod (issue #233)

## v0.9.1 (June 27, 2024)

- Fix missing default for generic of `FormStore` type (issue #219)
Expand Down
4 changes: 2 additions & 2 deletions packages/preact/src/adapters/zodField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { FieldValue, ValidateField, Maybe } from '../types';
export function zodField<TFieldValue extends FieldValue>(
schema: ZodType<any, any, TFieldValue>
): ValidateField<TFieldValue> {
return (value: Maybe<TFieldValue>) => {
const result = schema.safeParse(value);
return async (value: Maybe<TFieldValue>) => {
const result = await schema.safeParseAsync(value);
return result.success ? '' : result.error.issues[0].message;
};
}
4 changes: 2 additions & 2 deletions packages/preact/src/adapters/zodForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type {
export function zodForm<TFieldValues extends FieldValues>(
schema: ZodType<any, any, TFieldValues>
): ValidateForm<TFieldValues> {
return (values: PartialValues<TFieldValues>) => {
const result = schema.safeParse(values);
return async (values: PartialValues<TFieldValues>) => {
const result = await schema.safeParseAsync(values);
const formErrors: Record<string, string> = {};
if (!result.success) {
for (const issue of result.error.issues) {
Expand Down
4 changes: 4 additions & 0 deletions packages/qwik/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add support for async validation with Zod (issue #233)

## v0.26.1 (June 27, 2024)

- Fix missing default for generic of `FormStore` type (issue #219)
Expand Down
6 changes: 3 additions & 3 deletions packages/qwik/src/utils/getParsedZodSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function getParsedZodSchema<Schema, Value>(
value: Value
): Promise<SafeParseReturnType<Schema, any>> {
const zodSchema = await schema.resolve();
return (typeof zodSchema === 'function' ? zodSchema() : zodSchema).safeParse(
value
);
return (
typeof zodSchema === 'function' ? zodSchema() : zodSchema
).safeParseAsync(value);
}
4 changes: 4 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add support for async validation with Zod (issue #233)

## v0.8.1 (June 27, 2024)

- Fix missing default for generic of `FormStore` type (issue #219)
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/adapters/zodField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { FieldValue, ValidateField, Maybe } from '../types';
export function zodField<TFieldValue extends FieldValue>(
schema: ZodType<any, any, TFieldValue>
): ValidateField<TFieldValue> {
return (value: Maybe<TFieldValue>) => {
const result = schema.safeParse(value);
return async (value: Maybe<TFieldValue>) => {
const result = await schema.safeParseAsync(value);
return result.success ? '' : result.error.issues[0].message;
};
}
4 changes: 2 additions & 2 deletions packages/react/src/adapters/zodForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type {
export function zodForm<TFieldValues extends FieldValues>(
schema: ZodType<any, any, TFieldValues>
): ValidateForm<TFieldValues> {
return (values: PartialValues<TFieldValues>) => {
const result = schema.safeParse(values);
return async (values: PartialValues<TFieldValues>) => {
const result = await schema.safeParseAsync(values);
const formErrors: Record<string, string> = {};
if (!result.success) {
for (const issue of result.error.issues) {
Expand Down
4 changes: 4 additions & 0 deletions packages/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add support for async validation with Zod (issue #233)

## v0.22.1 (June 27, 2024)

- Fix missing default for generic of `FormStore` type (issue #219)
Expand Down
4 changes: 2 additions & 2 deletions packages/solid/src/adapters/zodField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type { FieldValue, ValidateField, Maybe } from '../types';
export function zodField<TFieldValue extends FieldValue>(
schema: ZodType<any, any, TFieldValue>
): ValidateField<TFieldValue> {
return (value: Maybe<TFieldValue>) => {
const result = schema.safeParse(value);
return async (value: Maybe<TFieldValue>) => {
const result = await schema.safeParseAsync(value);
return result.success ? '' : result.error.issues[0].message;
};
}
4 changes: 2 additions & 2 deletions packages/solid/src/adapters/zodForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type {
export function zodForm<TFieldValues extends FieldValues>(
schema: ZodType<any, any, TFieldValues>
): ValidateForm<TFieldValues> {
return (values: PartialValues<TFieldValues>) => {
const result = schema.safeParse(values);
return async (values: PartialValues<TFieldValues>) => {
const result = await schema.safeParseAsync(values);
const formErrors: Record<string, string> = {};
if (!result.success) {
for (const issue of result.error.issues) {
Expand Down

0 comments on commit c9700d3

Please sign in to comment.