Skip to content

Commit 9502fd2

Browse files
moltarRoman
authored andcommitted
feat: adds quartet package
Closes moltar#162
1 parent 673873b commit 9502fd2

16 files changed

+259
-211
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [marshal](https://github.com/marcj/marshal.ts)
1010
* [myzod](https://github.com/davidmdm/myzod)
1111
* [purify-ts](https://github.com/gigobyte/purify)
12+
* [quartet](https://github.com/whiteand/ts-quartet)
1213
* [runtypes](https://github.com/pelotom/runtypes)
1314
* [toi](https://github.com/hf/toi)
1415
* [ts-json-validator](https://github.com/ostrowr/ts-json-validator)
@@ -72,17 +73,18 @@ const res = isMyDataValid(data)
7273

7374
### Data Type Checks Sans `ts-json-validator`
7475

75-
Because `ts-json-validator` is so performant it makes it difficult to see the benchmarks for other packages.
76-
This benchmark run includes all of the above packages, but excludes `ts-json-validator`.
76+
Because some packages are so performant it makes it difficult to see the benchmarks for other packages.
7777

78-
#### Node 10.x ([JSON](./results/data-type-sans-ts-json-validator-10.x.json), [CSV](./results/data-type-sans-ts-json-validator-10.x.csv), [SVG](./results/data-type-sans-ts-json-validator-10.x.svg))
78+
This benchmark run includes all of the above packages, but excludes `ts-json-validator` and `quartet`.
7979

80-
![Bar Graph - Node 10.x](./results/data-type-sans-ts-json-validator-10.x.svg)
80+
#### Node 10.x ([JSON](./results/data-type-sans-outliers-10.x.json), [CSV](./results/data-type-sans-outliers-10.x.csv), [SVG](./results/data-type-sans-outliers-10.x.svg))
8181

82-
#### Node 12.x ([JSON](./results/data-type-sans-ts-json-validator-12.x.json), [CSV](./results/data-type-sans-ts-json-validator-12.x.csv), [SVG](./results/data-type-sans-ts-json-validator-12.x.svg))
82+
![Bar Graph - Node 10.x](./results/data-type-sans-outliers-10.x.svg)
8383

84-
![Bar Graph - Node 12.x](./results/data-type-sans-ts-json-validator-12.x.svg)
84+
#### Node 12.x ([JSON](./results/data-type-sans-outliers-12.x.json), [CSV](./results/data-type-sans-outliers-12.x.csv), [SVG](./results/data-type-sans-outliers-12.x.svg))
8585

86-
#### Node 13.x ([JSON](./results/data-type-sans-ts-json-validator-13.x.json), [CSV](./results/data-type-sans-ts-json-validator-13.x.csv), [SVG](./results/data-type-sans-ts-json-validator-13.x.svg))
86+
![Bar Graph - Node 12.x](./results/data-type-sans-outliers-12.x.svg)
8787

88-
![Bar Graph - Node 13.x](./results/data-type-sans-ts-json-validator-13.x.svg)
88+
#### Node 13.x ([JSON](./results/data-type-sans-outliers-13.x.json), [CSV](./results/data-type-sans-outliers-13.x.csv), [SVG](./results/data-type-sans-outliers-13.x.svg))
89+
90+
![Bar Graph - Node 13.x](./results/data-type-sans-outliers-13.x.svg)

cases/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { JsonDecoderCase } from './json-decoder';
88
import { MarshalCase } from './marshal';
99
import { MyzodCase } from './myzod';
1010
import { PurifyCase } from './purify-ts';
11+
import { QuartetCase } from './quartet';
1112
import { RuntypesCase } from './runtypes';
1213
import { ToiCase } from './toi';
1314
import { TsJsonValidatorCase } from './ts-json-validator';
@@ -24,6 +25,7 @@ export const cases = [
2425
MarshalCase,
2526
MyzodCase,
2627
PurifyCase,
28+
QuartetCase,
2729
RuntypesCase,
2830
ToiCase,
2931
TsJsonValidatorCase,

cases/quartet.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { v } from 'quartet';
2+
import { Case } from './abstract';
3+
import { Data } from '../data';
4+
5+
const checkData = v<Data>({
6+
number: v.safeInteger,
7+
negNumber: v.number,
8+
maxNumber: v.number,
9+
string: v.string,
10+
longString: v.string,
11+
boolean: v.boolean,
12+
deeplyNested: {
13+
foo: v.string,
14+
num: v.number,
15+
bool: v.boolean,
16+
},
17+
});
18+
19+
export class QuartetCase extends Case implements Case {
20+
name = 'quartet';
21+
22+
validate() {
23+
const { data } = this;
24+
25+
if (checkData(data)) {
26+
return data;
27+
}
28+
29+
throw new Error('Invalid');
30+
}
31+
}

index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DATA } from './data';
88
import { cases } from './cases';
99
import { Case } from './cases/abstract';
1010
import { TsJsonValidatorCase } from './cases/ts-json-validator';
11+
import { QuartetCase } from './cases/quartet';
1112

1213
const caseInstances: Case[] = cases.map(caseClass => new caseClass(DATA));
1314

@@ -16,7 +17,7 @@ const NODE_VERSION = process.env.NODE_VERSION || process.version;
1617

1718
async function main() {
1819
await suiteDataTypeValidation();
19-
await suiteDataTypeValidationSansTsJsonValidator();
20+
await suiteDataTypeValidationSansOutliers();
2021
}
2122
main();
2223

@@ -30,17 +31,17 @@ async function suiteDataTypeValidation() {
3031
}
3132

3233
/**
33-
* Benchmarking suite that performs data type validation only, but skips ts-json-validator
34-
* because it is such an outlier and it is difficult to look at the performance of other packages.
34+
* Benchmarking suite that performs data type validation only, but skips outliers
35+
* because it is difficult to look at the performance of other packages.
3536
*
3637
* https://en.wikipedia.org/wiki/Data_validation#Data-type_check
3738
*/
38-
async function suiteDataTypeValidationSansTsJsonValidator() {
39-
const cases = caseInstances.filter(
40-
caseInstance => !(caseInstance instanceof TsJsonValidatorCase)
41-
);
39+
async function suiteDataTypeValidationSansOutliers() {
40+
const cases = caseInstances
41+
.filter(caseInstance => !(caseInstance instanceof TsJsonValidatorCase))
42+
.filter(caseInstance => !(caseInstance instanceof QuartetCase));
4243

43-
await run('data-type-sans-ts-json-validator', cases, 'validate');
44+
await run('data-type-sans-outliers', cases, 'validate');
4445
}
4546

4647
async function run(name: string, cases: Case[], methodName: keyof Case) {

0 commit comments

Comments
 (0)