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
});
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.
Originally posted by @FedeMadoery in https://github.com/iway1/react-ts-form/issues/50#issuecomment-1448041046