wwf
8 天以前 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { ZodError, z } from 'zod'
 
describe('Zod Features', () => {
  it('should support string', () => {
    const stringSchema = z.string()
    const numberLikeStringSchema = z.coerce.string() // 12 would be converted to '12'
    const stringSchemaWithError = z.string({
      required_error: 'Name is required',
      invalid_type_error: 'Invalid name type, expected string',
    })
 
    const urlSchema = z.string().url()
    const uuidSchema = z.string().uuid()
 
    expect(stringSchema.parse('hello')).toBe('hello')
    expect(() => stringSchema.parse(12)).toThrow()
    expect(numberLikeStringSchema.parse('12')).toBe('12')
    expect(numberLikeStringSchema.parse(12)).toBe('12')
    expect(() => stringSchemaWithError.parse(undefined)).toThrow('Name is required')
    expect(() => stringSchemaWithError.parse(12)).toThrow('Invalid name type, expected string')
 
    expect(urlSchema.parse('https://dify.ai')).toBe('https://dify.ai')
    expect(uuidSchema.parse('123e4567-e89b-12d3-a456-426614174000')).toBe('123e4567-e89b-12d3-a456-426614174000')
  })
 
  it('should support enum', () => {
    enum JobStatus {
      waiting = 'waiting',
      processing = 'processing',
      completed = 'completed',
    }
    expect(z.nativeEnum(JobStatus).parse(JobStatus.waiting)).toBe(JobStatus.waiting)
    expect(z.nativeEnum(JobStatus).parse('completed')).toBe('completed')
    expect(() => z.nativeEnum(JobStatus).parse('invalid')).toThrow()
  })
 
  it('should support number', () => {
    const numberSchema = z.number()
    const numberWithMin = z.number().gt(0) // alias min
    const numberWithMinEqual = z.number().gte(0)
    const numberWithMax = z.number().lt(100) // alias max
 
    expect(numberSchema.parse(123)).toBe(123)
    expect(numberWithMin.parse(50)).toBe(50)
    expect(numberWithMinEqual.parse(0)).toBe(0)
    expect(() => numberWithMin.parse(-1)).toThrow()
    expect(numberWithMax.parse(50)).toBe(50)
    expect(() => numberWithMax.parse(101)).toThrow()
  })
 
  it('should support boolean', () => {
    const booleanSchema = z.boolean()
    expect(booleanSchema.parse(true)).toBe(true)
    expect(booleanSchema.parse(false)).toBe(false)
    expect(() => booleanSchema.parse('true')).toThrow()
  })
 
  it('should support date', () => {
    const dateSchema = z.date()
    expect(dateSchema.parse(new Date('2023-01-01'))).toEqual(new Date('2023-01-01'))
  })
 
  it('should support object', () => {
    const userSchema = z.object({
      id: z.union([z.string(), z.number()]),
      name: z.string(),
      email: z.string().email(),
      age: z.number().min(0).max(120).optional(),
    })
 
    type User = z.infer<typeof userSchema>
 
    const validUser: User = {
      id: 1,
      name: 'John',
      email: 'john@example.com',
      age: 30,
    }
 
    expect(userSchema.parse(validUser)).toEqual(validUser)
  })
 
  it('should support object optional field', () => {
    const userSchema = z.object({
      name: z.string(),
      optionalField: z.optional(z.string()),
    })
    type User = z.infer<typeof userSchema>
 
    const user: User = {
      name: 'John',
    }
    const userWithOptionalField: User = {
      name: 'John',
      optionalField: 'optional',
    }
    expect(userSchema.safeParse(user).success).toEqual(true)
    expect(userSchema.safeParse(userWithOptionalField).success).toEqual(true)
  })
 
  it('should support object intersection', () => {
    const Person = z.object({
      name: z.string(),
    })
 
    const Employee = z.object({
      role: z.string(),
    })
 
    const EmployedPerson = z.intersection(Person, Employee)
    const validEmployedPerson = {
      name: 'John',
      role: 'Developer',
    }
    expect(EmployedPerson.parse(validEmployedPerson)).toEqual(validEmployedPerson)
  })
 
  it('should support record', () => {
    const recordSchema = z.record(z.string(), z.number())
    const validRecord = {
      a: 1,
      b: 2,
    }
    expect(recordSchema.parse(validRecord)).toEqual(validRecord)
  })
 
  it('should support array', () => {
    const numbersSchema = z.array(z.number())
    const stringArraySchema = z.string().array()
 
    expect(numbersSchema.parse([1, 2, 3])).toEqual([1, 2, 3])
    expect(stringArraySchema.parse(['a', 'b', 'c'])).toEqual(['a', 'b', 'c'])
  })
 
  it('should support promise', () => {
    const promiseSchema = z.promise(z.string())
    const validPromise = Promise.resolve('success')
 
    expect(promiseSchema.parse(validPromise)).resolves.toBe('success')
  })
 
  it('should support unions', () => {
    const unionSchema = z.union([z.string(), z.number()])
 
    expect(unionSchema.parse('success')).toBe('success')
    expect(unionSchema.parse(404)).toBe(404)
  })
 
  it('should support functions', () => {
    const functionSchema = z.function().args(z.string(), z.number(), z.optional(z.string())).returns(z.number())
    const validFunction = (name: string, age: number, _optional?: string): number => {
      return age
    }
    expect(functionSchema.safeParse(validFunction).success).toEqual(true)
  })
 
  it('should support undefined, null, any, and void', () => {
    const undefinedSchema = z.undefined()
    const nullSchema = z.null()
    const anySchema = z.any()
 
    expect(undefinedSchema.parse(undefined)).toBeUndefined()
    expect(nullSchema.parse(null)).toBeNull()
    expect(anySchema.parse('anything')).toBe('anything')
    expect(anySchema.parse(3)).toBe(3)
  })
 
  it('should safeParse would not throw', () => {
    expect(z.string().safeParse('abc').success).toBe(true)
    expect(z.string().safeParse(123).success).toBe(false)
    expect(z.string().safeParse(123).error).toBeInstanceOf(ZodError)
  })
})