-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
nested_object.ts
154 lines (145 loc) · 3.44 KB
/
nested_object.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
// @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 {
username: 'virk',
password: 'secret',
contact: {
name: 'virk',
address: 'universe',
},
}
}
const zodSchema = z.object({
username: z.string(),
password: z.string(),
contact: z.object({
name: z.string(),
address: z.string().optional(),
}),
})
const yupSchema = yup
.object({
username: yup.string().required(),
password: yup.string().required(),
contact: yup
.object({
name: yup.string().required(),
address: yup.string(),
})
.required(),
})
.required()
const vineSchema = vine.compile(
vine.object({
username: vine.string(),
password: vine.string(),
contact: vine.object({
name: vine.string(),
address: vine.string().optional(),
}),
})
)
const valibotSchame = valibot.object({
username: valibot.string(),
password: valibot.string(),
contact: valibot.object({
name: valibot.string(),
address: valibot.optional(valibot.string()),
}),
})
const joiSchema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
contact: Joi.object({
name: Joi.string().required(),
address: Joi.string(),
}).required(),
}).required()
const ajv = new Ajv.default()
interface AjvData {
username: string
password: string
contact: {
name: string
address?: string
}
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
username: { type: 'string', nullable: false },
password: { type: 'string', nullable: false },
contact: {
type: 'object',
properties: {
name: { type: 'string', nullable: false },
address: { type: 'string' },
},
required: ['name'],
},
},
required: ['username', 'password', 'contact'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)
console.log('=================================')
console.log('Benchmarking with nested object')
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(valibotSchame, 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 })