Skip to content

Commit 824bd88

Browse files
authored
Revision 0.31.1 (#533)
1 parent 18d1cf7 commit 824bd88

File tree

9 files changed

+229
-7
lines changed

9 files changed

+229
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sinclair/typebox",
3-
"version": "0.31.0",
3+
"version": "0.31.1",
44
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
55
"keywords": [
66
"typescript",

readme.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ type T = Static<typeof T> // type T = {
5858
5959
## Overview
6060
61-
TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime checked using standard Json Schema validation.
61+
TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript or runtime checked using standard Json Schema validation.
6262
63-
TypeBox is designed to be a runtime type system based on industry standard specifications for use in distributed systems. It offers serializable and publishable types as standard, a fully extensible type system capable of supporting multiple schema specifications, a high performance runtime validation compiler, various tools for working with dynamic data and offers detailed structured error reporting.
64-
65-
TypeBox can be used as a simple tool to build up complex schemas or integrated into applications to enable high performance runtime validation for data received over the wire.
63+
TypeBox is designed to be a runtime type system based on industry standard specifications. It offers serializable and publishable types as standard, a fully extensible type system capable of supporting multiple schema specifications, a high performance runtime validation compiler, various tools for working with dynamic data and offers detailed structured error reporting. It can either be used as a simple tool to build up complex schemas or integrated into applications to enable high performance runtime validation for data received over the wire.
6664
6765
License MIT
6866

src/value/transform.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export namespace HasTransform {
7676
return Types.TypeGuard.TTransform(schema) || Visit(schema.items, references)
7777
}
7878
function TConstructor(schema: Types.TConstructor, references: Types.TSchema[]) {
79-
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema.returns, references))
79+
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references))
8080
}
8181
function TFunction(schema: Types.TFunction, references: Types.TSchema[]) {
82-
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema.returns, references))
82+
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references))
8383
}
8484
function TIntersect(schema: Types.TIntersect, references: Types.TSchema[]) {
8585
return Types.TypeGuard.TTransform(schema) || Types.TypeGuard.TTransform(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references))

test/runtime/compiler/constructor.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Type } from '@sinclair/typebox'
2+
import { Ok, Fail } from './validate'
3+
4+
describe('compiler/Constructor', () => {
5+
it('Should validate constructor 1', () => {
6+
const T = Type.Constructor([], Type.Object({}))
7+
Ok(T, class {})
8+
})
9+
it('Should validate constructor 2', () => {
10+
const T = Type.Constructor([Type.Number()], Type.Object({}))
11+
// note: constructor arguments are non-checkable
12+
Ok(T, class {})
13+
})
14+
it('Should validate constructor 3', () => {
15+
const T = Type.Constructor(
16+
[Type.Number()],
17+
Type.Object({
18+
method: Type.Function([], Type.Void()),
19+
}),
20+
)
21+
Ok(
22+
T,
23+
class {
24+
method() {}
25+
},
26+
)
27+
})
28+
it('Should validate constructor 4', () => {
29+
const T = Type.Constructor(
30+
[Type.Number()],
31+
Type.Object({
32+
x: Type.Number(),
33+
y: Type.Number(),
34+
z: Type.Number(),
35+
}),
36+
)
37+
Ok(
38+
T,
39+
class {
40+
get x() {
41+
return 1
42+
}
43+
get y() {
44+
return 1
45+
}
46+
get z() {
47+
return 1
48+
}
49+
},
50+
)
51+
})
52+
it('Should not validate constructor 1', () => {
53+
const T = Type.Constructor([Type.Number()], Type.Object({}))
54+
Fail(T, 1)
55+
})
56+
it('Should not validate constructor 2', () => {
57+
const T = Type.Constructor(
58+
[Type.Number()],
59+
Type.Object({
60+
x: Type.Number(),
61+
y: Type.Number(),
62+
z: Type.Number(),
63+
}),
64+
)
65+
Fail(
66+
T,
67+
class {
68+
get x() {
69+
return null
70+
}
71+
get y() {
72+
return null
73+
}
74+
get z() {
75+
return null
76+
}
77+
},
78+
)
79+
})
80+
})

test/runtime/compiler/function.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Type } from '@sinclair/typebox'
2+
import { Ok, Fail } from './validate'
3+
4+
describe('compiler/Function', () => {
5+
it('Should validate function 1', () => {
6+
const T = Type.Function([Type.Number()], Type.Number())
7+
Ok(T, function () {})
8+
})
9+
it('Should validate function 2', () => {
10+
const T = Type.Function([Type.Number()], Type.Number())
11+
// note: validation only checks typeof 'function'
12+
Ok(T, function (a: string, b: string, c: string, d: string) {})
13+
})
14+
it('Should validate function 3', () => {
15+
const T = Type.Function([Type.Number()], Type.Number())
16+
// note: validation only checks typeof 'function'
17+
Ok(T, function () {
18+
return 'not-a-number'
19+
})
20+
})
21+
it('Should not validate function', () => {
22+
const T = Type.Function([Type.Number()], Type.Number())
23+
Fail(T, 1)
24+
})
25+
})

test/runtime/compiler/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import './async-iterator'
44
import './bigint'
55
import './boolean'
66
import './composite'
7+
import './constructor'
78
import './date'
89
import './unicode'
910
import './enum'
11+
import './function'
1012
import './integer'
1113
import './intersect'
1214
import './iterator'
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Value } from '@sinclair/typebox/value'
2+
import { Type } from '@sinclair/typebox'
3+
import { Assert } from '../../assert/index'
4+
5+
describe('value/check/Constructor', () => {
6+
it('Should validate constructor 1', () => {
7+
const T = Type.Constructor([], Type.Object({}))
8+
Assert.IsTrue(Value.Check(T, class {}))
9+
})
10+
it('Should validate constructor 2', () => {
11+
const T = Type.Constructor([Type.Number()], Type.Object({}))
12+
// note: constructor arguments are non-checkable
13+
Assert.IsTrue(Value.Check(T, class {}))
14+
})
15+
it('Should validate constructor 3', () => {
16+
const T = Type.Constructor(
17+
[Type.Number()],
18+
Type.Object({
19+
method: Type.Function([], Type.Void()),
20+
}),
21+
)
22+
Assert.IsTrue(
23+
Value.Check(
24+
T,
25+
class {
26+
method() {}
27+
},
28+
),
29+
)
30+
})
31+
it('Should validate constructor 4', () => {
32+
const T = Type.Constructor(
33+
[Type.Number()],
34+
Type.Object({
35+
x: Type.Number(),
36+
y: Type.Number(),
37+
z: Type.Number(),
38+
}),
39+
)
40+
Assert.IsTrue(
41+
Value.Check(
42+
T,
43+
class {
44+
get x() {
45+
return 1
46+
}
47+
get y() {
48+
return 1
49+
}
50+
get z() {
51+
return 1
52+
}
53+
},
54+
),
55+
)
56+
})
57+
it('Should not validate constructor 1', () => {
58+
const T = Type.Constructor([Type.Number()], Type.Object({}))
59+
Assert.IsFalse(Value.Check(T, 1))
60+
})
61+
it('Should not validate constructor 2', () => {
62+
const T = Type.Constructor(
63+
[Type.Number()],
64+
Type.Object({
65+
x: Type.Number(),
66+
y: Type.Number(),
67+
z: Type.Number(),
68+
}),
69+
)
70+
Assert.IsFalse(
71+
Value.Check(
72+
T,
73+
class {
74+
get x() {
75+
return null
76+
}
77+
get y() {
78+
return null
79+
}
80+
get z() {
81+
return null
82+
}
83+
},
84+
),
85+
)
86+
})
87+
})

test/runtime/value/check/function.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Value } from '@sinclair/typebox/value'
2+
import { Type } from '@sinclair/typebox'
3+
import { Assert } from '../../assert/index'
4+
5+
describe('value/check/Function', () => {
6+
it('Should validate function 1', () => {
7+
const T = Type.Function([Type.Number()], Type.Number())
8+
Assert.IsTrue(Value.Check(T, function () {}))
9+
})
10+
it('Should validate function 2', () => {
11+
const T = Type.Function([Type.Number()], Type.Number())
12+
// note: validation only checks typeof 'function'
13+
Assert.IsTrue(Value.Check(T, function (a: string, b: string, c: string, d: string) {}))
14+
})
15+
it('Should validate function 3', () => {
16+
const T = Type.Function([Type.Number()], Type.Number())
17+
// note: validation only checks typeof 'function'
18+
Assert.IsTrue(
19+
Value.Check(T, function () {
20+
return 'not-a-number'
21+
}),
22+
)
23+
})
24+
it('Should not validate function', () => {
25+
const T = Type.Function([Type.Number()], Type.Number())
26+
Assert.IsFalse(Value.Check(T, 1))
27+
})
28+
})

test/runtime/value/check/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import './async-iterator'
44
import './bigint'
55
import './boolean'
66
import './composite'
7+
import './constructor'
78
import './date'
89
import './enum'
10+
import './function'
911
import './integer'
1012
import './intersect'
1113
import './iterator'

0 commit comments

Comments
 (0)