Skip to content

Commit

Permalink
Merge pull request #41 from iway1/bug/refine-transform
Browse files Browse the repository at this point in the history
fix typing issues with refine and transform usage
  • Loading branch information
iway1 authored Jan 14, 2023
2 parents 97ac22d + a1f0237 commit b0cef0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/__tests__/createSchemaForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
useReqDescription,
useTsController,
} from "../FieldContext";
import { expectTypeOf } from "expect-type";
import { createUniqueFieldSchema } from "../createFieldSchema";

const testIds = {
Expand Down Expand Up @@ -767,7 +768,8 @@ describe("createSchemaForm", () => {
render(<Form schema={Schema} onSubmit={() => {}} />);
}).toThrow();
});
it("should correctly type the form with multiple unique field schemas.", () => {

it("should have correct typings with multiple unique field schemas when transform and refine are used.", () => {
const A = createUniqueFieldSchema(z.string(), "one");
const B = createUniqueFieldSchema(z.string(), "two");
function In1(_: { req: string }) {
Expand All @@ -784,11 +786,16 @@ describe("createSchemaForm", () => {
const Form = createTsForm(mapping);

<Form
schema={z.object({
a: A,
b: B,
})}
onSubmit={() => {}}
schema={z
.object({
a: A,
b: B,
})
.refine((_) => true)
.transform((a) => a.a)}
onSubmit={(data) => {
expectTypeOf(data).toBeString();
}}
props={{
a: {
req: "One",
Expand Down
10 changes: 6 additions & 4 deletions src/createSchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export type ExtraProps = {
type UnwrapEffects<T extends AnyZodObject | ZodEffects<any, any>> =
T extends AnyZodObject
? T
: T extends ZodEffects<any, any>
? T["_def"]["schema"]
: T extends ZodEffects<infer EffectsSchema, any>
? EffectsSchema extends ZodEffects<infer EffectsSchemaInner, any>
? EffectsSchemaInner
: EffectsSchema
: never;

function checkForDuplicateTypes(array: RTFSupportedZodTypes[]) {
Expand Down Expand Up @@ -237,7 +239,7 @@ export function createTsForm<
/**
* A callback function that will be called with the data once the form has been submitted and validated successfully.
*/
onSubmit: (values: z.infer<UnwrapEffects<SchemaType>>) => void;
onSubmit: (values: z.infer<SchemaType>) => void;
/**
* Initializes your form with default values. Is a deep partial, so all properties and nested properties are optional.
*/
Expand Down Expand Up @@ -296,7 +298,7 @@ export function createTsForm<
*/
props?: RequireKeysWithRequiredChildren<
Partial<{
[key in keyof z.infer<SchemaType>]: Mapping[IndexOf<
[key in keyof z.infer<UnwrapEffects<SchemaType>>]: Mapping[IndexOf<
UnwrapMapping<Mapping>,
readonly [
IndexOfUnwrapZodType<
Expand Down

0 comments on commit b0cef0f

Please sign in to comment.