zod-matcher 中文文档教程

发布于 2年前 浏览 14 项目主页 更新于 2年前

Zod Matcher

Rust 启发了 Zod 模式的模式匹配

安装

yarn add zod-matcher
npm install zod-matcher

基本用法

Rust 中的示例匹配

let x = 1;

match x {
    1 => println!("one"),
    2 => println!("two"),
    3 => println!("three"),
    _ => println!("anything"),
}

与 zod-matcher 等效

declare const x : number

match(x)
    .case(z.literal(1), () => console.log('one'))
    .case(z.literal(2), () => console.log('two'))
    .case(z.literal(3), () => console.log('three'))
    .default(() => console.log('anything'))
    .parse()

为什么使用 zod-matcher

类型安全

未处理的情况不允许您解析。您必须处理每种情况或回退到默认值。

const x = 'A' as 'A' | 'B'

// Error "Unhandled cases"
match(x)
    .case(z.literal('A'), console.log)
    .parse()

// Resolve by adding case
match(x)
    .case(z.literal('A'), console.log)
    .case(z.literal('B'), console.log)
    .parse()

// Or by adding default
match(x)
    .case(z.literal('A'), console.log)
    .default(console.log)
    .parse()

类型缩小

.default() 方法将输入值作为键入的参数传递,不包括任何先前处理的情况。

const x = 'A' as 'A' | 'B' | 'C';

match(x)
  .case(z.literal('A'), console.log)
  .case(z.literal('B'), console.log)
  .default(x => x) // <== Type of x is "C"
  .parse();

联合返回

返回类型是每个案例的所有返回类型的联合。

const x = 'A' as 'A' | 'B' | 'C';

// Type of result is "A1" | "B2" | "C3"
const result = match(x)
  .case(z.literal('A'), x => `${x}1` as const)
  .case(z.literal('B'), x => `${x}2` as const)
  .case(z.literal('C'), x => `${x}3` as const)
  .parse();

没有不必要的情况!

如果所有可能的情况都已考虑在内,我们将删除添加更多情况的选项。

const x = 'A' as const

const result = match(x)
  .case(z.literal('A'), console.log)
  .case(z.literal('B'), console.log) // <== Error. All cases already handled
  .parse();

安全解析

您可以使用 .parse() 引发失败的匹配,或使用 .safeParse() 返回结果联合。

// Throws error if no match
const result = match(x)
    .case(z.string(), console.log)
    .parse()

// Returns result of union type
// | { success: true, data: x }
// | { success: false, error: MatcherError }
const result = match(x)
    .case(z.string(), console.log)
    .safeParse()

Zod Matcher

Rust inspired pattern matching for Zod schemas

Installation

yarn add zod-matcher
npm install zod-matcher

Basic usage

Example match in Rust

let x = 1;

match x {
    1 => println!("one"),
    2 => println!("two"),
    3 => println!("three"),
    _ => println!("anything"),
}

Equivalent with zod-matcher

declare const x : number

match(x)
    .case(z.literal(1), () => console.log('one'))
    .case(z.literal(2), () => console.log('two'))
    .case(z.literal(3), () => console.log('three'))
    .default(() => console.log('anything'))
    .parse()

Why use zod-matcher

Type safety

Unhandled cases won't allow you to parse. You must handle every case or fallback to a default.

const x = 'A' as 'A' | 'B'

// Error "Unhandled cases"
match(x)
    .case(z.literal('A'), console.log)
    .parse()

// Resolve by adding case
match(x)
    .case(z.literal('A'), console.log)
    .case(z.literal('B'), console.log)
    .parse()

// Or by adding default
match(x)
    .case(z.literal('A'), console.log)
    .default(console.log)
    .parse()

Type narrowing

The .default() method passes the input value as an argument typed excluding any previously handled cases.

const x = 'A' as 'A' | 'B' | 'C';

match(x)
  .case(z.literal('A'), console.log)
  .case(z.literal('B'), console.log)
  .default(x => x) // <== Type of x is "C"
  .parse();

Union return

The return type is a union of all the return types of each case.

const x = 'A' as 'A' | 'B' | 'C';

// Type of result is "A1" | "B2" | "C3"
const result = match(x)
  .case(z.literal('A'), x => `${x}1` as const)
  .case(z.literal('B'), x => `${x}2` as const)
  .case(z.literal('C'), x => `${x}3` as const)
  .parse();

No unnecessary cases!

If all possible cases are already accounted for we remove the option to add more.

const x = 'A' as const

const result = match(x)
  .case(z.literal('A'), console.log)
  .case(z.literal('B'), console.log) // <== Error. All cases already handled
  .parse();

Safe parsing

You can throw on failed matches using .parse() or return a result union with .safeParse().

// Throws error if no match
const result = match(x)
    .case(z.string(), console.log)
    .parse()

// Returns result of union type
// | { success: true, data: x }
// | { success: false, error: MatcherError }
const result = match(x)
    .case(z.string(), console.log)
    .safeParse()
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文