@sinclair/typebox 0.31.1 → 0.31.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.js +12 -2
- package/package.json +2 -2
- package/readme.md +11 -6
- package/typebox.d.ts +9 -8
package/compiler/compiler.js
CHANGED
|
@@ -145,6 +145,16 @@ var Identifier;
|
|
|
145
145
|
Identifier.Encode = Encode;
|
|
146
146
|
})(Identifier || (Identifier = {}));
|
|
147
147
|
// -------------------------------------------------------------------
|
|
148
|
+
// LiteralString
|
|
149
|
+
// -------------------------------------------------------------------
|
|
150
|
+
var LiteralString;
|
|
151
|
+
(function (LiteralString) {
|
|
152
|
+
function Escape(content) {
|
|
153
|
+
return content.replace(/'/g, "\\'");
|
|
154
|
+
}
|
|
155
|
+
LiteralString.Escape = Escape;
|
|
156
|
+
})(LiteralString || (LiteralString = {}));
|
|
157
|
+
// -------------------------------------------------------------------
|
|
148
158
|
// Errors
|
|
149
159
|
// -------------------------------------------------------------------
|
|
150
160
|
class TypeCompilerUnknownTypeError extends Types.TypeBoxError {
|
|
@@ -303,7 +313,7 @@ var TypeCompiler;
|
|
|
303
313
|
yield `(${value} === ${schema.const})`;
|
|
304
314
|
}
|
|
305
315
|
else {
|
|
306
|
-
yield `(${value} === '${schema.const}')`;
|
|
316
|
+
yield `(${value} === '${LiteralString.Escape(schema.const)}')`;
|
|
307
317
|
}
|
|
308
318
|
}
|
|
309
319
|
function* TNever(schema, references, value) {
|
|
@@ -374,7 +384,7 @@ var TypeCompiler;
|
|
|
374
384
|
if ((0, guard_1.IsNumber)(schema.maxProperties))
|
|
375
385
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
376
386
|
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
377
|
-
const variable = CreateVariable(
|
|
387
|
+
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
378
388
|
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
379
389
|
const check2 = Types.TypeGuard.TSchema(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
380
390
|
const expression = `(${variable}.test(key) ? ${check1} : ${check2})`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.31.
|
|
3
|
+
"version": "0.31.3",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -59,6 +59,6 @@
|
|
|
59
59
|
"ajv-formats": "^2.1.1",
|
|
60
60
|
"mocha": "^9.2.2",
|
|
61
61
|
"prettier": "^2.7.1",
|
|
62
|
-
"typescript": "^5.
|
|
62
|
+
"typescript": "^5.2.2"
|
|
63
63
|
}
|
|
64
64
|
}
|
package/readme.md
CHANGED
|
@@ -60,7 +60,9 @@ type T = Static<typeof T> // type T = {
|
|
|
60
60
|
|
|
61
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.
|
|
62
62
|
|
|
63
|
-
TypeBox
|
|
63
|
+
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.
|
|
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.
|
|
64
66
|
|
|
65
67
|
License MIT
|
|
66
68
|
|
|
@@ -947,7 +949,7 @@ const U = Type.Union(R) // const U = {
|
|
|
947
949
|
|
|
948
950
|
### Transform Types
|
|
949
951
|
|
|
950
|
-
TypeBox supports
|
|
952
|
+
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.
|
|
951
953
|
|
|
952
954
|
```typescript
|
|
953
955
|
import { Value } from '@sinclair/typebox/value'
|
|
@@ -1167,9 +1169,9 @@ const Z = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const Z = { x: 1, y: 2 }
|
|
|
1167
1169
|
Use the Decode function to decode a value from a type, or throw if the value is invalid. The return value will infer as the decoded type. This function will run Transform codecs if available.
|
|
1168
1170
|
|
|
1169
1171
|
```typescript
|
|
1170
|
-
const A =
|
|
1172
|
+
const A = Value.Decode(Type.String(), 'hello') // const A = 'hello'
|
|
1171
1173
|
|
|
1172
|
-
const B =
|
|
1174
|
+
const B = Value.Decode(Type.String(), 42) // throw
|
|
1173
1175
|
```
|
|
1174
1176
|
<a name='values-decode'></a>
|
|
1175
1177
|
|
|
@@ -1178,9 +1180,9 @@ const B = Type.Decode(Type.String(), 42) // throw
|
|
|
1178
1180
|
Use the Encode function to encode a value to a type, or throw if the value is invalid. The return value will infer as the encoded type. This function will run Transform codecs if available.
|
|
1179
1181
|
|
|
1180
1182
|
```typescript
|
|
1181
|
-
const A =
|
|
1183
|
+
const A = Value.Encode(Type.String(), 'hello') // const A = 'hello'
|
|
1182
1184
|
|
|
1183
|
-
const B =
|
|
1185
|
+
const B = Value.Encode(Type.String(), 42) // throw
|
|
1184
1186
|
```
|
|
1185
1187
|
|
|
1186
1188
|
<a name='values-equal'></a>
|
|
@@ -1602,13 +1604,16 @@ The following is a list of community packages that offer general tooling, extend
|
|
|
1602
1604
|
|
|
1603
1605
|
| Package | Description |
|
|
1604
1606
|
| ------------- | ------------- |
|
|
1607
|
+
| [drizzle-typebox](https://www.npmjs.com/package/drizzle-typebox) | Generates TypeBox types from Drizzle ORM schemas |
|
|
1605
1608
|
| [elysia](https://github.com/elysiajs/elysia) | Fast and friendly Bun web framework |
|
|
1606
1609
|
| [fastify-type-provider-typebox](https://github.com/fastify/fastify-type-provider-typebox) | Fastify TypeBox integration with the Fastify Type Provider |
|
|
1607
1610
|
| [feathersjs](https://github.com/feathersjs/feathers) | The API and real-time application framework |
|
|
1608
1611
|
| [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox |
|
|
1612
|
+
| [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | Schema validation utilities for h3 using TypeBox & Ajv |
|
|
1609
1613
|
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
|
|
1610
1614
|
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
|
|
1611
1615
|
| [typebox-client](https://github.com/flodlc/typebox-client) | Type safe http client library for Fastify |
|
|
1616
|
+
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
|
|
1612
1617
|
| [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
|
|
1613
1618
|
|
|
1614
1619
|
<a name='benchmark'></a>
|
package/typebox.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ export declare const Kind: unique symbol;
|
|
|
6
6
|
export declare const PatternBoolean = "(true|false)";
|
|
7
7
|
export declare const PatternNumber = "(0|[1-9][0-9]*)";
|
|
8
8
|
export declare const PatternString = "(.*)";
|
|
9
|
-
export declare const PatternBooleanExact
|
|
10
|
-
export declare const PatternNumberExact
|
|
11
|
-
export declare const PatternStringExact
|
|
9
|
+
export declare const PatternBooleanExact = "^(true|false)$";
|
|
10
|
+
export declare const PatternNumberExact = "^(0|[1-9][0-9]*)$";
|
|
11
|
+
export declare const PatternStringExact = "^(.*)$";
|
|
12
12
|
export type TupleToIntersect<T extends any[]> = T extends [infer I] ? I : T extends [infer I, ...infer R] ? I & TupleToIntersect<R> : never;
|
|
13
13
|
export type TupleToUnion<T extends any[]> = {
|
|
14
14
|
[K in keyof T]: T[K];
|
|
@@ -436,11 +436,12 @@ export interface TTemplateLiteral<T extends TTemplateLiteralKind[] = TTemplateLi
|
|
|
436
436
|
type: 'string';
|
|
437
437
|
pattern: string;
|
|
438
438
|
}
|
|
439
|
-
export type
|
|
440
|
-
|
|
439
|
+
export type DecodeModifier<D extends TSchema, T extends TSchema> = T extends TReadonly<T> & TOptional<T> ? TReadonly<TOptional<D>> : T extends TReadonly<T> ? TReadonly<D> : T extends TOptional<T> ? TOptional<D> : D;
|
|
440
|
+
export type DecodeProperties<T extends TProperties> = {
|
|
441
|
+
[K in keyof T]: DecodeType<T[K]>;
|
|
441
442
|
};
|
|
442
|
-
export type
|
|
443
|
-
export type
|
|
443
|
+
export type DecodeRest<T extends TSchema[]> = T extends [infer L, ...infer R] ? [DecodeType<AssertType<L>>, ...DecodeRest<AssertRest<R>>] : [];
|
|
444
|
+
export type DecodeType<T extends TSchema> = T extends TTransform<infer _, infer R> ? DecodeModifier<TUnsafe<R>, T> : T extends TArray<infer S> ? DecodeModifier<TArray<DecodeType<S>>, T> : T extends TAsyncIterator<infer S> ? DecodeModifier<TAsyncIterator<DecodeType<S>>, T> : T extends TConstructor<infer P, infer R> ? DecodeModifier<TConstructor<AssertRest<DecodeRest<P>>, DecodeType<R>>, T> : T extends TFunction<infer P, infer R> ? DecodeModifier<TFunction<AssertRest<DecodeRest<P>>, DecodeType<R>>, T> : T extends TIntersect<infer S> ? DecodeModifier<TIntersect<AssertRest<DecodeRest<S>>>, T> : T extends TIterator<infer S> ? DecodeModifier<TIterator<DecodeType<S>>, T> : T extends TNot<infer S> ? DecodeModifier<TNot<DecodeType<S>>, T> : T extends TObject<infer S> ? DecodeModifier<TObject<Evaluate<DecodeProperties<S>>>, T> : T extends TPromise<infer S> ? DecodeModifier<TPromise<DecodeType<S>>, T> : T extends TRecord<infer K, infer S> ? DecodeModifier<TRecord<K, DecodeType<S>>, T> : T extends TRecursive<infer S> ? DecodeModifier<TRecursive<DecodeType<S>>, T> : T extends TRef<infer S> ? DecodeModifier<TRef<DecodeType<S>>, T> : T extends TTuple<infer S> ? DecodeModifier<TTuple<AssertRest<DecodeRest<S>>>, T> : T extends TUnion<infer S> ? DecodeModifier<TUnion<AssertRest<DecodeRest<S>>>, T> : T;
|
|
444
445
|
export type TransformFunction<T = any, U = any> = (value: T) => U;
|
|
445
446
|
export interface TransformOptions<I extends TSchema = TSchema, O extends unknown = unknown> {
|
|
446
447
|
Decode: TransformFunction<StaticDecode<I>, O>;
|
|
@@ -507,7 +508,7 @@ export interface TVoid extends TSchema {
|
|
|
507
508
|
type: 'void';
|
|
508
509
|
}
|
|
509
510
|
/** Creates the decoded static form for a TypeBox type */
|
|
510
|
-
export type StaticDecode<T extends TSchema, P extends unknown[] = []> = Static<
|
|
511
|
+
export type StaticDecode<T extends TSchema, P extends unknown[] = []> = Static<DecodeType<T>, P>;
|
|
511
512
|
/** Creates the encoded static form for a TypeBox type */
|
|
512
513
|
export type StaticEncode<T extends TSchema, P extends unknown[] = []> = Static<T, P>;
|
|
513
514
|
/** Creates the static type for a TypeBox type */
|