Lack of validations for Image() and File() #7319
Replies: 5 comments 2 replies
-
What I'm currently very interested in is the |
Beta Was this translation helpful? Give feedback.
-
@noor-codes what type of validation did you have in mind? |
Beta Was this translation helpful? Give feedback.
-
Right now, In Keystone-v6 If we have an Let's say I want to upload an image or a file from the admin-ui and do not select any image or file and hit What we have:export const lists = {
Profile: list({
fields: {
profile_img: image() //No Options to validate the image() or file().
}
})
} What we want:export const lists = {
Profile: list({
fields: {
profile_img: image({ validation: { isRequired: true }, acceptable_files: [`.jpg`, '.png'] })
}
})
} |
Beta Was this translation helpful? Give feedback.
-
This is what I do in the custom hook export const file_ValidateInput = async ({ operation, resolvedData, addValidationError }) => {
// Check if the user creates a file.
if (operation === 'create') {
//Check if the user has selected any file.
if (!resolvedData?.file.filename) {
return addValidationError('Please select a file!')
}
// Generate Required Information.
const fileName: string = resolvedData?.file.filename
const extension: string = fileName.split('.')[1]
const acceptable_files: string[] = ['zip']
const content_type: string = generateFile_content_type(extension)
// Check if the file has any extension.
if (!extension) {
return addValidationError('The file is not supported!')
}
// Check if the file is acceptable.
if (!acceptable_files.includes(extension)) {
addValidationError(`${extension.toUpperCase()} files are not allowed to be uploaded!`)
addValidationError(`You can upload .zip files only.`)
}
// Perform all changes before saving the information.
resolvedData.content_type = content_type
resolvedData.extension = extension
resolvedData.created_at = new Date()
}
} The things I do in this piece of code.
|
Beta Was this translation helpful? Give feedback.
-
Still looking for this; validation support for files and images would be nice without a custom hook. |
Beta Was this translation helpful? Give feedback.
-
Lack of validations for Image() and File()
If we look at
text()
or other types, There are validations like if the field is required or not. We don't seem to have the same validations options forfile()
andimage()
type.Currently I have to write down my own complex hooks to achieve the same results which can be less than a line.
Any specific reason?
Beta Was this translation helpful? Give feedback.
All reactions