I use input type="file"
and instead of saving data in FileData
I use just plain object and created a type for it: Record<string, File>
. It works well, but when I try to create a validator with zod
– I don't get how to create the same type for it.
The usage:
import { object as zodObject, string, number } from 'zod';
import { useValidators } from '../some';
const useValidation = () => {
const { createResolver } = useValidators();
return {
resolver: createResolver(
zodObject({
name: string(),
price: number(),
files: ???,
})
),
};
};
The doc is quite excessive, but I could not find any example for my case: https://github.com/colinhacks/zod
The usage of input:
const App = () => {
const [files, setFiles] = useState<Record<string, File>>({});
return (
<input
type="file"
onChange={event => {
const files = event.target.files;
const newFiles: Record<string, File> = {};
const keys = Object.keys(files);
for(let i = 0; i < keys.length; i++) {
const file = newFiles[key];
newFiles[file.name] = file;
}
setFiles(newFiles);
}}
/>
)
}
File
type looks like? – Nonpartisaninput
. So, the main case to have this type to setfile.name
as akey
of object to have a fast access for updating and deleting files. Cause input should store multiple files and user can add or remove some of the files. – Glyptic