resolvers
resolvers copied to clipboard
Add ajv-formats to the ajv resolver
Is your feature request related to a problem? Please describe. A lot of people, including myself, use ajv-formats as an extension for ajv.
ajv-formats makes it easy and secure to validate emails, urls, uuids, and many others string formats.
At the moment the ajv resolver does not include it, which means JSON Schemas properties with format do not compile and are
not validated.
I would like to highlight that ajv-formats is an official package under the ajv validator.
Describe the solution you'd like Include ajv-formats as part of the ajv resolver.
Describe alternatives you've considered Creating another ajv resolver with ajv-formats. I don't think this is the best idea, as adding ajv-formats by default does not cause any damage. The only thing it does is enhancing the grammar to support formats. Giving the the grammar is currently not "pure", because it already includes ajv-errors, I don't think this is a big issue. Alternative implemented here: https://github.com/react-hook-form/resolvers/pull/434
Additional context Change already implemented here in this PR: https://github.com/react-hook-form/resolvers/pull/431
Going back to my original comment, please build a custom resolver for your use case. I don't think we prefer this external package to be a dep for everyone's use case with AJV.
Created PR with a custom ajv-formats resolver: https://github.com/react-hook-form/resolvers/pull/434
sorry, I have confused you, I meant to use in your app level, not within this lib. thanks for the PR and sorry about the confusion.
Aha, thanks for pointing it out. I will close PRs but I will keep the issue open to see if people are interested on the feature, if that is ok with you.
Thanks again.
Sounds good to me, thanks for the willing to contribute and PRs 🙏
This would be a great addition to this ajvResolver. I would argue that people use ajv and jsonschema precisly because of very specific validation rule needs. Hence adding format-validation would make perfect sense. I do understand the trade-off being made here in favor of the more generic approach though. Only.. the more generic a library becomes, the less useful it is (can't remember where this quote came from)
I'd love to have the ajvFormats support for validating schema formats.
It's completely unnecessary to create a custom resolver, as ajvResolver accepts and Ajv.Options object as the second parameter which allows setting the formats on the AJV instance. You can import the formats from the ajv-formats library and pass them in. This works:
import { ajvResolver } from "@hookform/resolvers/ajv";
import { fullFormats } from "ajv-formats/dist/formats";
function Form() {
const { control: c, handleSubmit } = useForm({
resolver: ajvResolver(myJsonSchema, {
formats: fullFormats,
}),
});
//...
}
That being said, I think this should be the default behavior of this resolver. Even just copy / pasting the formats from ajv-resolver into this library and passing them into the Ajv constructor would be super awesome. I just checked and this addition would be an additional 2.5kB. We could still include the option to overwrite formats, just have the default be the formats that people know and love.
If we don't make this change, this is probably going to be a headache for a lot of people in the future because they will likely expect things like format: email to work out of the box, if I had to guess, and even if not it'll take them a while to figure out how to get it working. IMO make it the default b/c it will ultimately save many people time =D
The issue seems to be solved. Closing
I feel like there should be an easier way to add plugins, something like:
ajvResolver(myJsonSchema, {
plugins: [require("ajv-formats"), require("ajv-keywords")]
})
I don't think I user should have to make a resolver from scratch for something like a plugin.