@sinclair/typebox 0.32.0-dev-8 → 0.32.0-dev-9
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/import/compiler/index.d.mts +1 -1
- package/build/import/compiler/index.mjs +1 -1
- package/build/import/errors/errors.d.mts +0 -1
- package/build/import/errors/errors.mjs +4 -3
- package/build/import/{system/errors.d.mts → errors/function.d.mts} +4 -7
- package/build/import/{system/errors.mjs → errors/function.mjs} +8 -17
- package/build/import/errors/index.d.mts +2 -1
- package/build/import/errors/index.mjs +2 -1
- package/build/import/index.d.mts +0 -1
- package/build/import/index.mjs +0 -1
- package/build/import/system/index.d.mts +0 -2
- package/build/import/system/index.mjs +0 -2
- package/build/import/value/cast/cast.mjs +34 -38
- package/build/require/compiler/index.d.ts +1 -1
- package/build/require/compiler/index.js +6 -16
- package/build/require/errors/errors.d.ts +0 -1
- package/build/require/errors/errors.js +3 -3
- package/build/require/{system/errors.d.ts → errors/function.d.ts} +4 -7
- package/build/require/{system/errors.js → errors/function.js} +11 -18
- package/build/require/errors/index.d.ts +2 -1
- package/build/require/errors/index.js +10 -15
- package/build/require/index.d.ts +0 -1
- package/build/require/index.js +2 -4
- package/build/require/system/index.d.ts +0 -2
- package/build/require/system/index.js +1 -6
- package/build/require/value/cast/cast.js +34 -38
- package/license +1 -1
- package/package.json +1 -1
- package/readme.md +48 -44
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
2
|
-
export
|
|
2
|
+
export { TypeCompiler, TypeCheck, type TypeCompilerCodegenOptions, type TypeCompilerLanguageOption, TypeCompilerTypeGuardError, TypeCompilerUnknownTypeError } from './compiler.mjs';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
2
|
-
export
|
|
2
|
+
export { TypeCompiler, TypeCheck, TypeCompilerTypeGuardError, TypeCompilerUnknownTypeError } from './compiler.mjs';
|
|
@@ -75,7 +75,6 @@ export declare class ValueErrorsUnknownTypeError extends Error {
|
|
|
75
75
|
readonly schema: TSchema;
|
|
76
76
|
constructor(schema: TSchema);
|
|
77
77
|
}
|
|
78
|
-
export declare function EscapeKey(key: string): string;
|
|
79
78
|
export declare class ValueErrorIterator {
|
|
80
79
|
private readonly iterator;
|
|
81
80
|
constructor(iterator: IterableIterator<ValueError>);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../value/guard/index.mjs';
|
|
2
|
-
import { TypeSystemPolicy
|
|
2
|
+
import { TypeSystemPolicy } from '../system/index.mjs';
|
|
3
3
|
import { KeyOfStringResolvePattern } from '../type/keyof/index.mjs';
|
|
4
4
|
import { TypeRegistry, FormatRegistry } from '../type/registry/index.mjs';
|
|
5
5
|
import { ExtendsUndefinedCheck } from '../type/extends/extends-undefined.mjs';
|
|
6
|
+
import { GetErrorFunction } from './function.mjs';
|
|
6
7
|
import { Deref } from '../value/deref/index.mjs';
|
|
7
8
|
import { Hash } from '../value/hash/index.mjs';
|
|
8
9
|
import { Kind } from '../type/symbols/index.mjs';
|
|
@@ -80,7 +81,7 @@ export class ValueErrorsUnknownTypeError extends Error {
|
|
|
80
81
|
this.schema = schema;
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
+
function EscapeKey(key) {
|
|
84
85
|
return key.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
85
86
|
}
|
|
86
87
|
function IsDefined(value) {
|
|
@@ -100,7 +101,7 @@ export class ValueErrorIterator {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
function Create(type, schema, path, value) {
|
|
103
|
-
return { type, schema, path, value, message:
|
|
104
|
+
return { type, schema, path, value, message: GetErrorFunction()(schema, type) };
|
|
104
105
|
}
|
|
105
106
|
function* TAny(schema, references, path, value) { }
|
|
106
107
|
function* TArray(schema, references, path, value) {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { ValueErrorType } from '../errors/errors.mjs';
|
|
2
1
|
import { TSchema } from '../type/schema/index.mjs';
|
|
3
|
-
|
|
4
|
-
export declare namespace TypeSystemErrorFunction {
|
|
5
|
-
function Reset(): void;
|
|
6
|
-
function Set(callback: ErrorFunction): void;
|
|
7
|
-
function Get(): ErrorFunction;
|
|
8
|
-
}
|
|
2
|
+
import { ValueErrorType } from './errors.mjs';
|
|
9
3
|
export declare function DefaultErrorFunction(schema: TSchema, errorType: ValueErrorType): string;
|
|
4
|
+
export type ErrorFunction = (schema: TSchema, type: ValueErrorType) => string;
|
|
5
|
+
export declare function SetErrorFunction(callback: ErrorFunction): void;
|
|
6
|
+
export declare function GetErrorFunction(): ErrorFunction;
|
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
import { ValueErrorType } from '../errors/errors.mjs';
|
|
2
1
|
import { Kind } from '../type/symbols/index.mjs';
|
|
3
|
-
|
|
4
|
-
(function (TypeSystemErrorFunction) {
|
|
5
|
-
let errorMessageFunction = DefaultErrorFunction;
|
|
6
|
-
function Reset() {
|
|
7
|
-
errorMessageFunction = DefaultErrorFunction;
|
|
8
|
-
}
|
|
9
|
-
TypeSystemErrorFunction.Reset = Reset;
|
|
10
|
-
function Set(callback) {
|
|
11
|
-
errorMessageFunction = callback;
|
|
12
|
-
}
|
|
13
|
-
TypeSystemErrorFunction.Set = Set;
|
|
14
|
-
function Get() {
|
|
15
|
-
return errorMessageFunction;
|
|
16
|
-
}
|
|
17
|
-
TypeSystemErrorFunction.Get = Get;
|
|
18
|
-
})(TypeSystemErrorFunction || (TypeSystemErrorFunction = {}));
|
|
2
|
+
import { ValueErrorType } from './errors.mjs';
|
|
19
3
|
export function DefaultErrorFunction(schema, errorType) {
|
|
20
4
|
switch (errorType) {
|
|
21
5
|
case ValueErrorType.ArrayContains:
|
|
@@ -148,3 +132,10 @@ export function DefaultErrorFunction(schema, errorType) {
|
|
|
148
132
|
return 'Unknown error type';
|
|
149
133
|
}
|
|
150
134
|
}
|
|
135
|
+
let errorFunction = DefaultErrorFunction;
|
|
136
|
+
export function SetErrorFunction(callback) {
|
|
137
|
+
errorFunction = callback;
|
|
138
|
+
}
|
|
139
|
+
export function GetErrorFunction() {
|
|
140
|
+
return errorFunction;
|
|
141
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { Errors, ValueError, ValueErrorIterator, ValueErrorType, ValueErrorsUnknownTypeError } from './errors.mjs';
|
|
2
|
+
export { DefaultErrorFunction, GetErrorFunction, SetErrorFunction, type ErrorFunction } from './function.mjs';
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { Errors, ValueErrorIterator, ValueErrorType, ValueErrorsUnknownTypeError } from './errors.mjs';
|
|
2
|
+
export { DefaultErrorFunction, GetErrorFunction, SetErrorFunction } from './function.mjs';
|
package/build/import/index.d.mts
CHANGED
|
@@ -3,7 +3,6 @@ export { PatternBoolean, PatternBooleanExact, PatternNumber, PatternNumberExact,
|
|
|
3
3
|
export { TypeRegistry, FormatRegistry } from './type/registry/index.mjs';
|
|
4
4
|
export { TypeGuard, ValueGuard } from './type/guard/index.mjs';
|
|
5
5
|
export { CloneType, CloneRest } from './type/clone/type.mjs';
|
|
6
|
-
export { Clone } from './type/clone/value.mjs';
|
|
7
6
|
export { Any, type TAny } from './type/any/index.mjs';
|
|
8
7
|
export { Array, type TArray, type ArrayOptions } from './type/array/index.mjs';
|
|
9
8
|
export { AsyncIterator, type TAsyncIterator } from './type/async-iterator/index.mjs';
|
package/build/import/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ export { PatternBoolean, PatternBooleanExact, PatternNumber, PatternNumberExact,
|
|
|
3
3
|
export { TypeRegistry, FormatRegistry } from './type/registry/index.mjs';
|
|
4
4
|
export { TypeGuard, ValueGuard } from './type/guard/index.mjs';
|
|
5
5
|
export { CloneType, CloneRest } from './type/clone/type.mjs';
|
|
6
|
-
export { Clone } from './type/clone/value.mjs';
|
|
7
6
|
export { Any } from './type/any/index.mjs';
|
|
8
7
|
export { Array } from './type/array/index.mjs';
|
|
9
8
|
export { AsyncIterator } from './type/async-iterator/index.mjs';
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export { ValueErrorType } from '../errors/errors.mjs';
|
|
2
|
-
export { DefaultErrorFunction, ErrorFunction, TypeSystemErrorFunction } from './errors.mjs';
|
|
3
1
|
export { TypeSystemPolicy } from './policy.mjs';
|
|
4
2
|
export { TypeSystem, TypeSystemDuplicateFormat, TypeSystemDuplicateTypeKind } from './system.mjs';
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export { ValueErrorType } from '../errors/errors.mjs';
|
|
2
|
-
export { DefaultErrorFunction, TypeSystemErrorFunction } from './errors.mjs';
|
|
3
1
|
export { TypeSystemPolicy } from './policy.mjs';
|
|
4
2
|
export { TypeSystem, TypeSystemDuplicateFormat, TypeSystemDuplicateTypeKind } from './system.mjs';
|
|
@@ -34,47 +34,43 @@ export class ValueCastUnknownTypeError extends Error {
|
|
|
34
34
|
this.schema = schema;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return acc + (literal + checks + exists);
|
|
50
|
-
}, 0);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
return Check(schema, references, value) ? 1 : 0;
|
|
54
|
-
}
|
|
37
|
+
function ScoreUnion(schema, references, value) {
|
|
38
|
+
if (schema[Kind] === 'Object' && typeof value === 'object' && !IsNull(value)) {
|
|
39
|
+
const object = schema;
|
|
40
|
+
const keys = Object.getOwnPropertyNames(value);
|
|
41
|
+
const entries = Object.entries(object.properties);
|
|
42
|
+
const [point, max] = [1 / entries.length, entries.length];
|
|
43
|
+
return entries.reduce((acc, [key, schema]) => {
|
|
44
|
+
const literal = schema[Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
45
|
+
const checks = Check(schema, references, value[key]) ? point : 0;
|
|
46
|
+
const exists = keys.includes(key) ? point : 0;
|
|
47
|
+
return acc + (literal + checks + exists);
|
|
48
|
+
}, 0);
|
|
55
49
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
for (const schema of union.anyOf) {
|
|
59
|
-
const score = Score(schema, references, value);
|
|
60
|
-
if (score > best) {
|
|
61
|
-
select = schema;
|
|
62
|
-
best = score;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return select;
|
|
50
|
+
else {
|
|
51
|
+
return Check(schema, references, value) ? 1 : 0;
|
|
66
52
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
53
|
+
}
|
|
54
|
+
function SelectUnion(union, references, value) {
|
|
55
|
+
let [select, best] = [union.anyOf[0], 0];
|
|
56
|
+
for (const schema of union.anyOf) {
|
|
57
|
+
const score = ScoreUnion(schema, references, value);
|
|
58
|
+
if (score > best) {
|
|
59
|
+
select = schema;
|
|
60
|
+
best = score;
|
|
74
61
|
}
|
|
75
62
|
}
|
|
76
|
-
|
|
77
|
-
}
|
|
63
|
+
return select;
|
|
64
|
+
}
|
|
65
|
+
function CastUnion(union, references, value) {
|
|
66
|
+
if ('default' in union) {
|
|
67
|
+
return union.default;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const schema = SelectUnion(union, references, value);
|
|
71
|
+
return Cast(schema, references, value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
78
74
|
function DefaultClone(schema, references, value) {
|
|
79
75
|
return Check(schema, references, value) ? Clone(value) : Create(schema, references);
|
|
80
76
|
}
|
|
@@ -166,7 +162,7 @@ function TTuple(schema, references, value) {
|
|
|
166
162
|
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
167
163
|
}
|
|
168
164
|
function TUnion(schema, references, value) {
|
|
169
|
-
return Check(schema, references, value) ? Clone(value) :
|
|
165
|
+
return Check(schema, references, value) ? Clone(value) : CastUnion(schema, references, value);
|
|
170
166
|
}
|
|
171
167
|
function Visit(schema, references, value) {
|
|
172
168
|
const references_ = IsString(schema.$id) ? [...references, schema] : references;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index';
|
|
2
|
-
export
|
|
2
|
+
export { TypeCompiler, TypeCheck, type TypeCompilerCodegenOptions, type TypeCompilerLanguageOption, TypeCompilerTypeGuardError, TypeCompilerUnknownTypeError } from './compiler';
|
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
3
|
+
exports.TypeCompilerUnknownTypeError = exports.TypeCompilerTypeGuardError = exports.TypeCheck = exports.TypeCompiler = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
18
4
|
var index_1 = require("../errors/index");
|
|
19
5
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
20
6
|
Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return index_1.ValueErrorIterator; } });
|
|
21
|
-
|
|
7
|
+
var compiler_1 = require("./compiler");
|
|
8
|
+
Object.defineProperty(exports, "TypeCompiler", { enumerable: true, get: function () { return compiler_1.TypeCompiler; } });
|
|
9
|
+
Object.defineProperty(exports, "TypeCheck", { enumerable: true, get: function () { return compiler_1.TypeCheck; } });
|
|
10
|
+
Object.defineProperty(exports, "TypeCompilerTypeGuardError", { enumerable: true, get: function () { return compiler_1.TypeCompilerTypeGuardError; } });
|
|
11
|
+
Object.defineProperty(exports, "TypeCompilerUnknownTypeError", { enumerable: true, get: function () { return compiler_1.TypeCompilerUnknownTypeError; } });
|
|
@@ -75,7 +75,6 @@ export declare class ValueErrorsUnknownTypeError extends Error {
|
|
|
75
75
|
readonly schema: TSchema;
|
|
76
76
|
constructor(schema: TSchema);
|
|
77
77
|
}
|
|
78
|
-
export declare function EscapeKey(key: string): string;
|
|
79
78
|
export declare class ValueErrorIterator {
|
|
80
79
|
private readonly iterator;
|
|
81
80
|
constructor(iterator: IterableIterator<ValueError>);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Errors = exports.ValueErrorIterator = exports.
|
|
3
|
+
exports.Errors = exports.ValueErrorIterator = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
|
|
4
4
|
const index_1 = require("../value/guard/index");
|
|
5
5
|
const index_2 = require("../system/index");
|
|
6
6
|
const index_3 = require("../type/keyof/index");
|
|
7
7
|
const index_4 = require("../type/registry/index");
|
|
8
8
|
const extends_undefined_1 = require("../type/extends/extends-undefined");
|
|
9
|
+
const function_1 = require("./function");
|
|
9
10
|
const index_5 = require("../value/deref/index");
|
|
10
11
|
const index_6 = require("../value/hash/index");
|
|
11
12
|
const index_7 = require("../type/symbols/index");
|
|
@@ -86,7 +87,6 @@ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
|
86
87
|
function EscapeKey(key) {
|
|
87
88
|
return key.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
88
89
|
}
|
|
89
|
-
exports.EscapeKey = EscapeKey;
|
|
90
90
|
function IsDefined(value) {
|
|
91
91
|
return value !== undefined;
|
|
92
92
|
}
|
|
@@ -104,7 +104,7 @@ class ValueErrorIterator {
|
|
|
104
104
|
}
|
|
105
105
|
exports.ValueErrorIterator = ValueErrorIterator;
|
|
106
106
|
function Create(type, schema, path, value) {
|
|
107
|
-
return { type, schema, path, value, message:
|
|
107
|
+
return { type, schema, path, value, message: (0, function_1.GetErrorFunction)()(schema, type) };
|
|
108
108
|
}
|
|
109
109
|
function* TAny(schema, references, path, value) { }
|
|
110
110
|
function* TArray(schema, references, path, value) {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { ValueErrorType } from '../errors/errors';
|
|
2
1
|
import { TSchema } from '../type/schema/index';
|
|
3
|
-
|
|
4
|
-
export declare namespace TypeSystemErrorFunction {
|
|
5
|
-
function Reset(): void;
|
|
6
|
-
function Set(callback: ErrorFunction): void;
|
|
7
|
-
function Get(): ErrorFunction;
|
|
8
|
-
}
|
|
2
|
+
import { ValueErrorType } from './errors';
|
|
9
3
|
export declare function DefaultErrorFunction(schema: TSchema, errorType: ValueErrorType): string;
|
|
4
|
+
export type ErrorFunction = (schema: TSchema, type: ValueErrorType) => string;
|
|
5
|
+
export declare function SetErrorFunction(callback: ErrorFunction): void;
|
|
6
|
+
export declare function GetErrorFunction(): ErrorFunction;
|
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const errors_1 = require("../errors/errors");
|
|
3
|
+
exports.GetErrorFunction = exports.SetErrorFunction = exports.DefaultErrorFunction = void 0;
|
|
5
4
|
const index_1 = require("../type/symbols/index");
|
|
6
|
-
|
|
7
|
-
(function (TypeSystemErrorFunction) {
|
|
8
|
-
let errorMessageFunction = DefaultErrorFunction;
|
|
9
|
-
function Reset() {
|
|
10
|
-
errorMessageFunction = DefaultErrorFunction;
|
|
11
|
-
}
|
|
12
|
-
TypeSystemErrorFunction.Reset = Reset;
|
|
13
|
-
function Set(callback) {
|
|
14
|
-
errorMessageFunction = callback;
|
|
15
|
-
}
|
|
16
|
-
TypeSystemErrorFunction.Set = Set;
|
|
17
|
-
function Get() {
|
|
18
|
-
return errorMessageFunction;
|
|
19
|
-
}
|
|
20
|
-
TypeSystemErrorFunction.Get = Get;
|
|
21
|
-
})(TypeSystemErrorFunction || (exports.TypeSystemErrorFunction = TypeSystemErrorFunction = {}));
|
|
5
|
+
const errors_1 = require("./errors");
|
|
22
6
|
function DefaultErrorFunction(schema, errorType) {
|
|
23
7
|
switch (errorType) {
|
|
24
8
|
case errors_1.ValueErrorType.ArrayContains:
|
|
@@ -152,3 +136,12 @@ function DefaultErrorFunction(schema, errorType) {
|
|
|
152
136
|
}
|
|
153
137
|
}
|
|
154
138
|
exports.DefaultErrorFunction = DefaultErrorFunction;
|
|
139
|
+
let errorFunction = DefaultErrorFunction;
|
|
140
|
+
function SetErrorFunction(callback) {
|
|
141
|
+
errorFunction = callback;
|
|
142
|
+
}
|
|
143
|
+
exports.SetErrorFunction = SetErrorFunction;
|
|
144
|
+
function GetErrorFunction() {
|
|
145
|
+
return errorFunction;
|
|
146
|
+
}
|
|
147
|
+
exports.GetErrorFunction = GetErrorFunction;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { Errors, ValueError, ValueErrorIterator, ValueErrorType, ValueErrorsUnknownTypeError } from './errors';
|
|
2
|
+
export { DefaultErrorFunction, GetErrorFunction, SetErrorFunction, type ErrorFunction } from './function';
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.SetErrorFunction = exports.GetErrorFunction = exports.DefaultErrorFunction = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = exports.ValueErrorIterator = exports.Errors = void 0;
|
|
4
|
+
var errors_1 = require("./errors");
|
|
5
|
+
Object.defineProperty(exports, "Errors", { enumerable: true, get: function () { return errors_1.Errors; } });
|
|
6
|
+
Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return errors_1.ValueErrorIterator; } });
|
|
7
|
+
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return errors_1.ValueErrorType; } });
|
|
8
|
+
Object.defineProperty(exports, "ValueErrorsUnknownTypeError", { enumerable: true, get: function () { return errors_1.ValueErrorsUnknownTypeError; } });
|
|
9
|
+
var function_1 = require("./function");
|
|
10
|
+
Object.defineProperty(exports, "DefaultErrorFunction", { enumerable: true, get: function () { return function_1.DefaultErrorFunction; } });
|
|
11
|
+
Object.defineProperty(exports, "GetErrorFunction", { enumerable: true, get: function () { return function_1.GetErrorFunction; } });
|
|
12
|
+
Object.defineProperty(exports, "SetErrorFunction", { enumerable: true, get: function () { return function_1.SetErrorFunction; } });
|
package/build/require/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export { PatternBoolean, PatternBooleanExact, PatternNumber, PatternNumberExact,
|
|
|
3
3
|
export { TypeRegistry, FormatRegistry } from './type/registry/index';
|
|
4
4
|
export { TypeGuard, ValueGuard } from './type/guard/index';
|
|
5
5
|
export { CloneType, CloneRest } from './type/clone/type';
|
|
6
|
-
export { Clone } from './type/clone/value';
|
|
7
6
|
export { Any, type TAny } from './type/any/index';
|
|
8
7
|
export { Array, type TArray, type ArrayOptions } from './type/array/index';
|
|
9
8
|
export { AsyncIterator, type TAsyncIterator } from './type/async-iterator/index';
|
package/build/require/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Uppercase = exports.Uncapitalize = exports.Lowercase = exports.Capitalize = exports.Iterator = exports.Intersect = exports.Integer = exports.InstanceType = exports.IndexedTypeResolve = exports.IndexedKeyResolve = exports.Index = exports.Increment = exports.Function = exports.Extract = exports.ExtendsUndefinedCheck = exports.ExtendsResult = exports.ExtendsCheck = exports.Extends = exports.Exclude = exports.Enum = exports.Deref = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Any = exports.
|
|
4
|
-
exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.Type = exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.Transform = exports.TemplateLiteralGenerate = exports.TemplateLiteralParseExact = exports.TemplateLiteralParse = exports.IsTemplateLiteralFinite = exports.TemplateLiteral = exports.Symbol = exports.String = exports.Strict = exports.ReturnType = exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.Readonly = exports.Promise = exports.Pick = exports.Partial = exports.Parameters = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.Mapped = exports.Literal = exports.KeyOfTypeResolve = exports.KeyOfStringResolvePattern = exports.KeyOfStringResolve = exports.KeyOf =
|
|
3
|
+
exports.IntrinsicResolve = exports.Uppercase = exports.Uncapitalize = exports.Lowercase = exports.Capitalize = exports.Iterator = exports.Intersect = exports.Integer = exports.InstanceType = exports.IndexedTypeResolve = exports.IndexedKeyResolve = exports.Index = exports.Increment = exports.Function = exports.Extract = exports.ExtendsUndefinedCheck = exports.ExtendsResult = exports.ExtendsCheck = exports.Extends = exports.Exclude = exports.Enum = exports.Deref = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Any = exports.CloneRest = exports.CloneType = exports.ValueGuard = exports.TypeGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternString = exports.PatternNumberExact = exports.PatternNumber = exports.PatternBooleanExact = exports.PatternBoolean = exports.TransformKind = exports.OptionalKind = exports.ReadonlyKind = exports.Hint = exports.Kind = void 0;
|
|
4
|
+
exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.Type = exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.Transform = exports.TemplateLiteralGenerate = exports.TemplateLiteralParseExact = exports.TemplateLiteralParse = exports.IsTemplateLiteralFinite = exports.TemplateLiteral = exports.Symbol = exports.String = exports.Strict = exports.ReturnType = exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.Readonly = exports.Promise = exports.Pick = exports.Partial = exports.Parameters = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.Mapped = exports.Literal = exports.KeyOfTypeResolve = exports.KeyOfStringResolvePattern = exports.KeyOfStringResolve = exports.KeyOf = void 0;
|
|
5
5
|
var index_1 = require("./type/symbols/index");
|
|
6
6
|
Object.defineProperty(exports, "Kind", { enumerable: true, get: function () { return index_1.Kind; } });
|
|
7
7
|
Object.defineProperty(exports, "Hint", { enumerable: true, get: function () { return index_1.Hint; } });
|
|
@@ -24,8 +24,6 @@ Object.defineProperty(exports, "ValueGuard", { enumerable: true, get: function (
|
|
|
24
24
|
var type_1 = require("./type/clone/type");
|
|
25
25
|
Object.defineProperty(exports, "CloneType", { enumerable: true, get: function () { return type_1.CloneType; } });
|
|
26
26
|
Object.defineProperty(exports, "CloneRest", { enumerable: true, get: function () { return type_1.CloneRest; } });
|
|
27
|
-
var value_1 = require("./type/clone/value");
|
|
28
|
-
Object.defineProperty(exports, "Clone", { enumerable: true, get: function () { return value_1.Clone; } });
|
|
29
27
|
var index_5 = require("./type/any/index");
|
|
30
28
|
Object.defineProperty(exports, "Any", { enumerable: true, get: function () { return index_5.Any; } });
|
|
31
29
|
var index_6 = require("./type/array/index");
|
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export { ValueErrorType } from '../errors/errors';
|
|
2
|
-
export { DefaultErrorFunction, ErrorFunction, TypeSystemErrorFunction } from './errors';
|
|
3
1
|
export { TypeSystemPolicy } from './policy';
|
|
4
2
|
export { TypeSystem, TypeSystemDuplicateFormat, TypeSystemDuplicateTypeKind } from './system';
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TypeSystemDuplicateTypeKind = exports.TypeSystemDuplicateFormat = exports.TypeSystem = exports.TypeSystemPolicy =
|
|
4
|
-
var errors_1 = require("../errors/errors");
|
|
5
|
-
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return errors_1.ValueErrorType; } });
|
|
6
|
-
var errors_2 = require("./errors");
|
|
7
|
-
Object.defineProperty(exports, "DefaultErrorFunction", { enumerable: true, get: function () { return errors_2.DefaultErrorFunction; } });
|
|
8
|
-
Object.defineProperty(exports, "TypeSystemErrorFunction", { enumerable: true, get: function () { return errors_2.TypeSystemErrorFunction; } });
|
|
3
|
+
exports.TypeSystemDuplicateTypeKind = exports.TypeSystemDuplicateFormat = exports.TypeSystem = exports.TypeSystemPolicy = void 0;
|
|
9
4
|
var policy_1 = require("./policy");
|
|
10
5
|
Object.defineProperty(exports, "TypeSystemPolicy", { enumerable: true, get: function () { return policy_1.TypeSystemPolicy; } });
|
|
11
6
|
var system_1 = require("./system");
|
|
@@ -36,47 +36,43 @@ class ValueCastUnknownTypeError extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
39
|
-
|
|
40
|
-
(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return acc + (literal + checks + exists);
|
|
52
|
-
}, 0);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
return (0, index_4.Check)(schema, references, value) ? 1 : 0;
|
|
56
|
-
}
|
|
39
|
+
function ScoreUnion(schema, references, value) {
|
|
40
|
+
if (schema[index_2.Kind] === 'Object' && typeof value === 'object' && !(0, index_1.IsNull)(value)) {
|
|
41
|
+
const object = schema;
|
|
42
|
+
const keys = Object.getOwnPropertyNames(value);
|
|
43
|
+
const entries = Object.entries(object.properties);
|
|
44
|
+
const [point, max] = [1 / entries.length, entries.length];
|
|
45
|
+
return entries.reduce((acc, [key, schema]) => {
|
|
46
|
+
const literal = schema[index_2.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
47
|
+
const checks = (0, index_4.Check)(schema, references, value[key]) ? point : 0;
|
|
48
|
+
const exists = keys.includes(key) ? point : 0;
|
|
49
|
+
return acc + (literal + checks + exists);
|
|
50
|
+
}, 0);
|
|
57
51
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
for (const schema of union.anyOf) {
|
|
61
|
-
const score = Score(schema, references, value);
|
|
62
|
-
if (score > best) {
|
|
63
|
-
select = schema;
|
|
64
|
-
best = score;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return select;
|
|
52
|
+
else {
|
|
53
|
+
return (0, index_4.Check)(schema, references, value) ? 1 : 0;
|
|
68
54
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
55
|
+
}
|
|
56
|
+
function SelectUnion(union, references, value) {
|
|
57
|
+
let [select, best] = [union.anyOf[0], 0];
|
|
58
|
+
for (const schema of union.anyOf) {
|
|
59
|
+
const score = ScoreUnion(schema, references, value);
|
|
60
|
+
if (score > best) {
|
|
61
|
+
select = schema;
|
|
62
|
+
best = score;
|
|
76
63
|
}
|
|
77
64
|
}
|
|
78
|
-
|
|
79
|
-
}
|
|
65
|
+
return select;
|
|
66
|
+
}
|
|
67
|
+
function CastUnion(union, references, value) {
|
|
68
|
+
if ('default' in union) {
|
|
69
|
+
return union.default;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
const schema = SelectUnion(union, references, value);
|
|
73
|
+
return Cast(schema, references, value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
80
76
|
function DefaultClone(schema, references, value) {
|
|
81
77
|
return (0, index_4.Check)(schema, references, value) ? (0, index_5.Clone)(value) : (0, index_3.Create)(schema, references);
|
|
82
78
|
}
|
|
@@ -168,7 +164,7 @@ function TTuple(schema, references, value) {
|
|
|
168
164
|
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
169
165
|
}
|
|
170
166
|
function TUnion(schema, references, value) {
|
|
171
|
-
return (0, index_4.Check)(schema, references, value) ? (0, index_5.Clone)(value) :
|
|
167
|
+
return (0, index_4.Check)(schema, references, value) ? (0, index_5.Clone)(value) : CastUnion(schema, references, value);
|
|
172
168
|
}
|
|
173
169
|
function Visit(schema, references, value) {
|
|
174
170
|
const references_ = (0, index_1.IsString)(schema.$id) ? [...references, schema] : references;
|
package/license
CHANGED
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -103,8 +103,8 @@ License MIT
|
|
|
103
103
|
- [Ajv](#typecheck-ajv)
|
|
104
104
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
105
105
|
- [TypeSystem](#typesystem)
|
|
106
|
-
- [Errors](#typesystem-errors)
|
|
107
106
|
- [Policies](#typesystem-policies)
|
|
107
|
+
- [Error Function](#error-function)
|
|
108
108
|
- [Workbench](#workbench)
|
|
109
109
|
- [Codegen](#codegen)
|
|
110
110
|
- [Ecosystem](#ecosystem)
|
|
@@ -1527,49 +1527,6 @@ const C = TypeCompiler.Code(Type.String()) // const C = `return functi
|
|
|
1527
1527
|
|
|
1528
1528
|
The TypeBox TypeSystem module provides functionality to define types above and beyond the built-in Json and JavaScript type sets. They also manage TypeBox's localization options (i18n) for error message generation and can control various assertion policies used when type checking. Configurations made to the TypeSystem module are observed by the TypeCompiler, Value and Error modules.
|
|
1529
1529
|
|
|
1530
|
-
<a name='typesystem-errors'></a>
|
|
1531
|
-
|
|
1532
|
-
### Errors
|
|
1533
|
-
|
|
1534
|
-
Use the TypeSystemErrorFunction to override validation error messages. This can be used to localize errors or create error messages for user defined types.
|
|
1535
|
-
|
|
1536
|
-
```typescript
|
|
1537
|
-
import { TypeSystemErrorFunction, ValueErrorType, DefaultErrorFunction } from '@sinclair/typebox/system'
|
|
1538
|
-
|
|
1539
|
-
TypeSystemErrorFunction.Set((schema, errorType) => { // i18n override
|
|
1540
|
-
switch(errorType) {
|
|
1541
|
-
/* en-US */ case ValueErrorType.String: return 'Expected string'
|
|
1542
|
-
/* fr-FR */ case ValueErrorType.Number: return 'Nombre attendu'
|
|
1543
|
-
/* ko-KR */ case ValueErrorType.Boolean: return '예상 부울'
|
|
1544
|
-
/* en-US */ default: return DefaultErrorFunction(schema, errorType)
|
|
1545
|
-
}
|
|
1546
|
-
})
|
|
1547
|
-
const T = Type.Object({ // const T = { ... }
|
|
1548
|
-
x: Type.String(),
|
|
1549
|
-
y: Type.Number(),
|
|
1550
|
-
z: Type.Boolean()
|
|
1551
|
-
})
|
|
1552
|
-
const E = [...Value.Errors(T, { // const E = [{
|
|
1553
|
-
x: null, // type: 48,
|
|
1554
|
-
y: null, // schema: { ... },
|
|
1555
|
-
z: null // path: '/x',
|
|
1556
|
-
})] // value: null,
|
|
1557
|
-
// message: 'Expected string'
|
|
1558
|
-
// }, {
|
|
1559
|
-
// type: 34,
|
|
1560
|
-
// schema: { ... },
|
|
1561
|
-
// path: '/y',
|
|
1562
|
-
// value: null,
|
|
1563
|
-
// message: 'Nombre attendu'
|
|
1564
|
-
// }, {
|
|
1565
|
-
// type: 14,
|
|
1566
|
-
// schema: { ... },
|
|
1567
|
-
// path: '/z',
|
|
1568
|
-
// value: null,
|
|
1569
|
-
// message: '예상 부울'
|
|
1570
|
-
// }]
|
|
1571
|
-
```
|
|
1572
|
-
|
|
1573
1530
|
<a name='typesystem-policies'></a>
|
|
1574
1531
|
|
|
1575
1532
|
### Policies
|
|
@@ -1606,6 +1563,53 @@ TypeSystemPolicy.AllowNaN = true
|
|
|
1606
1563
|
TypeSystemPolicy.AllowNullVoid = true
|
|
1607
1564
|
```
|
|
1608
1565
|
|
|
1566
|
+
<a name='error-function'></a>
|
|
1567
|
+
|
|
1568
|
+
## Error Function
|
|
1569
|
+
|
|
1570
|
+
Error messages in TypeBox can be customized by defining an ErrorFunction. This function allows for the localization of error messages as well as enabling custom error messages for custom types. By default, TypeBox will generate messages using the `en-US` locale. To support additional locales, you can replicate the function found in `src/errors/function.ts` and create a locale specific translations. The function can then be set via SetErrorFunction.
|
|
1571
|
+
|
|
1572
|
+
The following example shows an inline error function that intercepts errors for String, Number and Boolean only. The DefaultErrorFunction is used to return a default error message.
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
```typescript
|
|
1576
|
+
import { SetErrorFunction, DefaultErrorFunction, ValueErrorType } from '@sinclair/typebox/errors'
|
|
1577
|
+
|
|
1578
|
+
SetErrorFunction((schema, errorType) => { // i18n override
|
|
1579
|
+
switch(errorType) {
|
|
1580
|
+
/* en-US */ case ValueErrorType.String: return 'Expected string'
|
|
1581
|
+
/* fr-FR */ case ValueErrorType.Number: return 'Nombre attendu'
|
|
1582
|
+
/* ko-KR */ case ValueErrorType.Boolean: return '예상 부울'
|
|
1583
|
+
/* en-US */ default: return DefaultErrorFunction(schema, errorType)
|
|
1584
|
+
}
|
|
1585
|
+
})
|
|
1586
|
+
const T = Type.Object({ // const T: TObject<{
|
|
1587
|
+
x: Type.String(), // TString,
|
|
1588
|
+
y: Type.Number(), // TNumber,
|
|
1589
|
+
z: Type.Boolean() // TBoolean
|
|
1590
|
+
}) // }>
|
|
1591
|
+
|
|
1592
|
+
const E = [...Value.Errors(T, { // const E = [{
|
|
1593
|
+
x: null, // type: 48,
|
|
1594
|
+
y: null, // schema: { ... },
|
|
1595
|
+
z: null // path: '/x',
|
|
1596
|
+
})] // value: null,
|
|
1597
|
+
// message: 'Expected string'
|
|
1598
|
+
// }, {
|
|
1599
|
+
// type: 34,
|
|
1600
|
+
// schema: { ... },
|
|
1601
|
+
// path: '/y',
|
|
1602
|
+
// value: null,
|
|
1603
|
+
// message: 'Nombre attendu'
|
|
1604
|
+
// }, {
|
|
1605
|
+
// type: 14,
|
|
1606
|
+
// schema: { ... },
|
|
1607
|
+
// path: '/z',
|
|
1608
|
+
// value: null,
|
|
1609
|
+
// message: '예상 부울'
|
|
1610
|
+
// }]
|
|
1611
|
+
```
|
|
1612
|
+
|
|
1609
1613
|
<a name='workbench'></a>
|
|
1610
1614
|
|
|
1611
1615
|
## TypeBox Workbench
|