Valibot
https://scrapbox.io/files/690c315f925863818e775c94.png
https://valibot.dev/
https://github.com/fabian-hiller/valibot
by Fabian Hiller
特徴
バンドルサイズが小さい!!!!
公式 Playground がある
Playground | Valibot
バリバリ軽量No.1な検証ライブラリ Valibot の紹介
https://scrapbox.io/files/690c3147ad3c0cc2e9b37f21.png
schema で型を検証し、action で値を処理
それらを pipe でまとめる
Zod でのメソッドチェーン形式の記述よりも記述量は増えるが役割が明確に分離されている
Pipe V.S. Chain
code:ts
import * as v from "valibot";
const StringSchema = v.string(); // 大文字始まりが慣例
let inputValue: unknown;
try {
const result: string = v.parse(StringSchema, inputValue);
console.log(${result.output} is string);
} catch (e: unknown) {
// バリデーションに引っかかると例外を throw
if (v.isValiError(e) console.error(e);
}
code:ts
const StringSchema = v.string();
let inputValue: unknown;
const result = v.safeParse(StringSchema, inputValue);
if (result.success) console.log(${result.output} is string);
else console.error(new v.valiError(result.issues));