Skip to content

v0.13.0

Compare
Choose a tag to compare
@fabian-hiller fabian-hiller released this 23 Aug 00:02

Many thanks to @naruaway, @zkulbeda, @kurtextrem, @BastiDood, @ssalbdivad, @jussisaurio, @FlorianDevPhynix, @milankinen, @fvckDesa and @Demivan for contributing to this release.

  • Add fallback and fallbackAsync method (pull request #103)
  • Add excludes validation as negation of includes
  • Add support for more primitives to literal schema (pull request #102)
  • Add support for dynamic values to withDefault method
  • Change flatten function so that issues are also accepted as argument
  • Change return type of safeParse and safeParseAsync method
  • Change error handling and refactor library to improve performance
  • Rename .parse to ._parse and .types to ._types to mark it as internal

Migration guide

This version brings extreme performance improvements. However, this also required a few breaking changes. Below is an explanation of how these can be solved quite easily.

.parse

If you have been using the internal .parse API of a schema for validation directly, you must now switch to the parse method.

// Change this
YourSchema.parse(input);

// To that
parse(YourSchema, input);

safeParse

For safeParse we have depredated .data and .error. Use .output and .issues instead.

const result = safeParse(YourSchema, input);

if (result.success) {

  // Change this
  const output = result.data;

  // To that
  const output = result.output;

} else {

  // Change this
  const issues = result.error.issues;

  // To that
  const issues = result.issues;

}

If you still want to work with a ValiError, you can easily create a ValiError yourself.

const error = new ValiError(result.issues)

Custom validation

If you previously threw a ValiError on your own, for example in the pipeline of a schema, you must now return an object with an issue or output.

// Change this
const StringSchema = string([
  (input, info) => {
    if (input.length > 10) {
      throw new ValiError([
        {
          validation: 'custom',
          origin: 'value',
          message: 'Invalid length',
          input,
          ...info,
        },
      ]);
    }
    return input;
  },
]);

// To that
const StringSchema = string([
  (input) => {
    if (input.length > 10) {
      return {
        issue: {
          validation: 'custom',
          message: 'Invalid length',
          input,
        },
      };
    }
    return { output: input };
  },
]);

tRPC

Unfortunately we could not find a solution for tRPC yet. Currently you can either explicitly set your Valibot version to v0.12.0, call parse on your own or use TypeSchema as a layer in between.

Option 1:

npm install valibot@0.12.0     # npm
yarn add valibot@0.12.0        # yarn
pnpm add valibot@0.12.0        # pnpm
bun add valibot@0.12.0         # bun

Option 2:

import { parse } from 'valibot';

.input((input) => parse(YourSchema, input))

Option 3:

import { wrap } from '@decs/typeschema';

.input(wrap(YourSchema))