@sinclair/typebox 0.34.48 → 0.34.50
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/build/cjs/compiler/compiler.js +1 -1
- package/build/cjs/parser/runtime/guard.d.ts +1 -1
- package/build/cjs/parser/runtime/token.d.ts +2 -2
- package/build/cjs/syntax/mapping.d.ts +1 -1
- package/build/cjs/system/evaluate.d.ts +5 -0
- package/build/cjs/system/evaluate.js +11 -0
- package/build/cjs/system/index.d.ts +1 -0
- package/build/cjs/system/index.js +1 -0
- package/build/cjs/type/object/object.d.ts +2 -2
- package/build/cjs/type/object/object.js +18 -2
- package/build/cjs/value/parse/parse.d.ts +1 -1
- package/build/esm/compiler/compiler.mjs +2 -2
- package/build/esm/parser/runtime/guard.d.mts +1 -1
- package/build/esm/parser/runtime/token.d.mts +2 -2
- package/build/esm/syntax/mapping.d.mts +1 -1
- package/build/esm/system/evaluate.d.mts +5 -0
- package/build/esm/system/evaluate.mjs +7 -0
- package/build/esm/system/index.d.mts +1 -0
- package/build/esm/system/index.mjs +1 -0
- package/build/esm/type/composite/composite.mjs +2 -2
- package/build/esm/type/const/const.mjs +3 -3
- package/build/esm/type/instantiate/instantiate.mjs +2 -2
- package/build/esm/type/mapped/mapped.mjs +3 -3
- package/build/esm/type/module/compute.mjs +2 -2
- package/build/esm/type/object/object.d.mts +2 -2
- package/build/esm/type/object/object.mjs +18 -2
- package/build/esm/type/omit/omit.mjs +3 -3
- package/build/esm/type/partial/partial.mjs +3 -3
- package/build/esm/type/pick/pick.mjs +3 -3
- package/build/esm/type/record/record.mjs +3 -3
- package/build/esm/type/required/required.mjs +3 -3
- package/build/esm/type/type/json.mjs +2 -2
- package/build/esm/value/delta/delta.mjs +4 -4
- package/build/esm/value/parse/parse.d.mts +1 -1
- package/package.json +2 -2
- package/readme.md +7 -7
|
@@ -645,7 +645,7 @@ var TypeCompiler;
|
|
|
645
645
|
/** Compiles a TypeBox type for optimal runtime type checking. Types must be valid TypeBox types of TSchema */
|
|
646
646
|
function Compile(schema, references = []) {
|
|
647
647
|
const generatedCode = Code(schema, references, { language: 'javascript' });
|
|
648
|
-
const compiledFunction =
|
|
648
|
+
const compiledFunction = (0, index_3.Evaluate)('kind', 'format', 'hash', generatedCode);
|
|
649
649
|
const instances = new Map(state.instances);
|
|
650
650
|
function typeRegistryFunction(kind, instance, value) {
|
|
651
651
|
if (!index_8.TypeRegistry.Has(kind) || !instances.has(instance))
|
|
@@ -20,4 +20,4 @@ export declare function IsTuple(value: unknown): value is ITuple;
|
|
|
20
20
|
/** Returns true if the value is a Union Parser */
|
|
21
21
|
export declare function IsUnion(value: unknown): value is IUnion;
|
|
22
22
|
/** Returns true if the value is a Parser */
|
|
23
|
-
export declare function IsParser(value: unknown): value is
|
|
23
|
+
export declare function IsParser(value: unknown): value is IArray<unknown> | IConst<unknown> | IContext<unknown> | IIdent<unknown> | INumber<unknown> | IOptional<unknown> | IRef<unknown> | IString<unknown> | ITuple<unknown> | IUnion<unknown>;
|
|
@@ -3,6 +3,6 @@ export declare function Const(value: string, code: string): [] | [string, string
|
|
|
3
3
|
/** Scans for the next Ident token */
|
|
4
4
|
export declare function Ident(code: string): [] | [string, string];
|
|
5
5
|
/** Scans for the next number token */
|
|
6
|
-
export declare function Number(code: string): [
|
|
6
|
+
export declare function Number(code: string): [] | [string, string];
|
|
7
7
|
/** Scans the next Literal String value */
|
|
8
|
-
export declare function String(options: string[], code: string): [
|
|
8
|
+
export declare function String(options: string[], code: string): [] | [string, string];
|
|
@@ -121,7 +121,7 @@ export declare function AsyncIteratorMapping(input: [unknown, unknown, unknown,
|
|
|
121
121
|
export type TIteratorMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Iterator', '<', infer Type extends T.TSchema, '>'] ? T.TIterator<Type> : never;
|
|
122
122
|
export declare function IteratorMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TIterator<T.TSchema>;
|
|
123
123
|
export type TArgumentMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Argument', '<', infer Type extends T.TSchema, '>'] ? Type extends T.TLiteral<infer Index extends number> ? T.TArgument<Index> : T.TNever : never;
|
|
124
|
-
export declare function ArgumentMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.
|
|
124
|
+
export declare function ArgumentMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TArgument<number> | T.TNever;
|
|
125
125
|
export type TAwaitedMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Awaited', '<', infer Type extends T.TSchema, '>'] ? T.TAwaited<Type> : never;
|
|
126
126
|
export declare function AwaitedMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TSchema;
|
|
127
127
|
export type TArrayMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Array', '<', infer Type extends T.TSchema, '>'] ? T.TArray<Type> : never;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Evaluate = Evaluate;
|
|
5
|
+
/**
|
|
6
|
+
* Evaluates code in the current environment. This function matches centralized
|
|
7
|
+
* evaluation as implemented in TypeBox 1.x.
|
|
8
|
+
*/
|
|
9
|
+
function Evaluate(...args) {
|
|
10
|
+
return new globalThis.Function(...args);
|
|
11
|
+
}
|
|
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
16
|
};
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./evaluate"), exports);
|
|
18
19
|
__exportStar(require("./policy"), exports);
|
|
19
20
|
__exportStar(require("./system"), exports);
|
|
@@ -45,7 +45,7 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
45
45
|
required: TRequiredArray<T>;
|
|
46
46
|
}
|
|
47
47
|
/** `[Json]` Creates an Object type */
|
|
48
|
-
declare function
|
|
48
|
+
declare function _Object_<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
|
|
49
49
|
/** `[Json]` Creates an Object type */
|
|
50
|
-
export declare var Object: typeof
|
|
50
|
+
export declare var Object: typeof _Object_;
|
|
51
51
|
export {};
|
|
@@ -13,10 +13,26 @@ function RequiredArray(properties) {
|
|
|
13
13
|
return globalThis.Object.keys(properties).filter((key) => !(0, kind_1.IsOptional)(properties[key]));
|
|
14
14
|
}
|
|
15
15
|
/** `[Json]` Creates an Object type */
|
|
16
|
-
function
|
|
16
|
+
function _Object_(properties, options) {
|
|
17
17
|
const required = RequiredArray(properties);
|
|
18
18
|
const schema = required.length > 0 ? { [index_1.Kind]: 'Object', type: 'object', required, properties } : { [index_1.Kind]: 'Object', type: 'object', properties };
|
|
19
19
|
return (0, type_1.CreateType)(schema, options);
|
|
20
20
|
}
|
|
21
|
+
// ------------------------------------------------------------------
|
|
22
|
+
// TypeScript 7: CommonJS TS2441
|
|
23
|
+
//
|
|
24
|
+
// TypeScript generates a CommonJS shim that patches local variables
|
|
25
|
+
// via an unqualified reference to Object (e.g. the __esModule shim's
|
|
26
|
+
// Object.defineProperty call). Other compiler tools have been
|
|
27
|
+
// observed patching in the same way, but TypeScript 7 has correctly
|
|
28
|
+
// begun flagging variables named Object in CommonJS, since they
|
|
29
|
+
// conflict with the shim and can cause "used before definition"
|
|
30
|
+
// errors. This is CommonJS-specific; no such shim exists for ESM.
|
|
31
|
+
//
|
|
32
|
+
// TypeBox works around this using a `var` declaration, which is
|
|
33
|
+
// known to avoid use-before-definition errors. TypeBox 1.x employs
|
|
34
|
+
// a similar strategy.
|
|
35
|
+
// ------------------------------------------------------------------
|
|
21
36
|
/** `[Json]` Creates an Object type */
|
|
22
|
-
|
|
37
|
+
// @ts-ignore - error TS2441: Duplicate identifier 'Object'. Compiler reserves name 'Object' in top level scope of a module.
|
|
38
|
+
exports.Object = _Object_;
|
|
@@ -11,7 +11,7 @@ export declare namespace ParseRegistry {
|
|
|
11
11
|
function Set(key: string, callback: TParseFunction): void;
|
|
12
12
|
function Get(key: string): TParseFunction | undefined;
|
|
13
13
|
}
|
|
14
|
-
export declare const ParseDefault: readonly [
|
|
14
|
+
export declare const ParseDefault: readonly ['Clone', 'Clean', 'Default', 'Convert', 'Assert', 'Decode'];
|
|
15
15
|
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
16
|
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
17
|
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TransformEncode, TransformDecode, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/index.mjs';
|
|
2
2
|
import { Errors } from '../errors/index.mjs';
|
|
3
|
-
import { TypeSystemPolicy } from '../system/index.mjs';
|
|
3
|
+
import { TypeSystemPolicy, Evaluate } from '../system/index.mjs';
|
|
4
4
|
import { TypeBoxError } from '../type/error/index.mjs';
|
|
5
5
|
import { Deref } from '../value/deref/index.mjs';
|
|
6
6
|
import { Hash } from '../value/hash/index.mjs';
|
|
@@ -638,7 +638,7 @@ export var TypeCompiler;
|
|
|
638
638
|
/** Compiles a TypeBox type for optimal runtime type checking. Types must be valid TypeBox types of TSchema */
|
|
639
639
|
function Compile(schema, references = []) {
|
|
640
640
|
const generatedCode = Code(schema, references, { language: 'javascript' });
|
|
641
|
-
const compiledFunction =
|
|
641
|
+
const compiledFunction = Evaluate('kind', 'format', 'hash', generatedCode);
|
|
642
642
|
const instances = new Map(state.instances);
|
|
643
643
|
function typeRegistryFunction(kind, instance, value) {
|
|
644
644
|
if (!TypeRegistry.Has(kind) || !instances.has(instance))
|
|
@@ -20,4 +20,4 @@ export declare function IsTuple(value: unknown): value is ITuple;
|
|
|
20
20
|
/** Returns true if the value is a Union Parser */
|
|
21
21
|
export declare function IsUnion(value: unknown): value is IUnion;
|
|
22
22
|
/** Returns true if the value is a Parser */
|
|
23
|
-
export declare function IsParser(value: unknown): value is
|
|
23
|
+
export declare function IsParser(value: unknown): value is IArray<unknown> | IConst<unknown> | IContext<unknown> | IIdent<unknown> | INumber<unknown> | IOptional<unknown> | IRef<unknown> | IString<unknown> | ITuple<unknown> | IUnion<unknown>;
|
|
@@ -3,6 +3,6 @@ export declare function Const(value: string, code: string): [] | [string, string
|
|
|
3
3
|
/** Scans for the next Ident token */
|
|
4
4
|
export declare function Ident(code: string): [] | [string, string];
|
|
5
5
|
/** Scans for the next number token */
|
|
6
|
-
export declare function Number(code: string): [
|
|
6
|
+
export declare function Number(code: string): [] | [string, string];
|
|
7
7
|
/** Scans the next Literal String value */
|
|
8
|
-
export declare function String(options: string[], code: string): [
|
|
8
|
+
export declare function String(options: string[], code: string): [] | [string, string];
|
|
@@ -121,7 +121,7 @@ export declare function AsyncIteratorMapping(input: [unknown, unknown, unknown,
|
|
|
121
121
|
export type TIteratorMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Iterator', '<', infer Type extends T.TSchema, '>'] ? T.TIterator<Type> : never;
|
|
122
122
|
export declare function IteratorMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TIterator<T.TSchema>;
|
|
123
123
|
export type TArgumentMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Argument', '<', infer Type extends T.TSchema, '>'] ? Type extends T.TLiteral<infer Index extends number> ? T.TArgument<Index> : T.TNever : never;
|
|
124
|
-
export declare function ArgumentMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.
|
|
124
|
+
export declare function ArgumentMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TArgument<number> | T.TNever;
|
|
125
125
|
export type TAwaitedMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Awaited', '<', infer Type extends T.TSchema, '>'] ? T.TAwaited<Type> : never;
|
|
126
126
|
export declare function AwaitedMapping(input: [unknown, unknown, unknown, unknown], context: unknown): T.TSchema;
|
|
127
127
|
export type TArrayMapping<Input extends [unknown, unknown, unknown, unknown], Context extends T.TProperties> = Input extends ['Array', '<', infer Type extends T.TSchema, '>'] ? T.TArray<Type> : never;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IntersectEvaluated } from '../intersect/index.mjs';
|
|
2
2
|
import { IndexFromPropertyKeys } from '../indexed/index.mjs';
|
|
3
3
|
import { KeyOfPropertyKeys } from '../keyof/index.mjs';
|
|
4
|
-
import { Object } from '../object/index.mjs';
|
|
4
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
5
5
|
import { SetDistinct } from '../sets/index.mjs';
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// TypeGuard
|
|
@@ -37,6 +37,6 @@ function CompositeProperties(T, K) {
|
|
|
37
37
|
export function Composite(T, options) {
|
|
38
38
|
const K = CompositeKeys(T);
|
|
39
39
|
const P = CompositeProperties(T, K);
|
|
40
|
-
const R =
|
|
40
|
+
const R = _Object_(P, options);
|
|
41
41
|
return R;
|
|
42
42
|
}
|
|
@@ -4,7 +4,7 @@ import { Date } from '../date/index.mjs';
|
|
|
4
4
|
import { Function as FunctionType } from '../function/index.mjs';
|
|
5
5
|
import { Literal } from '../literal/index.mjs';
|
|
6
6
|
import { Null } from '../null/index.mjs';
|
|
7
|
-
import { Object } from '../object/index.mjs';
|
|
7
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
8
8
|
import { Symbol } from '../symbol/index.mjs';
|
|
9
9
|
import { Tuple } from '../tuple/index.mjs';
|
|
10
10
|
import { Readonly } from '../readonly/index.mjs';
|
|
@@ -37,7 +37,7 @@ function FromValue(value, root) {
|
|
|
37
37
|
IsArray(value) ? Readonly(Tuple(FromArray(value))) :
|
|
38
38
|
IsUint8Array(value) ? Uint8Array() :
|
|
39
39
|
IsDate(value) ? Date() :
|
|
40
|
-
IsObject(value) ? ConditionalReadonly(
|
|
40
|
+
IsObject(value) ? ConditionalReadonly(_Object_(FromProperties(value)), root) :
|
|
41
41
|
IsFunction(value) ? ConditionalReadonly(FunctionType([], Unknown()), root) :
|
|
42
42
|
IsUndefined(value) ? Undefined() :
|
|
43
43
|
IsNull(value) ? Null() :
|
|
@@ -46,7 +46,7 @@ function FromValue(value, root) {
|
|
|
46
46
|
IsNumber(value) ? Literal(value) :
|
|
47
47
|
IsBoolean(value) ? Literal(value) :
|
|
48
48
|
IsString(value) ? Literal(value) :
|
|
49
|
-
|
|
49
|
+
_Object_({}));
|
|
50
50
|
}
|
|
51
51
|
/** `[JavaScript]` Creates a readonly const type from the given value. */
|
|
52
52
|
export function Const(T, options) {
|
|
@@ -3,7 +3,7 @@ import { Unknown } from '../unknown/index.mjs';
|
|
|
3
3
|
import { ReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
4
4
|
import { Readonly } from '../readonly/index.mjs';
|
|
5
5
|
import { Optional } from '../optional/index.mjs';
|
|
6
|
-
import { Object } from '../object/index.mjs';
|
|
6
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
7
7
|
import { Record, RecordKey, RecordValue } from '../record/index.mjs';
|
|
8
8
|
import * as ValueGuard from '../guard/value.mjs';
|
|
9
9
|
import * as KindGuard from '../guard/kind.mjs';
|
|
@@ -59,7 +59,7 @@ function FromPromise(args, type) {
|
|
|
59
59
|
// prettier-ignore
|
|
60
60
|
function FromObject(args, type) {
|
|
61
61
|
const mappedProperties = FromProperties(args, type.properties);
|
|
62
|
-
return { ...type, ...
|
|
62
|
+
return { ...type, ..._Object_(mappedProperties) }; // retain options
|
|
63
63
|
}
|
|
64
64
|
// prettier-ignore
|
|
65
65
|
function FromRecord(args, type) {
|
|
@@ -9,7 +9,7 @@ import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
|
9
9
|
import { Intersect } from '../intersect/index.mjs';
|
|
10
10
|
import { Iterator } from '../iterator/index.mjs';
|
|
11
11
|
import { Literal } from '../literal/index.mjs';
|
|
12
|
-
import { Object } from '../object/index.mjs';
|
|
12
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
13
13
|
import { Optional } from '../optional/index.mjs';
|
|
14
14
|
import { Promise } from '../promise/index.mjs';
|
|
15
15
|
import { Readonly } from '../readonly/index.mjs';
|
|
@@ -81,7 +81,7 @@ function FromSchemaType(K, T) {
|
|
|
81
81
|
IsIntersect(T) ? Intersect(FromRest(K, T.allOf), options) :
|
|
82
82
|
IsUnion(T) ? Union(FromRest(K, T.anyOf), options) :
|
|
83
83
|
IsTuple(T) ? Tuple(FromRest(K, T.items ?? []), options) :
|
|
84
|
-
IsObject(T) ?
|
|
84
|
+
IsObject(T) ? _Object_(FromProperties(K, T.properties), options) :
|
|
85
85
|
IsArray(T) ? Array(FromSchemaType(K, T.items), options) :
|
|
86
86
|
IsPromise(T) ? Promise(FromSchemaType(K, T.item), options) :
|
|
87
87
|
T);
|
|
@@ -98,5 +98,5 @@ export function Mapped(key, map, options) {
|
|
|
98
98
|
const K = IsSchema(key) ? IndexPropertyKeys(key) : key;
|
|
99
99
|
const RT = map({ [Kind]: 'MappedKey', keys: K });
|
|
100
100
|
const R = MappedFunctionReturnType(K, RT);
|
|
101
|
-
return
|
|
101
|
+
return _Object_(R, options);
|
|
102
102
|
}
|
|
@@ -10,7 +10,7 @@ import { Function as FunctionType } from '../function/index.mjs';
|
|
|
10
10
|
import { Intersect } from '../intersect/index.mjs';
|
|
11
11
|
import { Iterator } from '../iterator/index.mjs';
|
|
12
12
|
import { KeyOf } from '../keyof/index.mjs';
|
|
13
|
-
import { Object } from '../object/index.mjs';
|
|
13
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
14
14
|
import { Omit } from '../omit/index.mjs';
|
|
15
15
|
import { Pick } from '../pick/index.mjs';
|
|
16
16
|
import { Never } from '../never/index.mjs';
|
|
@@ -104,7 +104,7 @@ function FromIterator(moduleProperties, type) {
|
|
|
104
104
|
return Iterator(FromType(moduleProperties, type));
|
|
105
105
|
}
|
|
106
106
|
function FromObject(moduleProperties, properties) {
|
|
107
|
-
return
|
|
107
|
+
return _Object_(globalThis.Object.keys(properties).reduce((result, key) => {
|
|
108
108
|
return { ...result, [key]: FromType(moduleProperties, properties[key]) };
|
|
109
109
|
}, {}));
|
|
110
110
|
}
|
|
@@ -45,7 +45,7 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
45
45
|
required: TRequiredArray<T>;
|
|
46
46
|
}
|
|
47
47
|
/** `[Json]` Creates an Object type */
|
|
48
|
-
declare function
|
|
48
|
+
declare function _Object_<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
|
|
49
49
|
/** `[Json]` Creates an Object type */
|
|
50
|
-
export declare var Object: typeof
|
|
50
|
+
export declare var Object: typeof _Object_;
|
|
51
51
|
export {};
|
|
@@ -9,10 +9,26 @@ function RequiredArray(properties) {
|
|
|
9
9
|
return globalThis.Object.keys(properties).filter((key) => !IsOptional(properties[key]));
|
|
10
10
|
}
|
|
11
11
|
/** `[Json]` Creates an Object type */
|
|
12
|
-
function
|
|
12
|
+
function _Object_(properties, options) {
|
|
13
13
|
const required = RequiredArray(properties);
|
|
14
14
|
const schema = required.length > 0 ? { [Kind]: 'Object', type: 'object', required, properties } : { [Kind]: 'Object', type: 'object', properties };
|
|
15
15
|
return CreateType(schema, options);
|
|
16
16
|
}
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
// TypeScript 7: CommonJS TS2441
|
|
19
|
+
//
|
|
20
|
+
// TypeScript generates a CommonJS shim that patches local variables
|
|
21
|
+
// via an unqualified reference to Object (e.g. the __esModule shim's
|
|
22
|
+
// Object.defineProperty call). Other compiler tools have been
|
|
23
|
+
// observed patching in the same way, but TypeScript 7 has correctly
|
|
24
|
+
// begun flagging variables named Object in CommonJS, since they
|
|
25
|
+
// conflict with the shim and can cause "used before definition"
|
|
26
|
+
// errors. This is CommonJS-specific; no such shim exists for ESM.
|
|
27
|
+
//
|
|
28
|
+
// TypeBox works around this using a `var` declaration, which is
|
|
29
|
+
// known to avoid use-before-definition errors. TypeBox 1.x employs
|
|
30
|
+
// a similar strategy.
|
|
31
|
+
// ------------------------------------------------------------------
|
|
17
32
|
/** `[Json]` Creates an Object type */
|
|
18
|
-
|
|
33
|
+
// @ts-ignore - error TS2441: Duplicate identifier 'Object'. Compiler reserves name 'Object' in top level scope of a module.
|
|
34
|
+
export var Object = _Object_;
|
|
@@ -6,7 +6,7 @@ import { Literal } from '../literal/index.mjs';
|
|
|
6
6
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
7
7
|
import { Intersect } from '../intersect/index.mjs';
|
|
8
8
|
import { Union } from '../union/index.mjs';
|
|
9
|
-
import { Object } from '../object/index.mjs';
|
|
9
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// Mapped
|
|
12
12
|
// ------------------------------------------------------------------
|
|
@@ -41,7 +41,7 @@ function FromProperties(properties, propertyKeys) {
|
|
|
41
41
|
function FromObject(type, propertyKeys, properties) {
|
|
42
42
|
const options = Discard(type, [TransformKind, '$id', 'required', 'properties']);
|
|
43
43
|
const mappedProperties = FromProperties(properties, propertyKeys);
|
|
44
|
-
return
|
|
44
|
+
return _Object_(mappedProperties, options);
|
|
45
45
|
}
|
|
46
46
|
// prettier-ignore
|
|
47
47
|
function UnionFromPropertyKeys(propertyKeys) {
|
|
@@ -53,7 +53,7 @@ function OmitResolve(type, propertyKeys) {
|
|
|
53
53
|
return (IsIntersect(type) ? Intersect(FromIntersect(type.allOf, propertyKeys)) :
|
|
54
54
|
IsUnion(type) ? Union(FromUnion(type.anyOf, propertyKeys)) :
|
|
55
55
|
IsObject(type) ? FromObject(type, propertyKeys, type.properties) :
|
|
56
|
-
|
|
56
|
+
_Object_({}));
|
|
57
57
|
}
|
|
58
58
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
59
59
|
// prettier-ignore
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
2
|
import { Computed } from '../computed/index.mjs';
|
|
3
3
|
import { Optional } from '../optional/index.mjs';
|
|
4
|
-
import { Object } from '../object/index.mjs';
|
|
4
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
5
5
|
import { Intersect } from '../intersect/index.mjs';
|
|
6
6
|
import { Union } from '../union/index.mjs';
|
|
7
7
|
import { Ref } from '../ref/index.mjs';
|
|
@@ -31,7 +31,7 @@ function FromProperties(properties) {
|
|
|
31
31
|
function FromObject(type, properties) {
|
|
32
32
|
const options = Discard(type, [TransformKind, '$id', 'required', 'properties']);
|
|
33
33
|
const mappedProperties = FromProperties(properties);
|
|
34
|
-
return
|
|
34
|
+
return _Object_(mappedProperties, options);
|
|
35
35
|
}
|
|
36
36
|
// prettier-ignore
|
|
37
37
|
function FromRest(types) {
|
|
@@ -60,7 +60,7 @@ function PartialResolve(type) {
|
|
|
60
60
|
KindGuard.IsSymbol(type) ? type :
|
|
61
61
|
KindGuard.IsUndefined(type) ? type :
|
|
62
62
|
// Passthrough
|
|
63
|
-
|
|
63
|
+
_Object_({}));
|
|
64
64
|
}
|
|
65
65
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
66
66
|
export function Partial(type, options) {
|
|
@@ -3,7 +3,7 @@ import { Discard } from '../discard/discard.mjs';
|
|
|
3
3
|
import { Computed } from '../computed/index.mjs';
|
|
4
4
|
import { Intersect } from '../intersect/index.mjs';
|
|
5
5
|
import { Literal } from '../literal/index.mjs';
|
|
6
|
-
import { Object } from '../object/index.mjs';
|
|
6
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
7
7
|
import { Union } from '../union/index.mjs';
|
|
8
8
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
9
9
|
import { TransformKind } from '../symbols/symbols.mjs';
|
|
@@ -36,7 +36,7 @@ function FromProperties(properties, propertyKeys) {
|
|
|
36
36
|
function FromObject(Type, keys, properties) {
|
|
37
37
|
const options = Discard(Type, [TransformKind, '$id', 'required', 'properties']);
|
|
38
38
|
const mappedProperties = FromProperties(properties, keys);
|
|
39
|
-
return
|
|
39
|
+
return _Object_(mappedProperties, options);
|
|
40
40
|
}
|
|
41
41
|
// prettier-ignore
|
|
42
42
|
function UnionFromPropertyKeys(propertyKeys) {
|
|
@@ -48,7 +48,7 @@ function PickResolve(type, propertyKeys) {
|
|
|
48
48
|
return (IsIntersect(type) ? Intersect(FromIntersect(type.allOf, propertyKeys)) :
|
|
49
49
|
IsUnion(type) ? Union(FromUnion(type.anyOf, propertyKeys)) :
|
|
50
50
|
IsObject(type) ? FromObject(type, propertyKeys, type.properties) :
|
|
51
|
-
|
|
51
|
+
_Object_({}));
|
|
52
52
|
}
|
|
53
53
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
54
54
|
// prettier-ignore
|
|
@@ -2,7 +2,7 @@ import { CreateType } from '../create/type.mjs';
|
|
|
2
2
|
import { Kind, Hint } from '../symbols/index.mjs';
|
|
3
3
|
import { Never } from '../never/index.mjs';
|
|
4
4
|
import { Number } from '../number/index.mjs';
|
|
5
|
-
import { Object } from '../object/index.mjs';
|
|
5
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
6
6
|
import { String } from '../string/index.mjs';
|
|
7
7
|
import { Union } from '../union/index.mjs';
|
|
8
8
|
import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
|
|
@@ -31,7 +31,7 @@ function RecordCreateFromKeys(K, T, options) {
|
|
|
31
31
|
const result = {};
|
|
32
32
|
for (const K2 of K)
|
|
33
33
|
result[K2] = T;
|
|
34
|
-
return
|
|
34
|
+
return _Object_(result, { ...options, [Hint]: 'Record' });
|
|
35
35
|
}
|
|
36
36
|
// prettier-ignore
|
|
37
37
|
function FromTemplateLiteralKey(K, T, options) {
|
|
@@ -66,7 +66,7 @@ function FromNeverKey(_key, type, options) {
|
|
|
66
66
|
}
|
|
67
67
|
// prettier-ignore
|
|
68
68
|
function FromBooleanKey(_key, type, options) {
|
|
69
|
-
return
|
|
69
|
+
return _Object_({ true: type, false: type }, options);
|
|
70
70
|
}
|
|
71
71
|
// prettier-ignore
|
|
72
72
|
function FromIntegerKey(_key, type, options) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
2
|
import { Computed } from '../computed/index.mjs';
|
|
3
|
-
import { Object } from '../object/index.mjs';
|
|
3
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
4
4
|
import { Intersect } from '../intersect/index.mjs';
|
|
5
5
|
import { Union } from '../union/index.mjs';
|
|
6
6
|
import { Ref } from '../ref/index.mjs';
|
|
@@ -30,7 +30,7 @@ function FromProperties(properties) {
|
|
|
30
30
|
function FromObject(type, properties) {
|
|
31
31
|
const options = Discard(type, [TransformKind, '$id', 'required', 'properties']);
|
|
32
32
|
const mappedProperties = FromProperties(properties);
|
|
33
|
-
return
|
|
33
|
+
return _Object_(mappedProperties, options);
|
|
34
34
|
}
|
|
35
35
|
// prettier-ignore
|
|
36
36
|
function FromRest(types) {
|
|
@@ -59,7 +59,7 @@ function RequiredResolve(type) {
|
|
|
59
59
|
KindGuard.IsSymbol(type) ? type :
|
|
60
60
|
KindGuard.IsUndefined(type) ? type :
|
|
61
61
|
// Passthrough
|
|
62
|
-
|
|
62
|
+
_Object_({}));
|
|
63
63
|
}
|
|
64
64
|
/** `[Json]` Constructs a type where all properties are required */
|
|
65
65
|
export function Required(type, options) {
|
|
@@ -19,7 +19,7 @@ import { Not } from '../not/index.mjs';
|
|
|
19
19
|
import { Null } from '../null/index.mjs';
|
|
20
20
|
import { Module } from '../module/index.mjs';
|
|
21
21
|
import { Number } from '../number/index.mjs';
|
|
22
|
-
import { Object } from '../object/index.mjs';
|
|
22
|
+
import { Object as _Object_ } from '../object/index.mjs';
|
|
23
23
|
import { Omit } from '../omit/index.mjs';
|
|
24
24
|
import { Optional } from '../optional/index.mjs';
|
|
25
25
|
import { Partial } from '../partial/index.mjs';
|
|
@@ -148,7 +148,7 @@ export class JsonTypeBuilder {
|
|
|
148
148
|
}
|
|
149
149
|
/** `[Json]` Creates an Object type */
|
|
150
150
|
Object(properties, options) {
|
|
151
|
-
return
|
|
151
|
+
return _Object_(properties, options);
|
|
152
152
|
}
|
|
153
153
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
154
154
|
Omit(schema, selector, options) {
|
|
@@ -4,21 +4,21 @@ import { Clone } from '../clone/index.mjs';
|
|
|
4
4
|
import { Equal } from '../equal/equal.mjs';
|
|
5
5
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
6
6
|
import { Literal } from '../../type/literal/index.mjs';
|
|
7
|
-
import { Object } from '../../type/object/index.mjs';
|
|
7
|
+
import { Object as _Object_ } from '../../type/object/index.mjs';
|
|
8
8
|
import { String } from '../../type/string/index.mjs';
|
|
9
9
|
import { Unknown } from '../../type/unknown/index.mjs';
|
|
10
10
|
import { Union } from '../../type/union/index.mjs';
|
|
11
|
-
export const Insert =
|
|
11
|
+
export const Insert = _Object_({
|
|
12
12
|
type: Literal('insert'),
|
|
13
13
|
path: String(),
|
|
14
14
|
value: Unknown(),
|
|
15
15
|
});
|
|
16
|
-
export const Update =
|
|
16
|
+
export const Update = _Object_({
|
|
17
17
|
type: Literal('update'),
|
|
18
18
|
path: String(),
|
|
19
19
|
value: Unknown(),
|
|
20
20
|
});
|
|
21
|
-
export const Delete =
|
|
21
|
+
export const Delete = _Object_({
|
|
22
22
|
type: Literal('delete'),
|
|
23
23
|
path: String(),
|
|
24
24
|
});
|
|
@@ -11,7 +11,7 @@ export declare namespace ParseRegistry {
|
|
|
11
11
|
function Set(key: string, callback: TParseFunction): void;
|
|
12
12
|
function Get(key: string): TParseFunction | undefined;
|
|
13
13
|
}
|
|
14
|
-
export declare const ParseDefault: readonly [
|
|
14
|
+
export declare const ParseDefault: readonly ['Clone', 'Clean', 'Default', 'Convert', 'Assert', 'Decode'];
|
|
15
15
|
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
16
|
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
17
|
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.34.
|
|
3
|
+
"version": "0.34.50",
|
|
4
4
|
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "https://github.com/sinclairzx81/typebox
|
|
15
|
+
"url": "https://github.com/sinclairzx81/sinclair-typebox"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
18
|
"test": "echo test"
|
package/readme.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
<div align='center'>
|
|
2
2
|
|
|
3
|
-
<h1>TypeBox
|
|
3
|
+
<h1>Sinclair TypeBox</h1>
|
|
4
4
|
|
|
5
5
|
<p>Json Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
6
|
|
|
7
|
-
<img src="https://raw.githubusercontent.com/sinclairzx81/typebox
|
|
7
|
+
<img src="https://raw.githubusercontent.com/sinclairzx81/sinclair-typebox/refs/heads/main/typebox.png" />
|
|
8
8
|
|
|
9
9
|
<br />
|
|
10
10
|
<br />
|
|
@@ -16,14 +16,17 @@
|
|
|
16
16
|
|
|
17
17
|
</div>
|
|
18
18
|
|
|
19
|
+
|
|
19
20
|
<a name="Install"></a>
|
|
20
21
|
|
|
21
22
|
## Install
|
|
22
23
|
|
|
24
|
+
For the latest version use [TypeBox 1.x](https://github.com/sinclairzx81/typebox)
|
|
25
|
+
|
|
23
26
|
```bash
|
|
24
|
-
$ npm install @sinclair/typebox # TypeBox
|
|
27
|
+
$ npm install @sinclair/typebox # TypeBox 0.x - Long Term Support
|
|
25
28
|
|
|
26
|
-
$ npm install typebox # TypeBox
|
|
29
|
+
$ npm install typebox # TypeBox 1.x - Latest
|
|
27
30
|
```
|
|
28
31
|
|
|
29
32
|
## Example
|
|
@@ -48,13 +51,10 @@ type T = Static<typeof T> // type T = {
|
|
|
48
51
|
// }
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
|
|
52
54
|
<a name="Overview"></a>
|
|
53
55
|
|
|
54
56
|
## Overview
|
|
55
57
|
|
|
56
|
-
> ⚠️ TypeBox versions (pre-1.0) will continue active maintenance through 2026 and beyond. This repository services as the OIDC publishing environment for the `@sinclair/typebox` package scope on NPM. For TypeBox versions 1.0 and above, refer to https://github.com/sinclairzx81/typebox
|
|
57
|
-
|
|
58
58
|
TypeBox is a runtime type builder that creates in-memory Json Schema objects that infer as TypeScript types. The schematics 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 asserted using standard Json Schema validation.
|
|
59
59
|
|
|
60
60
|
This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|