Skip to content

Commit 956ca7c

Browse files
authored
Merge pull request #1707 from DarkGL/feat/joi-benchmarks
feat: integrate Joi benchmarks
2 parents f6d70de + 8f42e85 commit 956ca7c

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [@effect/schema](https://github.com/Effect-TS/effect/blob/main/packages/schema/README.md)
2727
* [io-ts](https://github.com/gcanti/io-ts)
2828
* [jet-schema](https://github.com/seanpmaxwell/jet-schema)
29+
* [joi] (https://github.com/hapijs/joi)
2930
* [jointz](https://github.com/moodysalem/jointz)
3031
* [json-decoder](https://github.com/venil7/json-decoder)
3132
* [@mojotech/json-type-validaton](https://github.com/mojotech/json-type-validation)

cases/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const cases = [
1010
'computed-types',
1111
'decoders',
1212
'io-ts',
13+
'joi',
1314
'jointz',
1415
'json-decoder',
1516
'mol_data',

cases/joi.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Joi from 'joi';
2+
import { createCase } from '../benchmarks';
3+
4+
const schema = Joi.object({
5+
number: Joi.number().required(),
6+
negNumber: Joi.number().required(),
7+
maxNumber: Joi.number().unsafe().required(),
8+
string: Joi.string().required(),
9+
longString: Joi.string().required(),
10+
boolean: Joi.boolean().required(),
11+
deeplyNested: Joi.object({
12+
foo: Joi.string().required(),
13+
num: Joi.number().required(),
14+
bool: Joi.boolean().required(),
15+
}).required(),
16+
});
17+
18+
createCase('joi', 'parseSafe', () => {
19+
return data => {
20+
const { value, error } = schema.validate(data, {
21+
stripUnknown: true,
22+
allowUnknown: true,
23+
convert: false,
24+
});
25+
26+
if (error) throw error;
27+
28+
return value;
29+
};
30+
});
31+
32+
createCase('joi', 'parseStrict', () => {
33+
return data => {
34+
const { value, error } = schema.validate(data, {
35+
allowUnknown: false,
36+
convert: false,
37+
});
38+
39+
if (error) throw error;
40+
41+
return value;
42+
};
43+
});
44+
45+
createCase('joi', 'assertLoose', () => {
46+
return data => {
47+
const { error } = schema.validate(data, {
48+
stripUnknown: false,
49+
convert: false,
50+
allowUnknown: true,
51+
});
52+
if (error) throw error;
53+
return true;
54+
};
55+
});
56+
57+
createCase('joi', 'assertStrict', () => {
58+
const strictSchema = schema.options({ convert: false });
59+
return data => {
60+
const { error } = strictSchema.validate(data, {
61+
convert: false,
62+
allowUnknown: false,
63+
});
64+
if (error) throw error;
65+
return true;
66+
};
67+
});

download-packages-popularity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ export const packages = [
214214
name: 'type-predicate-generator',
215215
packageName: 'type-predicate-generator',
216216
},
217+
{
218+
name: 'joi',
219+
packageName: 'joi',
220+
},
217221
] as const;
218222

219223
interface BodyWeeklyDownloads {

package-lock.json

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"fp-ts": "2.16.9",
6565
"io-ts": "2.2.22",
6666
"jet-schema": "1.4.3",
67+
"joi": "17.13.3",
6768
"jointz": "7.0.4",
6869
"json-decoder": "1.4.1",
6970
"mol_data_all": "1.1.1386",

test/benchmarks.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '../cases/cleaners';
1515
import '../cases/computed-types';
1616
import '../cases/decoders';
1717
import '../cases/io-ts';
18+
import '../cases/joi';
1819
import '../cases/jointz';
1920
import '../cases/json-decoder';
2021
import '../cases/mol_data';

0 commit comments

Comments
 (0)