Skip to content

Commit

Permalink
Revision 0.31.3 (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 authored Aug 25, 2023
1 parent 0762bd3 commit 6e0b999
Show file tree
Hide file tree
Showing 42 changed files with 196 additions and 88 deletions.
2 changes: 1 addition & 1 deletion hammer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function benchmark() {
// Test
// -------------------------------------------------------------------------------
export async function test_typescript() {
for (const version of ['4.9.5', '5.0.4', '5.1.3', '5.1.6', 'next', 'latest']) {
for (const version of ['4.9.5', '5.0.4', '5.1.3', '5.1.6', '5.2.2', 'next', 'latest']) {
await shell(`npm install typescript@${version} --no-save`)
await test_static()
}
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.2",
"version": "0.31.3",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down Expand Up @@ -59,6 +59,6 @@
"ajv-formats": "^2.1.1",
"mocha": "^9.2.2",
"prettier": "^2.7.1",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
}
}
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type T = Static<typeof T> // type T = {
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 built upon industry standard specifications. It offers reflectable, serializable and publishable types as standard, a fully extensible type system capable of supporting multiple schema specifications, includes a high performance validation compiler, offers various tools for working with dynamic data and provides detailed structured error reporting.
TypeBox types are designed to express industry standard schematics as TypeScript types. All types are runtime reflectable, serializable and publishable by default. It includes an extensible type system able to represent type safe schematics for multiple schema specifications. It also provides a high performance 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.
Expand Down Expand Up @@ -949,7 +949,7 @@ const U = Type.Union(R) // const U = {
### Transform Types
TypeBox supports bi-directional decode and encode with Transform types. These types are designed to operate with the Value and TypeCompiler Encode and Decode functions. Transform types are useful to convert Json encoded values into constructs more natural to JavaScript. The following creates a Transform type to convert between Date and number using the Value module.
TypeBox supports value decoding and encoding with Transform types. These types work in tandem with the Encode and Decode functions available on the Value and TypeCompiler modules. Transform types can be used to convert Json encoded values into constructs more natural to JavaScript. The following creates a Transform type to decode numbers into Dates using the Value module.
```typescript
import { Value } from '@sinclair/typebox/value'
Expand Down Expand Up @@ -1604,13 +1604,16 @@ The following is a list of community packages that offer general tooling, extend
| Package | Description |
| ------------- | ------------- |
| [drizzle-typebox](https://www.npmjs.com/package/drizzle-typebox) | Generates TypeBox types from Drizzle ORM schemas |
| [elysia](https://github.com/elysiajs/elysia) | Fast and friendly Bun web framework |
| [fastify-type-provider-typebox](https://github.com/fastify/fastify-type-provider-typebox) | Fastify TypeBox integration with the Fastify Type Provider |
| [feathersjs](https://github.com/feathersjs/feathers) | The API and real-time application framework |
| [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox |
| [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | Schema validation utilities for h3 using TypeBox & Ajv |
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
| [typebox-client](https://github.com/flodlc/typebox-client) | Type safe http client library for Fastify |
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
| [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
<a name='benchmark'></a>
Expand Down
22 changes: 20 additions & 2 deletions test/runtime/compiler/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('compiler/Record', () => {
const R = Type.Record(Type.KeyOf(T), Type.Number(), { additionalProperties: false })
Fail(R, { a: 1, b: 2, c: 3, d: 4 })
})
it('Should should validate when specifying regular expressions', () => {
it('Should validate when specifying regular expressions', () => {
const K = Type.RegExp(/^op_.*$/)
const T = Type.Record(K, Type.Number())
Ok(T, {
Expand All @@ -94,7 +94,7 @@ describe('compiler/Record', () => {
op_c: 3,
})
})
it('Should should not validate when specifying regular expressions and passing invalid property', () => {
it('Should not validate when specifying regular expressions and passing invalid property', () => {
const K = Type.RegExp(/^op_.*$/)
const T = Type.Record(K, Type.Number(), { additionalProperties: false })
Fail(T, {
Expand All @@ -103,6 +103,24 @@ describe('compiler/Record', () => {
aop_c: 3,
})
})
it('Should validate with quoted string pattern', () => {
const K = Type.String({ pattern: "'(a|b|c)" })
const T = Type.Record(K, Type.Number())
Ok(T, {
"'a": 1,
"'b": 2,
"'c": 3,
})
})
it('Should validate with forward-slash pattern', () => {
const K = Type.String({ pattern: '/(a|b|c)' })
const T = Type.Record(K, Type.Number())
Ok(T, {
'/a': 1,
'/b': 2,
'/c': 3,
})
})
// ------------------------------------------------------------
// Integer Keys
// ------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './compiler/index'
import './errors/index'
import './schema/index'
import './errors/index'
import './system/index'
import './type/index'
import './value/index'
2 changes: 1 addition & 1 deletion test/runtime/schema/any.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok } from './validate'

describe('type/schema/Any', () => {
describe('compiler-ajv/Any', () => {
it('Should validate number', () => {
const T = Type.Any()
Ok(T, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Array', () => {
describe('compiler-ajv/Array', () => {
it('Should validate an array of any', () => {
const T = Type.Array(Type.Any())
Ok(T, [0, true, 'hello', {}])
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Boolean', () => {
describe('compiler-ajv/Boolean', () => {
it('Should validate a boolean', () => {
const T = Type.Boolean()
Ok(T, true)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type } from '@sinclair/typebox'
import { Assert } from '../assert'
import { Ok, Fail } from './validate'

describe('type/schema/Composite', () => {
describe('compiler-ajv/Composite', () => {
it('Should compose two objects', () => {
const A = Type.Object({ a: Type.String() })
const B = Type.Object({ b: Type.Number() })
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// which are configured to use Value.Check()
// ----------------------------------------------------

// describe('type/schema/Date', () => {
// describe('compiler-ajv/Date', () => {
// it('Should not validate number', () => {
// const T = Type.Date()
// Fail(T, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/enum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Enum', () => {
describe('compiler-ajv/Enum', () => {
it('Should validate when emum uses default numeric values', () => {
enum Kind {
Foo, // = 0
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/integer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Integer', () => {
describe('compiler-ajv/Integer', () => {
it('Should not validate number', () => {
const T = Type.Integer()
Fail(T, 3.14)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/intersect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, Static } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Intersect', () => {
describe('compiler-ajv/Intersect', () => {
it('Should intersect number and number', () => {
const A = Type.Number()
const B = Type.Number()
Expand Down
3 changes: 1 addition & 2 deletions test/runtime/schema/keyof.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'
import { strictEqual } from 'assert'

describe('type/schema/KeyOf', () => {
describe('compiler-ajv/KeyOf', () => {
it('Should validate with all object keys as a kind of union', () => {
const T = Type.KeyOf(
Type.Object({
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/literal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Literal', () => {
describe('compiler-ajv/Literal', () => {
it('Should validate literal number', () => {
const T = Type.Literal(42)
Ok(T, 42)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/never.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Fail } from './validate'

describe('type/schema/Never', () => {
describe('compiler-ajv/Never', () => {
it('Should not validate number', () => {
const T = Type.Never()
Fail(T, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/not.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Not', () => {
describe('compiler-ajv/Not', () => {
it('Should validate not number', () => {
const T = Type.Not(Type.Number())
Fail(T, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/null.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Null', () => {
describe('compiler-ajv/Null', () => {
it('Should not validate number', () => {
const T = Type.Null()
Fail(T, 1)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/number.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Number', () => {
describe('compiler-ajv/Number', () => {
it('Should validate number', () => {
const T = Type.Number()
Ok(T, 3.14)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'

describe('type/schema/Object', () => {
describe('compiler-ajv/Object', () => {
it('Should not validate a number', () => {
const T = Type.Object({})
Fail(T, 42)
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'
import { strictEqual } from 'assert'

describe('type/schema/Omit', () => {
describe('compiler-ajv/Omit', () => {
it('Should omit properties on the source schema', () => {
const A = Type.Object(
{
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/schema/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { strictEqual } from 'assert'
import { Type } from '@sinclair/typebox'
import { Ok } from './validate'

describe('type/schema/Optional', () => {
describe('compiler-ajv/Optional', () => {
it('Should validate object with optional', () => {
const T = Type.Object(
{
Expand Down
22 changes: 11 additions & 11 deletions test/runtime/schema/partial.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type, Readonly, Optional } from '@sinclair/typebox'
import { Ok, Fail } from './validate'
import { strictEqual } from 'assert'
import { Ok } from './validate'
import { Assert } from '../assert'

describe('type/schema/Partial', () => {
describe('compiler-ajv/Partial', () => {
it('Should convert a required object into a partial.', () => {
const A = Type.Object(
{
Expand All @@ -29,12 +29,12 @@ describe('type/schema/Partial', () => {
{ additionalProperties: false },
)
const T = Type.Partial(A)
strictEqual(T.properties.x[Readonly], 'Readonly')
strictEqual(T.properties.x[Optional], 'Optional')
strictEqual(T.properties.y[Readonly], 'Readonly')
strictEqual(T.properties.y[Optional], 'Optional')
strictEqual(T.properties.z[Optional], 'Optional')
strictEqual(T.properties.w[Optional], 'Optional')
Assert.IsEqual(T.properties.x[Readonly], 'Readonly')
Assert.IsEqual(T.properties.x[Optional], 'Optional')
Assert.IsEqual(T.properties.y[Readonly], 'Readonly')
Assert.IsEqual(T.properties.y[Optional], 'Optional')
Assert.IsEqual(T.properties.z[Optional], 'Optional')
Assert.IsEqual(T.properties.w[Optional], 'Optional')
})
it('Should inherit options from the source object', () => {
const A = Type.Object(
Expand All @@ -46,7 +46,7 @@ describe('type/schema/Partial', () => {
{ additionalProperties: false },
)
const T = Type.Partial(A)
strictEqual(A.additionalProperties, false)
strictEqual(T.additionalProperties, false)
Assert.IsEqual(A.additionalProperties, false)
Assert.IsEqual(T.additionalProperties, false)
})
})
12 changes: 6 additions & 6 deletions test/runtime/schema/pick.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type } from '@sinclair/typebox'
import { Ok, Fail } from './validate'
import { strictEqual } from 'assert'
import { Assert } from '../assert'

describe('type/schema/Pick', () => {
describe('compiler-ajv/Pick', () => {
it('Should pick properties from the source schema', () => {
const Vector3 = Type.Object(
{
Expand All @@ -25,7 +25,7 @@ describe('type/schema/Pick', () => {
{ additionalProperties: false },
)
const T = Type.Pick(A, ['x', 'y'])
strictEqual(T.required!.includes('z'), false)
Assert.IsEqual(T.required!.includes('z'), false)
})
it('Should delete the required property if no required properties remain', () => {
const A = Type.Object(
Expand All @@ -37,7 +37,7 @@ describe('type/schema/Pick', () => {
{ additionalProperties: false },
)
const T = Type.Pick(A, ['x', 'y'])
strictEqual(T.required, undefined)
Assert.IsEqual(T.required, undefined)
})
it('Should inherit options from the source object', () => {
const A = Type.Object(
Expand All @@ -49,8 +49,8 @@ describe('type/schema/Pick', () => {
{ additionalProperties: false },
)
const T = Type.Pick(A, ['x', 'y'])
strictEqual(A.additionalProperties, false)
strictEqual(T.additionalProperties, false)
Assert.IsEqual(A.additionalProperties, false)
Assert.IsEqual(T.additionalProperties, false)
})
it('Should pick with keyof object', () => {
const A = Type.Object({
Expand Down
Loading

0 comments on commit 6e0b999

Please sign in to comment.