Validation Library Support
RVF is supports Standard Schema out of the box, so you can use any compatible validation library with no adapter. Some of these include:
- Zod
- Valibot
- ArkType
- Effect Schema
- And more
There's also an official adapter for yup
. Install @rvf/yup
if you want to use yup
.
Creating an adapter
If you use a validation library that doesn't implement Standard Schema, and doesn't have an official adapter, you can make one yourself.
The approach outlined here uses the legacy createValidator
api.
If you're able to, we recommend creating an adapter to turn the schema into a Standard Schema.
You might also be able to raise a feature request to add Standard Schema support to that library.
However, this option will remain open until we have documentation on how to do that.
In order to make an adapter for your validation library of choice,
you can create a function that accepts a schema from the validation library and turns it into a validator using createValidator
.
For more on this you can check the implementations for withYup
.
The out-of-the-box support for yup
in this library works like this:
export const withYup = <Schema extends AnyObjectSchema>(
validationSchema: Schema,
// For best result with Typescript,
// we should type the `Validator`
// we return based on the provided schema
): Validator<InferType<Schema>> =>
createValidator({
validate: async (unvalidatedData) => {
// Validate with yup and return the
// validated & typed data or the error
if (isValid)
return {
data: { field1: "someValue" },
error: undefined,
};
else
return {
error: { field1: "Some error!" },
data: undefined,
};
},
});