Skip to content

Commit

Permalink
fix typing issues with refine and transform usage
Browse files Browse the repository at this point in the history
  • Loading branch information
iway1 committed Jan 14, 2023
1 parent 6e5f818 commit b49a9b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/__tests__/createSchemaForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,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 +785,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) => {
data.startsWith("cool"); // just type checks as a string
}}
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 b49a9b8

Please sign in to comment.