Skip to content

Commit

Permalink
Revision 0.31.1 (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 authored Aug 14, 2023
1 parent 18d1cf7 commit 824bd88
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.0",
"version": "0.31.1",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
6 changes: 2 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ type T = Static<typeof T> // type T = {
## Overview
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.
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.
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.
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.
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.
License MIT
Expand Down
4 changes: 2 additions & 2 deletions src/value/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ export namespace HasTransform {
return Types.TypeGuard.TTransform(schema) || Visit(schema.items, references)
}
function TConstructor(schema: Types.TConstructor, references: Types.TSchema[]) {
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema.returns, references))
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references))
}
function TFunction(schema: Types.TFunction, references: Types.TSchema[]) {
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema.returns, references))
return Types.TypeGuard.TTransform(schema) || Visit(schema.returns, references) || schema.parameters.some((schema) => Visit(schema, references))
}
function TIntersect(schema: Types.TIntersect, references: Types.TSchema[]) {
return Types.TypeGuard.TTransform(schema) || Types.TypeGuard.TTransform(schema.unevaluatedProperties) || schema.allOf.some((schema) => Visit(schema, references))
Expand Down
80 changes: 80 additions & 0 deletions test/runtime/compiler/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('compiler/Constructor', () => {
it('Should validate constructor 1', () => {
const T = Type.Constructor([], Type.Object({}))
Ok(T, class {})
})
it('Should validate constructor 2', () => {
const T = Type.Constructor([Type.Number()], Type.Object({}))
// note: constructor arguments are non-checkable
Ok(T, class {})
})
it('Should validate constructor 3', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
method: Type.Function([], Type.Void()),
}),
)
Ok(
T,
class {
method() {}
},
)
})
it('Should validate constructor 4', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
}),
)
Ok(
T,
class {
get x() {
return 1
}
get y() {
return 1
}
get z() {
return 1
}
},
)
})
it('Should not validate constructor 1', () => {
const T = Type.Constructor([Type.Number()], Type.Object({}))
Fail(T, 1)
})
it('Should not validate constructor 2', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
}),
)
Fail(
T,
class {
get x() {
return null
}
get y() {
return null
}
get z() {
return null
}
},
)
})
})
25 changes: 25 additions & 0 deletions test/runtime/compiler/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('compiler/Function', () => {
it('Should validate function 1', () => {
const T = Type.Function([Type.Number()], Type.Number())
Ok(T, function () {})
})
it('Should validate function 2', () => {
const T = Type.Function([Type.Number()], Type.Number())
// note: validation only checks typeof 'function'
Ok(T, function (a: string, b: string, c: string, d: string) {})
})
it('Should validate function 3', () => {
const T = Type.Function([Type.Number()], Type.Number())
// note: validation only checks typeof 'function'
Ok(T, function () {
return 'not-a-number'
})
})
it('Should not validate function', () => {
const T = Type.Function([Type.Number()], Type.Number())
Fail(T, 1)
})
})
2 changes: 2 additions & 0 deletions test/runtime/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import './async-iterator'
import './bigint'
import './boolean'
import './composite'
import './constructor'
import './date'
import './unicode'
import './enum'
import './function'
import './integer'
import './intersect'
import './iterator'
Expand Down
87 changes: 87 additions & 0 deletions test/runtime/value/check/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/check/Constructor', () => {
it('Should validate constructor 1', () => {
const T = Type.Constructor([], Type.Object({}))
Assert.IsTrue(Value.Check(T, class {}))
})
it('Should validate constructor 2', () => {
const T = Type.Constructor([Type.Number()], Type.Object({}))
// note: constructor arguments are non-checkable
Assert.IsTrue(Value.Check(T, class {}))
})
it('Should validate constructor 3', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
method: Type.Function([], Type.Void()),
}),
)
Assert.IsTrue(
Value.Check(
T,
class {
method() {}
},
),
)
})
it('Should validate constructor 4', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
}),
)
Assert.IsTrue(
Value.Check(
T,
class {
get x() {
return 1
}
get y() {
return 1
}
get z() {
return 1
}
},
),
)
})
it('Should not validate constructor 1', () => {
const T = Type.Constructor([Type.Number()], Type.Object({}))
Assert.IsFalse(Value.Check(T, 1))
})
it('Should not validate constructor 2', () => {
const T = Type.Constructor(
[Type.Number()],
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
}),
)
Assert.IsFalse(
Value.Check(
T,
class {
get x() {
return null
}
get y() {
return null
}
get z() {
return null
}
},
),
)
})
})
28 changes: 28 additions & 0 deletions test/runtime/value/check/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/check/Function', () => {
it('Should validate function 1', () => {
const T = Type.Function([Type.Number()], Type.Number())
Assert.IsTrue(Value.Check(T, function () {}))
})
it('Should validate function 2', () => {
const T = Type.Function([Type.Number()], Type.Number())
// note: validation only checks typeof 'function'
Assert.IsTrue(Value.Check(T, function (a: string, b: string, c: string, d: string) {}))
})
it('Should validate function 3', () => {
const T = Type.Function([Type.Number()], Type.Number())
// note: validation only checks typeof 'function'
Assert.IsTrue(
Value.Check(T, function () {
return 'not-a-number'
}),
)
})
it('Should not validate function', () => {
const T = Type.Function([Type.Number()], Type.Number())
Assert.IsFalse(Value.Check(T, 1))
})
})
2 changes: 2 additions & 0 deletions test/runtime/value/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import './async-iterator'
import './bigint'
import './boolean'
import './composite'
import './constructor'
import './date'
import './enum'
import './function'
import './integer'
import './intersect'
import './iterator'
Expand Down

0 comments on commit 824bd88

Please sign in to comment.