Validation Library Support

There are official adapters available for zod, yup, and valibot, but you can easily support whatever library you want by creating your own adapter.

And if you create an adapter for a library, feel free to make a PR to add official support 😊

Creating an adapter

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 withZod, withYup, and withValibot.

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,
        };
    },
  });