iway1 / react-ts-form

https://react-ts-form.com
MIT License
2.01k stars 35 forks source link

Schema created with 'createUniqueFieldSchema' stops working if added .optional() #73

Closed FedeMadoery closed 1 year ago

FedeMadoery commented 1 year ago

I have the same issue reported on #50, I'm working with v1.2.0... I will add a code sandbox that reproduces the error.

Code sandbox that reproduces the error with minimal setup. https://codesandbox.io/embed/react-ts-form-optional-error-3oz8lk?fontsize=14&hidenavigation=1&theme=dark


Code example, just in case.

const MAX_FILE_SIZE = 1000000000000;

export const FileSchema = createUniqueFieldSchema(
  z
    .any()
    .refine((value) => !!value && !!value.file, "Upload a file is required.")
    .refine(
      (value) => value?.file?.size <= MAX_FILE_SIZE,
      `Max file size is 5MB.`
    ),
  "FileSchema"
);
const FileSchemaComponent = () => <div>{`FileSchema :)`}</div>;

// create the mapping
const mapping = [[FileSchema, FileSchemaComponent]] as const; // 👈 `as const` is necessary

// A typesafe React component
const MyForm = createTsForm(mapping);

// Creates Schema
const schema = z.object({
  file: FileSchema.optional() // if .optional is added to a schema it stops working
  // My Original file field has a description, just in case I let an example without it, the issue persists in both cases
  // fileWithDescript: FileSchema.optional().description('Label // Description') // if .optional is added to a schema it stops working
});

Originally posted by @FedeMadoery in https://github.com/iway1/react-ts-form/issues/50#issuecomment-1448041046

iway1 commented 1 year ago

Thanks for the repro, I think I know what's causing this. gonna check this out later today

iway1 commented 1 year ago

fixed in 1.3.1