Closed notflip closed 4 months ago
Sure, add validation to the top level array item, you can even return item-specific paths with an error message.
defineField({
name: 'title',
type: 'internationalizedArrayString',
description: `Catch the reader's eye. Use fewer than 5 words.`,
validation: (rule) =>
rule.custom<{value: string; _type: string; _key: string}[]>((value) => {
if (!value) {
return 'Title is required'
}
const invalidItems = value.filter((item) => item.value.split(' ').length > 5)
if (invalidItems.length) {
return invalidItems.map((item) => ({
message: `Title "${item.value}" is too long. Must be 5 words or fewer.`,
path: [{_key: item._key}, 'value'],
}))
}
return true
}),
}),
Added this example to the Readme now too :)
Beautiful, thanks! @SimeonGriggs I'm loving Sanity so far, certainly so with this nice way of handling translations
@SimeonGriggs Your suggestion works, but I can't seem to see the error message inline in the field (with the icon, like with normal validations), the field is just red and the message is only in the validations sidebar. Should open a new issue for this?
Is your feature request related to a problem? Please describe. Is it possible to validate the entry of a translated field? For example:
Describe the solution you'd like Having
validation: (Rule) => Rule.required(),
work on theinternationalizedArrayString
field