Skip to content

Commit 280e800

Browse files
committed
feat: initial commit
1 parent 80fef79 commit 280e800

23 files changed

+6507
-0
lines changed

.gitignore

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
# Created by https://www.gitignore.io/api/node
3+
# Edit at https://www.gitignore.io/?templates=node
4+
5+
### Node ###
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# Diagnostic reports (https://nodejs.org/api/report.html)
15+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Directory for instrumented libs generated by jscoverage/JSCover
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
coverage
28+
*.lcov
29+
30+
# nyc test coverage
31+
.nyc_output
32+
33+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
34+
.grunt
35+
36+
# Bower dependency directory (https://bower.io/)
37+
bower_components
38+
39+
# node-waf configuration
40+
.lock-wscript
41+
42+
# Compiled binary addons (https://nodejs.org/api/addons.html)
43+
build/Release
44+
45+
# Dependency directories
46+
node_modules/
47+
jspm_packages/
48+
49+
# TypeScript v1 declaration files
50+
typings/
51+
52+
# TypeScript cache
53+
*.tsbuildinfo
54+
55+
# Optional npm cache directory
56+
.npm
57+
58+
# Optional eslint cache
59+
.eslintcache
60+
61+
# Optional REPL history
62+
.node_repl_history
63+
64+
# Output of 'npm pack'
65+
*.tgz
66+
67+
# Yarn Integrity file
68+
.yarn-integrity
69+
70+
# dotenv environment variables file
71+
.env
72+
.env.test
73+
74+
# parcel-bundler cache (https://parceljs.org/)
75+
.cache
76+
77+
# next.js build output
78+
.next
79+
80+
# nuxt.js build output
81+
.nuxt
82+
83+
# react / gatsby
84+
public/
85+
86+
# vuepress build output
87+
.vuepress/dist
88+
89+
# Serverless directories
90+
.serverless/
91+
92+
# FuseBox cache
93+
.fusebox/
94+
95+
# DynamoDB Local files
96+
.dynamodb/
97+
98+
# End of https://www.gitignore.io/api/node

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Benchmark Comparison of TypeScript Runtime Type Support Modules
2+
3+
> Write up coming soon...
4+
5+
![][./bar-graph.svg]

bar-graph.svg

Lines changed: 1 addition & 0 deletions
Loading

benchmarks.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name,hz
2+
JSON Encode Decode,79966.79245582566
3+
runtypes,41973.09084616639
4+
io-ts,337073.6125561012
5+
class-validator sync,73694.89411739286
6+
class-validator async,61958.63602038928
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`caseClassValidatorAsync should fail min length test 1`] = `
4+
Array [
5+
ValidationError {
6+
"children": Array [],
7+
"constraints": Object {
8+
"minLength": "long_string must be longer than or equal to 100 characters",
9+
},
10+
"property": "long_string",
11+
"target": DataType {
12+
"boolean": true,
13+
"deeplyNested": DeeplyNestedType {
14+
"bool": false,
15+
"foo": "bar",
16+
"num": 1,
17+
},
18+
"long_string": "",
19+
"max_number": 1.7976931348623157e+308,
20+
"neg_number": -1,
21+
"number": 1,
22+
"string": "string",
23+
},
24+
"value": "",
25+
},
26+
]
27+
`;
28+
29+
exports[`caseClassValidatorAsync should fail negative number test 1`] = `
30+
Array [
31+
ValidationError {
32+
"children": Array [],
33+
"constraints": Object {
34+
"isNegative": "neg_number must be a negative number",
35+
},
36+
"property": "neg_number",
37+
"target": DataType {
38+
"boolean": true,
39+
"deeplyNested": DeeplyNestedType {
40+
"bool": false,
41+
"foo": "bar",
42+
"num": 1,
43+
},
44+
"long_string": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Vivendum intellegat et qui, ei denique consequuntur vix. Semper aeterno percipit ut his, sea ex utinam referrentur repudiandae. No epicuri hendrerit consetetur sit, sit dicta adipiscing ex, in facete detracto deterruisset duo. Quot populo ad qui. Sit fugit nostrum et. Ad per diam dicant interesset, lorem iusto sensibus ut sed. No dicam aperiam vis. Pri posse graeco definitiones cu, id eam populo quaestio adipiscing, usu quod malorum te. Ex nam agam veri, dicunt efficiantur ad qui, ad legere adversarium sit. Commune platonem mel id, brute adipiscing duo an. Vivendum intellegat et qui, ei denique consequuntur vix. Offendit eleifend moderatius ex vix, quem odio mazim et qui, purto expetendis cotidieque quo cu, veri persius vituperata ei nec. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
45+
"max_number": 1.7976931348623157e+308,
46+
"neg_number": 1,
47+
"number": 1,
48+
"string": "string",
49+
},
50+
"value": 1,
51+
},
52+
]
53+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`io-ts should fail min length test 1`] = `"Invalid"`;
4+
5+
exports[`io-ts should fail negative number test 1`] = `"Invalid"`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`runtypes should fail min length test 1`] = `"Failed constraint check"`;
4+
5+
exports[`runtypes should fail negative number test 1`] = `"Failed constraint check"`;

cases/class-validator.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { caseClassValidatorSync, caseClassValidatorAsync } from './class-validator'
2+
import { DATA } from '../data'
3+
4+
describe('caseClassValidatorSync', () => {
5+
it('should pass', () => {
6+
const res = caseClassValidatorSync(DATA)
7+
expect(res).toEqual(DATA)
8+
9+
if (res) {
10+
expect(res.number).toEqual(DATA.number)
11+
}
12+
})
13+
14+
it('should fail negative number test', () => {
15+
const req = Object.assign({}, DATA, { neg_number: 1 })
16+
expect(() => caseClassValidatorSync(req)).toThrow()
17+
})
18+
19+
it('should fail min length test', () => {
20+
const req = Object.assign({}, DATA, { long_string: '' })
21+
expect(() => caseClassValidatorSync(req)).toThrow()
22+
})
23+
})
24+
25+
describe('caseClassValidatorAsync', () => {
26+
it('should pass', async () => {
27+
const res = await caseClassValidatorAsync(DATA)
28+
expect(res).toEqual(DATA)
29+
30+
if (res) {
31+
expect(res.number).toEqual(DATA.number)
32+
}
33+
})
34+
35+
it('should fail negative number test', () => {
36+
const req = Object.assign({}, DATA, { neg_number: 1 })
37+
return expect(caseClassValidatorAsync(req)).rejects.toMatchSnapshot()
38+
})
39+
40+
it('should fail min length test', () => {
41+
const req = Object.assign({}, DATA, { long_string: '' })
42+
return expect(caseClassValidatorAsync(req)).rejects.toMatchSnapshot()
43+
})
44+
})

cases/class-validator.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'reflect-metadata'
2+
import { IsNegative, MinLength } from 'class-validator'
3+
import { Type } from 'class-transformer'
4+
import { transformAndValidate, transformAndValidateSync } from 'class-transformer-validator'
5+
import { Data } from '../data'
6+
7+
type DeeplyNested = Data['deeplyNested']
8+
9+
class DeeplyNestedType implements DeeplyNested {
10+
foo!: string
11+
num!: number
12+
bool!: boolean
13+
}
14+
15+
class DataType implements Data {
16+
number!: number
17+
18+
@IsNegative()
19+
neg_number!: number
20+
21+
max_number!: number
22+
string!: string
23+
24+
@MinLength(100)
25+
long_string!: string
26+
27+
boolean!:boolean
28+
29+
@Type(() => DeeplyNestedType)
30+
deeplyNested!: DeeplyNestedType
31+
}
32+
33+
export function caseClassValidatorSync(data: Data) {
34+
return transformAndValidateSync(DataType, data)
35+
}
36+
37+
export function caseClassValidatorAsync(data: Data) {
38+
return transformAndValidate(DataType, data)
39+
}

cases/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './json-encode-decode'
2+
export * from './runtypes'
3+
export * from './io-ts'
4+
export * from './class-validator'

0 commit comments

Comments
 (0)