forked from vinejs/vine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.ts
158 lines (149 loc) · 3.23 KB
/
array.ts
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
// @ts-ignore
import Benchmark from 'benchmark'
import { z } from 'zod'
import * as yup from 'yup'
import vine from '../index.js'
import * as valibot from 'valibot'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'
function getData() {
return {
contacts: [
{
type: 'email',
value: 'foo@bar.com',
},
{
type: 'phone',
value: '12345678',
},
],
}
}
const zodSchema = z.object({
contacts: z.array(
z.object({
type: z.string(),
value: z.string(),
})
),
})
const yupSchema = yup
.object({
contacts: yup
.array(
yup
.object({
type: yup.string().required(),
value: yup.string().required(),
})
.required()
)
.required(),
})
.required()
const vineSchema = vine.compile(
vine.object({
contacts: vine.array(
vine.object({
type: vine.string(),
value: vine.string(),
})
),
})
)
const valibotSchema = valibot.object({
contacts: valibot.array(
valibot.object({
type: valibot.string(),
value: valibot.string(),
})
),
})
const joiSchema = Joi.object({
contacts: Joi.array()
.items(
Joi.object({
type: Joi.string().required(),
value: Joi.string().required(),
})
)
.required(),
}).required()
const ajv = new Ajv.default()
interface AjvData {
contacts: [{ type: string; value: string }]
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
contacts: {
type: 'array',
items: {
type: 'object',
properties: {
type: { type: 'string', nullable: false },
value: { type: 'string', nullable: false },
},
required: ['type', 'value'],
},
},
},
required: ['contacts'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)
console.log('======================')
console.log('Benchmarking arrays')
console.log('======================')
const suite = new Benchmark.Suite()
suite
.add('Vine', {
defer: true,
fn: function (deferred: any) {
vineSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Zod', {
defer: true,
fn: function (deferred: any) {
zodSchema.parseAsync(getData()).then(() => deferred.resolve())
},
})
.add('Yup', {
defer: true,
fn: function (deferred: any) {
yupSchema.validate(getData()).then(() => deferred.resolve())
},
})
.add('Valibot', {
defer: true,
fn: function (deferred: any) {
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.add('Joi', {
defer: true,
fn: function (deferred: any) {
joiSchema
.validateAsync(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
})
.add('Ajv', {
defer: true,
fn: function (deferred: any) {
ajvValidator(getData())
.then(() => deferred.resolve())
.catch(console.log)
},
})
.on('cycle', function (event: any) {
console.log(String(event.target))
})
.on('complete', function (this: any) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })