Skip to content

Commit

Permalink
Merge pull request #39 from nakrovati/fix-lolo32-pr
Browse files Browse the repository at this point in the history
Fix lolo32 PR
  • Loading branch information
RomainLanz authored Feb 22, 2024
2 parents f94d274 + 200bb39 commit ef50170
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
55 changes: 55 additions & 0 deletions benchmarks/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 {
Expand Down Expand Up @@ -64,6 +66,42 @@ const valibotSchema = valibot.object({
),
})

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('======================')
Expand Down Expand Up @@ -94,6 +132,23 @@ suite
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))
})
Expand Down
41 changes: 41 additions & 0 deletions benchmarks/flat_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 {
Expand Down Expand Up @@ -36,6 +38,28 @@ const valibotSchema = valibot.object({
password: valibot.string(),
})

const joiSchema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
}).required()

const ajv = new Ajv.default()
interface AjvData {
username: string
password: string
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
username: { type: 'string', nullable: false },
password: { type: 'string', nullable: false },
},
required: ['username', 'password'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)

console.log('===============================')
console.log('Benchmarking with flat object')
console.log('===============================')
Expand Down Expand Up @@ -66,6 +90,23 @@ suite
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))
})
Expand Down
57 changes: 57 additions & 0 deletions benchmarks/nested_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 {
Expand Down Expand Up @@ -58,6 +60,44 @@ const valibotSchame = valibot.object({
}),
})

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('=================================')
Expand Down Expand Up @@ -88,6 +128,23 @@ suite
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))
})
Expand Down
71 changes: 71 additions & 0 deletions benchmarks/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Benchmark from 'benchmark'
import { z } from 'zod'
import vine from '../index.js'
import * as valibot from 'valibot'
import Joi from 'joi'
import Ajv, { AsyncSchema } from 'ajv'

function getData() {
return {
Expand Down Expand Up @@ -60,6 +62,58 @@ const valibotSchema = valibot.object({
]),
})

const joiSchema = Joi.object({
contact: Joi.alternatives()
.try(
Joi.object({ type: 'email', email: Joi.string().required() }),
Joi.object({ type: 'phone', mobile_number: Joi.string().required() })
)
.required(),
}).required()

const ajv = new Ajv.default({ discriminator: true })
interface AjvEmail {
type: 'email'
email: string
}
interface AjvPhone {
type: 'phone'
mobile_number: string
}
interface AjvData {
contact: AjvEmail | AjvPhone
}
const ajvSchema: AsyncSchema = {
$async: true,
type: 'object',
properties: {
contact: {
type: 'object',
discriminator: { propertyName: 'type' },
required: ['type'],
oneOf: [
{
properties: {
type: { const: 'email' },
email: { type: 'string', nullable: false },
},
required: ['email'],
},
{
properties: {
type: { const: 'phone' },
mobile_number: { type: 'string', nullable: false },
},
required: ['mobile_number'],
},
],
},
},
required: ['contact'],
additionalProperties: false,
}
const ajvValidator = ajv.compile<AjvData>(ajvSchema)

console.log('=======================')
console.log('Benchmarking unions')
console.log('=======================')
Expand All @@ -84,6 +138,23 @@ suite
valibot.parseAsync(valibotSchema, getData()).then(() => deferred.resolve())
},
})
.add('Joi', {
defer: true,
fn: function (deferred: any) {
joiSchema
.validateAsync(getData())
.then(() => deferred.resolve())
.catch((err) => console.dir(err, { depth: 20, colors: true }))
},
})
.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))
})
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
"@swc/core": "^1.3.107",
"@types/dlv": "^1.1.4",
"@types/node": "^20.11.10",
"ajv": "^8.12.0",
"benchmark": "^2.1.4",
"c8": "^9.1.0",
"del-cli": "^5.1.0",
"eslint": "^8.56.0",
"github-label-sync": "^2.3.1",
"husky": "^9.0.6",
"joi": "^17.9.2",
"np": "^9.2.0",
"prettier": "^3.2.4",
"ts-node": "^10.9.2",
Expand Down

0 comments on commit ef50170

Please sign in to comment.