diff --git a/standard-schema/src/__tests__/Form.tsx b/standard-schema/src/__tests__/Form.tsx index 500a46d..93dc5a6 100644 --- a/standard-schema/src/__tests__/Form.tsx +++ b/standard-schema/src/__tests__/Form.tsx @@ -12,16 +12,16 @@ const schema = type({ type FormData = typeof schema.infer & { unusedProperty: string }; -interface Props { - onSubmit: (data: FormData) => void; -} - -function TestComponent({ onSubmit }: Props) { +function TestComponent({ + onSubmit, +}: { + onSubmit: (data: typeof schema.infer) => void; +}) { const { register, handleSubmit, formState: { errors }, - } = useForm({ + } = useForm({ resolver: standardSchemaResolver(schema), // Useful to check TypeScript regressions }); @@ -54,3 +54,29 @@ test("form's validation with arkType and TypeScript's integration", async () => ).toBeInTheDocument(); expect(handleSubmit).not.toHaveBeenCalled(); }); + +export function TestComponentManualType({ + onSubmit, +}: { + onSubmit: (data: FormData) => void; +}) { + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + resolver: standardSchemaResolver(schema), // Useful to check TypeScript regressions + }); + + return ( +
+ + {errors.username && {errors.username.message}} + + + {errors.password && {errors.password.message}} + + +
+ ); +} diff --git a/standard-schema/src/__tests__/__fixtures__/data.ts b/standard-schema/src/__tests__/__fixtures__/data.ts index 5f0495e..a84cddd 100644 --- a/standard-schema/src/__tests__/__fixtures__/data.ts +++ b/standard-schema/src/__tests__/__fixtures__/data.ts @@ -43,7 +43,7 @@ export const invalidData = { birthYear: 'birthYear', like: [{ id: 'z' }], url: 'abc', -}; +} as any as typeof schema.infer; export const fields: Record = { username: { diff --git a/standard-schema/src/index.ts b/standard-schema/src/index.ts index 5ee0d3c..ef117ca 100644 --- a/standard-schema/src/index.ts +++ b/standard-schema/src/index.ts @@ -1,2 +1 @@ export * from './standard-schema'; -export * from './types'; diff --git a/standard-schema/src/standard-schema.ts b/standard-schema/src/standard-schema.ts index 12d9a9a..1aaa15b 100644 --- a/standard-schema/src/standard-schema.ts +++ b/standard-schema/src/standard-schema.ts @@ -1,9 +1,8 @@ import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers'; import { StandardSchemaV1 } from '@standard-schema/spec'; -import { FieldError } from 'react-hook-form'; -import type { Resolver } from './types'; +import { FieldError, FieldValues, Resolver } from 'react-hook-form'; -const parseIssues = (issues: readonly StandardSchemaV1.Issue[]) => { +function parseIssues(issues: readonly StandardSchemaV1.Issue[]) { const errors: Record = {}; for (let i = 0; i < issues.length; i++) { @@ -18,10 +17,15 @@ const parseIssues = (issues: readonly StandardSchemaV1.Issue[]) => { } return errors; -}; - -export const standardSchemaResolver: Resolver = - (schema) => async (values, _, options) => { +} + +export function standardSchemaResolver< + TFieldValues extends FieldValues, + Schema extends StandardSchemaV1, +>( + schema: Schema, +): Resolver['output']> { + return async (values: TFieldValues, _, options) => { let result = schema['~standard'].validate(values); if (result instanceof Promise) { result = await result; @@ -43,3 +47,4 @@ export const standardSchemaResolver: Resolver = errors: {}, }; }; +} diff --git a/standard-schema/src/types.ts b/standard-schema/src/types.ts deleted file mode 100644 index c1b8d51..0000000 --- a/standard-schema/src/types.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { StandardSchemaV1 } from '@standard-schema/spec'; -import { FieldValues, ResolverOptions, ResolverResult } from 'react-hook-form'; - -export type Resolver = >( - schema: T, -) => ( - values: TFieldValues, - context: TContext | undefined, - options: ResolverOptions, -) => Promise>;