validatorjs
validatorjs copied to clipboard
[Bug] Rules which use dot notation do not get given correct attribute names
Hey,
We've got a data structure (simplified to this)
const data = {
name: "owen",
address: {
line_one: "xxx",
line_two: null
}
}
validator = new Validator(data, {
"address.line_one": "required",
"address.line_two": "required"
})
The validation all kicks in perfectly, however the attribute name replacement doesn't quite work!
It ends up returning
{
"address.line_two": [
"The address.line two field is required."
]
}
As you can see the :attribute
replacement ends up being address.line two
- it has happily replaced the underscores.
However it's failed to remove anything before the .
dot notation.
Thanks!
This is still an issue. You can get around it while it gets fixed by setting a custom attributeFormatter
on your validator:
validator.setAttributeFormatter((attribute) => {
// Replaces `._[` with a `{blank space}` to make the attribute a bit more presentable
return attribute.replace(/[._[]/g, ' ').replace(/]/g, '')
})