@sinclair/typebox 0.32.0-dev-22 → 0.32.0-dev-24
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/compiler.d.mts +3 -2
- package/build/import/compiler/compiler.mjs +3 -2
- package/build/import/errors/errors.d.mts +2 -1
- package/build/import/errors/errors.mjs +2 -1
- package/build/import/index.d.mts +1 -0
- package/build/import/index.mjs +1 -0
- package/build/import/system/system.d.mts +3 -2
- package/build/import/system/system.mjs +3 -2
- package/build/import/type/error/error.d.mts +4 -0
- package/build/import/type/error/error.mjs +6 -0
- package/build/import/type/error/index.d.mts +1 -0
- package/build/import/type/error/index.mjs +1 -0
- package/build/import/type/extends/extends-check.d.mts +2 -1
- package/build/import/type/extends/extends-check.mjs +2 -1
- package/build/import/type/guard/type.d.mts +2 -1
- package/build/import/type/guard/type.mjs +2 -1
- package/build/import/type/index.d.mts +1 -0
- package/build/import/type/index.mjs +1 -0
- package/build/import/type/record/record.d.mts +1 -1
- package/build/import/type/template-literal/finite.d.mts +2 -1
- package/build/import/type/template-literal/finite.mjs +2 -1
- package/build/import/type/template-literal/generate.d.mts +3 -2
- package/build/import/type/template-literal/generate.mjs +2 -1
- package/build/import/type/template-literal/parse.d.mts +2 -1
- package/build/import/type/template-literal/parse.mjs +2 -1
- package/build/import/type/template-literal/pattern.d.mts +2 -1
- package/build/import/type/template-literal/pattern.mjs +2 -1
- package/build/import/value/cast/cast.d.mts +5 -4
- package/build/import/value/cast/cast.mjs +5 -4
- package/build/import/value/check/check.d.mts +2 -1
- package/build/import/value/check/check.mjs +2 -1
- package/build/import/value/convert/convert.d.mts +2 -1
- package/build/import/value/convert/convert.mjs +2 -1
- package/build/import/value/create/create.d.mts +7 -6
- package/build/import/value/create/create.mjs +7 -6
- package/build/import/value/delta/delta.d.mts +3 -2
- package/build/import/value/delta/delta.mjs +3 -2
- package/build/import/value/deref/deref.d.mts +2 -1
- package/build/import/value/deref/deref.mjs +2 -1
- package/build/import/value/hash/hash.d.mts +2 -1
- package/build/import/value/hash/hash.mjs +2 -1
- package/build/import/value/mutate/mutate.d.mts +3 -2
- package/build/import/value/mutate/mutate.mjs +3 -2
- package/build/import/value/pointer/pointer.d.mts +3 -2
- package/build/import/value/pointer/pointer.mjs +3 -2
- package/build/import/value/transform/decode.d.mts +3 -2
- package/build/import/value/transform/decode.mjs +3 -2
- package/build/import/value/transform/encode.d.mts +3 -2
- package/build/import/value/transform/encode.mjs +3 -2
- package/build/require/compiler/compiler.d.ts +3 -2
- package/build/require/compiler/compiler.js +61 -60
- package/build/require/errors/errors.d.ts +2 -1
- package/build/require/errors/errors.js +34 -33
- package/build/require/index.d.ts +1 -0
- package/build/require/index.js +154 -152
- package/build/require/system/system.d.ts +3 -2
- package/build/require/system/system.js +3 -2
- package/build/require/type/error/error.d.ts +4 -0
- package/build/require/type/error/error.js +11 -0
- package/build/require/type/error/index.d.ts +1 -0
- package/build/require/type/error/index.js +18 -0
- package/build/require/type/extends/extends-check.d.ts +2 -1
- package/build/require/type/extends/extends-check.js +172 -171
- package/build/require/type/guard/type.d.ts +2 -1
- package/build/require/type/guard/type.js +2 -1
- package/build/require/type/index.d.ts +1 -0
- package/build/require/type/index.js +1 -0
- package/build/require/type/record/record.d.ts +1 -1
- package/build/require/type/template-literal/finite.d.ts +2 -1
- package/build/require/type/template-literal/finite.js +2 -1
- package/build/require/type/template-literal/generate.d.ts +3 -2
- package/build/require/type/template-literal/generate.js +2 -1
- package/build/require/type/template-literal/parse.d.ts +2 -1
- package/build/require/type/template-literal/parse.js +3 -2
- package/build/require/type/template-literal/pattern.d.ts +2 -1
- package/build/require/type/template-literal/pattern.js +2 -1
- package/build/require/value/cast/cast.d.ts +5 -4
- package/build/require/value/cast/cast.js +36 -35
- package/build/require/value/check/check.d.ts +2 -1
- package/build/require/value/check/check.js +24 -23
- package/build/require/value/convert/convert.d.ts +2 -1
- package/build/require/value/convert/convert.js +32 -31
- package/build/require/value/create/create.d.ts +7 -6
- package/build/require/value/create/create.js +7 -6
- package/build/require/value/delta/delta.d.ts +3 -2
- package/build/require/value/delta/delta.js +20 -19
- package/build/require/value/deref/deref.d.ts +2 -1
- package/build/require/value/deref/deref.js +2 -1
- package/build/require/value/hash/hash.d.ts +2 -1
- package/build/require/value/hash/hash.js +2 -1
- package/build/require/value/mutate/mutate.d.ts +3 -2
- package/build/require/value/mutate/mutate.js +3 -2
- package/build/require/value/pointer/pointer.d.ts +3 -2
- package/build/require/value/pointer/pointer.js +3 -2
- package/build/require/value/transform/decode.d.ts +3 -2
- package/build/require/value/transform/decode.js +19 -18
- package/build/require/value/transform/encode.d.ts +3 -2
- package/build/require/value/transform/encode.js +20 -19
- package/package.json +1 -1
- package/readme.md +117 -137
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { IsArray, IsBoolean, IsBigInt, IsDate, IsNull, IsNumber, IsPlainObject, IsString, IsSymbol, IsUint8Array, IsUndefined } from '../guard/index.mjs';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
3
|
// ------------------------------------------------------------------
|
|
3
4
|
// Errors
|
|
4
5
|
// ------------------------------------------------------------------
|
|
5
|
-
export class ValueHashError extends
|
|
6
|
+
export class ValueHashError extends TypeBoxError {
|
|
6
7
|
value;
|
|
7
8
|
constructor(value) {
|
|
8
9
|
super(`Unable to hash value`);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
|
+
export declare class ValueMutateTypeMismatchError extends TypeBoxError {
|
|
2
3
|
constructor();
|
|
3
4
|
}
|
|
4
|
-
export declare class ValueMutateInvalidRootMutationError extends
|
|
5
|
+
export declare class ValueMutateInvalidRootMutationError extends TypeBoxError {
|
|
5
6
|
constructor();
|
|
6
7
|
}
|
|
7
8
|
export type Mutable = {
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { IsPlainObject, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
2
2
|
import { ValuePointer } from '../pointer/index.mjs';
|
|
3
3
|
import { Clone } from '../clone/index.mjs';
|
|
4
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
4
5
|
// ------------------------------------------------------------------
|
|
5
6
|
// Errors
|
|
6
7
|
// ------------------------------------------------------------------
|
|
7
|
-
export class ValueMutateTypeMismatchError extends
|
|
8
|
+
export class ValueMutateTypeMismatchError extends TypeBoxError {
|
|
8
9
|
constructor() {
|
|
9
10
|
super('Cannot assign due type mismatch of assignable values');
|
|
10
11
|
}
|
|
11
12
|
}
|
|
12
|
-
export class ValueMutateInvalidRootMutationError extends
|
|
13
|
+
export class ValueMutateInvalidRootMutationError extends TypeBoxError {
|
|
13
14
|
constructor() {
|
|
14
15
|
super('Only object and array types can be mutated at the root level');
|
|
15
16
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
|
+
export declare class ValuePointerRootSetError extends TypeBoxError {
|
|
2
3
|
readonly value: unknown;
|
|
3
4
|
readonly path: string;
|
|
4
5
|
readonly update: unknown;
|
|
5
6
|
constructor(value: unknown, path: string, update: unknown);
|
|
6
7
|
}
|
|
7
|
-
export declare class ValuePointerRootDeleteError extends
|
|
8
|
+
export declare class ValuePointerRootDeleteError extends TypeBoxError {
|
|
8
9
|
readonly value: unknown;
|
|
9
10
|
readonly path: string;
|
|
10
11
|
constructor(value: unknown, path: string);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
1
2
|
// ------------------------------------------------------------------
|
|
2
3
|
// Errors
|
|
3
4
|
// ------------------------------------------------------------------
|
|
4
|
-
export class ValuePointerRootSetError extends
|
|
5
|
+
export class ValuePointerRootSetError extends TypeBoxError {
|
|
5
6
|
value;
|
|
6
7
|
path;
|
|
7
8
|
update;
|
|
@@ -12,7 +13,7 @@ export class ValuePointerRootSetError extends Error {
|
|
|
12
13
|
this.update = update;
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
|
-
export class ValuePointerRootDeleteError extends
|
|
16
|
+
export class ValuePointerRootDeleteError extends TypeBoxError {
|
|
16
17
|
value;
|
|
17
18
|
path;
|
|
18
19
|
constructor(value, path) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
1
2
|
import { ValueError } from '../../errors/index.mjs';
|
|
2
3
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
3
|
-
export declare class TransformDecodeCheckError extends
|
|
4
|
+
export declare class TransformDecodeCheckError extends TypeBoxError {
|
|
4
5
|
readonly schema: TSchema;
|
|
5
6
|
readonly value: unknown;
|
|
6
7
|
readonly error: ValueError;
|
|
7
8
|
constructor(schema: TSchema, value: unknown, error: ValueError);
|
|
8
9
|
}
|
|
9
|
-
export declare class TransformDecodeError extends
|
|
10
|
+
export declare class TransformDecodeError extends TypeBoxError {
|
|
10
11
|
readonly schema: TSchema;
|
|
11
12
|
readonly value: unknown;
|
|
12
13
|
constructor(schema: TSchema, value: unknown, error: any);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Kind, TransformKind } from '../../type/symbols/index.mjs';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
3
|
import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
|
|
3
4
|
import { Index } from '../../type/indexed/index.mjs';
|
|
4
5
|
import { Deref } from '../deref/index.mjs';
|
|
@@ -15,7 +16,7 @@ import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
|
15
16
|
// Errors
|
|
16
17
|
// ------------------------------------------------------------------
|
|
17
18
|
// thrown externally
|
|
18
|
-
export class TransformDecodeCheckError extends
|
|
19
|
+
export class TransformDecodeCheckError extends TypeBoxError {
|
|
19
20
|
schema;
|
|
20
21
|
value;
|
|
21
22
|
error;
|
|
@@ -26,7 +27,7 @@ export class TransformDecodeCheckError extends Error {
|
|
|
26
27
|
this.error = error;
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
export class TransformDecodeError extends
|
|
30
|
+
export class TransformDecodeError extends TypeBoxError {
|
|
30
31
|
schema;
|
|
31
32
|
value;
|
|
32
33
|
constructor(schema, value, error) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
1
2
|
import { ValueError } from '../../errors/index.mjs';
|
|
2
3
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
3
|
-
export declare class TransformEncodeCheckError extends
|
|
4
|
+
export declare class TransformEncodeCheckError extends TypeBoxError {
|
|
4
5
|
readonly schema: TSchema;
|
|
5
6
|
readonly value: unknown;
|
|
6
7
|
readonly error: ValueError;
|
|
7
8
|
constructor(schema: TSchema, value: unknown, error: ValueError);
|
|
8
9
|
}
|
|
9
|
-
export declare class TransformEncodeError extends
|
|
10
|
+
export declare class TransformEncodeError extends TypeBoxError {
|
|
10
11
|
readonly schema: TSchema;
|
|
11
12
|
readonly value: unknown;
|
|
12
13
|
constructor(schema: TSchema, value: unknown, error: any);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Kind, TransformKind } from '../../type/symbols/index.mjs';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
3
|
import { KeyOfPropertyKeys } from '../../type/keyof/index.mjs';
|
|
3
4
|
import { Index } from '../../type/indexed/index.mjs';
|
|
4
5
|
import { Deref } from '../deref/index.mjs';
|
|
@@ -14,7 +15,7 @@ import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
|
14
15
|
// ------------------------------------------------------------------
|
|
15
16
|
// Errors
|
|
16
17
|
// ------------------------------------------------------------------
|
|
17
|
-
export class TransformEncodeCheckError extends
|
|
18
|
+
export class TransformEncodeCheckError extends TypeBoxError {
|
|
18
19
|
schema;
|
|
19
20
|
value;
|
|
20
21
|
error;
|
|
@@ -25,7 +26,7 @@ export class TransformEncodeCheckError extends Error {
|
|
|
25
26
|
this.error = error;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
|
-
export class TransformEncodeError extends
|
|
29
|
+
export class TransformEncodeError extends TypeBoxError {
|
|
29
30
|
schema;
|
|
30
31
|
value;
|
|
31
32
|
constructor(schema, value, error) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ValueErrorIterator } from '../errors/index';
|
|
2
|
+
import { TypeBoxError } from '../type/error/index';
|
|
2
3
|
import type { TSchema } from '../type/schema/index';
|
|
3
4
|
import type { Static, StaticDecode, StaticEncode } from '../type/static/index';
|
|
4
5
|
export type CheckFunction = (value: unknown) => boolean;
|
|
@@ -20,11 +21,11 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
20
21
|
/** Encodes a value or throws if error */
|
|
21
22
|
Encode(value: unknown): StaticEncode<T>;
|
|
22
23
|
}
|
|
23
|
-
export declare class TypeCompilerUnknownTypeError extends
|
|
24
|
+
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
24
25
|
readonly schema: TSchema;
|
|
25
26
|
constructor(schema: TSchema);
|
|
26
27
|
}
|
|
27
|
-
export declare class TypeCompilerTypeGuardError extends
|
|
28
|
+
export declare class TypeCompilerTypeGuardError extends TypeBoxError {
|
|
28
29
|
readonly schema: TSchema;
|
|
29
30
|
constructor(schema: TSchema);
|
|
30
31
|
}
|
|
@@ -5,17 +5,18 @@ exports.TypeCompiler = exports.Policy = exports.TypeCompilerTypeGuardError = exp
|
|
|
5
5
|
const index_1 = require("../value/transform/index");
|
|
6
6
|
const index_2 = require("../errors/index");
|
|
7
7
|
const index_3 = require("../system/index");
|
|
8
|
-
const index_4 = require("../
|
|
9
|
-
const index_5 = require("../value/
|
|
10
|
-
const index_6 = require("../
|
|
11
|
-
const index_7 = require("../type/
|
|
12
|
-
const index_8 = require("../type/
|
|
8
|
+
const index_4 = require("../type/error/index");
|
|
9
|
+
const index_5 = require("../value/deref/index");
|
|
10
|
+
const index_6 = require("../value/hash/index");
|
|
11
|
+
const index_7 = require("../type/symbols/index");
|
|
12
|
+
const index_8 = require("../type/registry/index");
|
|
13
|
+
const index_9 = require("../type/keyof/index");
|
|
13
14
|
const extends_undefined_1 = require("../type/extends/extends-undefined");
|
|
14
|
-
const
|
|
15
|
+
const index_10 = require("../type/never/index");
|
|
15
16
|
// ------------------------------------------------------------------
|
|
16
17
|
// ValueGuard
|
|
17
18
|
// ------------------------------------------------------------------
|
|
18
|
-
const
|
|
19
|
+
const index_11 = require("../value/guard/index");
|
|
19
20
|
// ------------------------------------------------------------------
|
|
20
21
|
// TypeGuard
|
|
21
22
|
// ------------------------------------------------------------------
|
|
@@ -142,14 +143,14 @@ var LiteralString;
|
|
|
142
143
|
// ------------------------------------------------------------------
|
|
143
144
|
// Errors
|
|
144
145
|
// ------------------------------------------------------------------
|
|
145
|
-
class TypeCompilerUnknownTypeError extends
|
|
146
|
+
class TypeCompilerUnknownTypeError extends index_4.TypeBoxError {
|
|
146
147
|
constructor(schema) {
|
|
147
148
|
super('Unknown type');
|
|
148
149
|
this.schema = schema;
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
152
|
-
class TypeCompilerTypeGuardError extends
|
|
153
|
+
class TypeCompilerTypeGuardError extends index_4.TypeBoxError {
|
|
153
154
|
constructor(schema) {
|
|
154
155
|
super('Preflight validation check failed to guard for the given schema');
|
|
155
156
|
this.schema = schema;
|
|
@@ -191,7 +192,7 @@ var TypeCompiler;
|
|
|
191
192
|
// Guards
|
|
192
193
|
// ----------------------------------------------------------------
|
|
193
194
|
function IsAnyOrUnknown(schema) {
|
|
194
|
-
return schema[
|
|
195
|
+
return schema[index_7.Kind] === 'Any' || schema[index_7.Kind] === 'Unknown';
|
|
195
196
|
}
|
|
196
197
|
// ----------------------------------------------------------------
|
|
197
198
|
// Types
|
|
@@ -202,17 +203,17 @@ var TypeCompiler;
|
|
|
202
203
|
function* FromArray(schema, references, value) {
|
|
203
204
|
yield `Array.isArray(${value})`;
|
|
204
205
|
const [parameter, accumulator] = [CreateParameter('value', 'any'), CreateParameter('acc', 'number')];
|
|
205
|
-
if ((0,
|
|
206
|
+
if ((0, index_11.IsNumber)(schema.maxItems))
|
|
206
207
|
yield `${value}.length <= ${schema.maxItems}`;
|
|
207
|
-
if ((0,
|
|
208
|
+
if ((0, index_11.IsNumber)(schema.minItems))
|
|
208
209
|
yield `${value}.length >= ${schema.minItems}`;
|
|
209
210
|
const elementExpression = CreateExpression(schema.items, references, 'value');
|
|
210
211
|
yield `${value}.every((${parameter}) => ${elementExpression})`;
|
|
211
|
-
if ((0, type_1.IsSchema)(schema.contains) || (0,
|
|
212
|
-
const containsSchema = (0, type_1.IsSchema)(schema.contains) ? schema.contains : (0,
|
|
212
|
+
if ((0, type_1.IsSchema)(schema.contains) || (0, index_11.IsNumber)(schema.minContains) || (0, index_11.IsNumber)(schema.maxContains)) {
|
|
213
|
+
const containsSchema = (0, type_1.IsSchema)(schema.contains) ? schema.contains : (0, index_10.Never)();
|
|
213
214
|
const checkExpression = CreateExpression(containsSchema, references, 'value');
|
|
214
|
-
const checkMinContains = (0,
|
|
215
|
-
const checkMaxContains = (0,
|
|
215
|
+
const checkMinContains = (0, index_11.IsNumber)(schema.minContains) ? [`(count >= ${schema.minContains})`] : [];
|
|
216
|
+
const checkMaxContains = (0, index_11.IsNumber)(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : [];
|
|
216
217
|
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`;
|
|
217
218
|
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ');
|
|
218
219
|
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`;
|
|
@@ -228,15 +229,15 @@ var TypeCompiler;
|
|
|
228
229
|
}
|
|
229
230
|
function* FromBigInt(schema, references, value) {
|
|
230
231
|
yield `(typeof ${value} === 'bigint')`;
|
|
231
|
-
if ((0,
|
|
232
|
+
if ((0, index_11.IsBigInt)(schema.exclusiveMaximum))
|
|
232
233
|
yield `${value} < BigInt(${schema.exclusiveMaximum})`;
|
|
233
|
-
if ((0,
|
|
234
|
+
if ((0, index_11.IsBigInt)(schema.exclusiveMinimum))
|
|
234
235
|
yield `${value} > BigInt(${schema.exclusiveMinimum})`;
|
|
235
|
-
if ((0,
|
|
236
|
+
if ((0, index_11.IsBigInt)(schema.maximum))
|
|
236
237
|
yield `${value} <= BigInt(${schema.maximum})`;
|
|
237
|
-
if ((0,
|
|
238
|
+
if ((0, index_11.IsBigInt)(schema.minimum))
|
|
238
239
|
yield `${value} >= BigInt(${schema.minimum})`;
|
|
239
|
-
if ((0,
|
|
240
|
+
if ((0, index_11.IsBigInt)(schema.multipleOf))
|
|
240
241
|
yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
|
|
241
242
|
}
|
|
242
243
|
function* FromBoolean(schema, references, value) {
|
|
@@ -247,15 +248,15 @@ var TypeCompiler;
|
|
|
247
248
|
}
|
|
248
249
|
function* FromDate(schema, references, value) {
|
|
249
250
|
yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
|
|
250
|
-
if ((0,
|
|
251
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMaximumTimestamp))
|
|
251
252
|
yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
|
|
252
|
-
if ((0,
|
|
253
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMinimumTimestamp))
|
|
253
254
|
yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
|
|
254
|
-
if ((0,
|
|
255
|
+
if ((0, index_11.IsNumber)(schema.maximumTimestamp))
|
|
255
256
|
yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
|
|
256
|
-
if ((0,
|
|
257
|
+
if ((0, index_11.IsNumber)(schema.minimumTimestamp))
|
|
257
258
|
yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
|
|
258
|
-
if ((0,
|
|
259
|
+
if ((0, index_11.IsNumber)(schema.multipleOfTimestamp))
|
|
259
260
|
yield `(${value}.getTime() % ${schema.multipleOfTimestamp}) === 0`;
|
|
260
261
|
}
|
|
261
262
|
function* FromFunction(schema, references, value) {
|
|
@@ -263,26 +264,26 @@ var TypeCompiler;
|
|
|
263
264
|
}
|
|
264
265
|
function* FromInteger(schema, references, value) {
|
|
265
266
|
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
|
|
266
|
-
if ((0,
|
|
267
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMaximum))
|
|
267
268
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
268
|
-
if ((0,
|
|
269
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMinimum))
|
|
269
270
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
270
|
-
if ((0,
|
|
271
|
+
if ((0, index_11.IsNumber)(schema.maximum))
|
|
271
272
|
yield `${value} <= ${schema.maximum}`;
|
|
272
|
-
if ((0,
|
|
273
|
+
if ((0, index_11.IsNumber)(schema.minimum))
|
|
273
274
|
yield `${value} >= ${schema.minimum}`;
|
|
274
|
-
if ((0,
|
|
275
|
+
if ((0, index_11.IsNumber)(schema.multipleOf))
|
|
275
276
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
276
277
|
}
|
|
277
278
|
function* FromIntersect(schema, references, value) {
|
|
278
279
|
const check1 = schema.allOf.map((schema) => CreateExpression(schema, references, value)).join(' && ');
|
|
279
280
|
if (schema.unevaluatedProperties === false) {
|
|
280
|
-
const keyCheck = CreateVariable(`${new RegExp((0,
|
|
281
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_9.KeyOfPattern)(schema))};`);
|
|
281
282
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`;
|
|
282
283
|
yield `(${check1} && ${check2})`;
|
|
283
284
|
}
|
|
284
285
|
else if ((0, type_1.IsSchema)(schema.unevaluatedProperties)) {
|
|
285
|
-
const keyCheck = CreateVariable(`${new RegExp((0,
|
|
286
|
+
const keyCheck = CreateVariable(`${new RegExp((0, index_9.KeyOfPattern)(schema))};`);
|
|
286
287
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
287
288
|
yield `(${check1} && ${check2})`;
|
|
288
289
|
}
|
|
@@ -313,22 +314,22 @@ var TypeCompiler;
|
|
|
313
314
|
}
|
|
314
315
|
function* FromNumber(schema, references, value) {
|
|
315
316
|
yield Policy.IsNumberLike(value);
|
|
316
|
-
if ((0,
|
|
317
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMaximum))
|
|
317
318
|
yield `${value} < ${schema.exclusiveMaximum}`;
|
|
318
|
-
if ((0,
|
|
319
|
+
if ((0, index_11.IsNumber)(schema.exclusiveMinimum))
|
|
319
320
|
yield `${value} > ${schema.exclusiveMinimum}`;
|
|
320
|
-
if ((0,
|
|
321
|
+
if ((0, index_11.IsNumber)(schema.maximum))
|
|
321
322
|
yield `${value} <= ${schema.maximum}`;
|
|
322
|
-
if ((0,
|
|
323
|
+
if ((0, index_11.IsNumber)(schema.minimum))
|
|
323
324
|
yield `${value} >= ${schema.minimum}`;
|
|
324
|
-
if ((0,
|
|
325
|
+
if ((0, index_11.IsNumber)(schema.multipleOf))
|
|
325
326
|
yield `(${value} % ${schema.multipleOf}) === 0`;
|
|
326
327
|
}
|
|
327
328
|
function* FromObject(schema, references, value) {
|
|
328
329
|
yield Policy.IsObjectLike(value);
|
|
329
|
-
if ((0,
|
|
330
|
+
if ((0, index_11.IsNumber)(schema.minProperties))
|
|
330
331
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
331
|
-
if ((0,
|
|
332
|
+
if ((0, index_11.IsNumber)(schema.maxProperties))
|
|
332
333
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
333
334
|
const knownKeys = Object.getOwnPropertyNames(schema.properties);
|
|
334
335
|
for (const knownKey of knownKeys) {
|
|
@@ -364,9 +365,9 @@ var TypeCompiler;
|
|
|
364
365
|
}
|
|
365
366
|
function* FromRecord(schema, references, value) {
|
|
366
367
|
yield Policy.IsRecordLike(value);
|
|
367
|
-
if ((0,
|
|
368
|
+
if ((0, index_11.IsNumber)(schema.minProperties))
|
|
368
369
|
yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
|
|
369
|
-
if ((0,
|
|
370
|
+
if ((0, index_11.IsNumber)(schema.maxProperties))
|
|
370
371
|
yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
|
|
371
372
|
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
372
373
|
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
@@ -376,7 +377,7 @@ var TypeCompiler;
|
|
|
376
377
|
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
|
377
378
|
}
|
|
378
379
|
function* FromRef(schema, references, value) {
|
|
379
|
-
const target = (0,
|
|
380
|
+
const target = (0, index_5.Deref)(schema, references);
|
|
380
381
|
// Reference: If we have seen this reference before we can just yield and return the function call.
|
|
381
382
|
// If this isn't the case we defer to visit to generate and set the function for subsequent passes.
|
|
382
383
|
if (state.functions.has(schema.$ref))
|
|
@@ -385,9 +386,9 @@ var TypeCompiler;
|
|
|
385
386
|
}
|
|
386
387
|
function* FromString(schema, references, value) {
|
|
387
388
|
yield `(typeof ${value} === 'string')`;
|
|
388
|
-
if ((0,
|
|
389
|
+
if ((0, index_11.IsNumber)(schema.maxLength))
|
|
389
390
|
yield `${value}.length <= ${schema.maxLength}`;
|
|
390
|
-
if ((0,
|
|
391
|
+
if ((0, index_11.IsNumber)(schema.minLength))
|
|
391
392
|
yield `${value}.length >= ${schema.minLength}`;
|
|
392
393
|
if (schema.pattern !== undefined) {
|
|
393
394
|
const variable = CreateVariable(`${new RegExp(schema.pattern)};`);
|
|
@@ -428,9 +429,9 @@ var TypeCompiler;
|
|
|
428
429
|
}
|
|
429
430
|
function* FromUint8Array(schema, references, value) {
|
|
430
431
|
yield `${value} instanceof Uint8Array`;
|
|
431
|
-
if ((0,
|
|
432
|
+
if ((0, index_11.IsNumber)(schema.maxByteLength))
|
|
432
433
|
yield `(${value}.length <= ${schema.maxByteLength})`;
|
|
433
|
-
if ((0,
|
|
434
|
+
if ((0, index_11.IsNumber)(schema.minByteLength))
|
|
434
435
|
yield `(${value}.length >= ${schema.minByteLength})`;
|
|
435
436
|
}
|
|
436
437
|
function* FromUnknown(schema, references, value) {
|
|
@@ -442,15 +443,15 @@ var TypeCompiler;
|
|
|
442
443
|
function* FromKind(schema, references, value) {
|
|
443
444
|
const instance = state.instances.size;
|
|
444
445
|
state.instances.set(instance, schema);
|
|
445
|
-
yield `kind('${schema[
|
|
446
|
+
yield `kind('${schema[index_7.Kind]}', ${instance}, ${value})`;
|
|
446
447
|
}
|
|
447
448
|
function* Visit(schema, references, value, useHoisting = true) {
|
|
448
|
-
const references_ = (0,
|
|
449
|
+
const references_ = (0, index_11.IsString)(schema.$id) ? [...references, schema] : references;
|
|
449
450
|
const schema_ = schema;
|
|
450
451
|
// --------------------------------------------------------------
|
|
451
452
|
// Hoisting
|
|
452
453
|
// --------------------------------------------------------------
|
|
453
|
-
if (useHoisting && (0,
|
|
454
|
+
if (useHoisting && (0, index_11.IsString)(schema.$id)) {
|
|
454
455
|
const functionName = CreateFunctionName(schema.$id);
|
|
455
456
|
if (state.functions.has(functionName)) {
|
|
456
457
|
return yield `${functionName}(${value})`;
|
|
@@ -461,7 +462,7 @@ var TypeCompiler;
|
|
|
461
462
|
return yield `${functionName}(${value})`;
|
|
462
463
|
}
|
|
463
464
|
}
|
|
464
|
-
switch (schema_[
|
|
465
|
+
switch (schema_[index_7.Kind]) {
|
|
465
466
|
case 'Any':
|
|
466
467
|
return yield* FromAny(schema_, references_, value);
|
|
467
468
|
case 'Array':
|
|
@@ -523,7 +524,7 @@ var TypeCompiler;
|
|
|
523
524
|
case 'Void':
|
|
524
525
|
return yield* FromVoid(schema_, references_, value);
|
|
525
526
|
default:
|
|
526
|
-
if (!
|
|
527
|
+
if (!index_8.TypeRegistry.Has(schema_[index_7.Kind]))
|
|
527
528
|
throw new TypeCompilerUnknownTypeError(schema);
|
|
528
529
|
return yield* FromKind(schema_, references_, value);
|
|
529
530
|
}
|
|
@@ -576,7 +577,7 @@ var TypeCompiler;
|
|
|
576
577
|
const functions = [...state.functions.values()];
|
|
577
578
|
const variables = [...state.variables.values()];
|
|
578
579
|
// prettier-ignore
|
|
579
|
-
const checkFunction = (0,
|
|
580
|
+
const checkFunction = (0, index_11.IsString)(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
580
581
|
? `return function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
581
582
|
: `return ${functionCode}`;
|
|
582
583
|
return [...variables, ...functions, checkFunction].join('\n');
|
|
@@ -585,8 +586,8 @@ var TypeCompiler;
|
|
|
585
586
|
function Code(...args) {
|
|
586
587
|
const defaults = { language: 'javascript' };
|
|
587
588
|
// prettier-ignore
|
|
588
|
-
const [schema, references, options] = (args.length === 2 && (0,
|
|
589
|
-
args.length === 2 && !(0,
|
|
589
|
+
const [schema, references, options] = (args.length === 2 && (0, index_11.IsArray)(args[1]) ? [args[0], args[1], defaults] :
|
|
590
|
+
args.length === 2 && !(0, index_11.IsArray)(args[1]) ? [args[0], [], args[1]] :
|
|
590
591
|
args.length === 3 ? [args[0], args[1], args[2]] :
|
|
591
592
|
args.length === 1 ? [args[0], [], defaults] :
|
|
592
593
|
[null, [], defaults]);
|
|
@@ -609,20 +610,20 @@ var TypeCompiler;
|
|
|
609
610
|
const compiledFunction = globalThis.Function('kind', 'format', 'hash', generatedCode);
|
|
610
611
|
const instances = new Map(state.instances);
|
|
611
612
|
function typeRegistryFunction(kind, instance, value) {
|
|
612
|
-
if (!
|
|
613
|
+
if (!index_8.TypeRegistry.Has(kind) || !instances.has(instance))
|
|
613
614
|
return false;
|
|
614
|
-
const checkFunc =
|
|
615
|
+
const checkFunc = index_8.TypeRegistry.Get(kind);
|
|
615
616
|
const schema = instances.get(instance);
|
|
616
617
|
return checkFunc(schema, value);
|
|
617
618
|
}
|
|
618
619
|
function formatRegistryFunction(format, value) {
|
|
619
|
-
if (!
|
|
620
|
+
if (!index_8.FormatRegistry.Has(format))
|
|
620
621
|
return false;
|
|
621
|
-
const checkFunc =
|
|
622
|
+
const checkFunc = index_8.FormatRegistry.Get(format);
|
|
622
623
|
return checkFunc(value);
|
|
623
624
|
}
|
|
624
625
|
function hashFunction(value) {
|
|
625
|
-
return (0,
|
|
626
|
+
return (0, index_6.Hash)(value);
|
|
626
627
|
}
|
|
627
628
|
const checkFunction = compiledFunction(typeRegistryFunction, formatRegistryFunction, hashFunction);
|
|
628
629
|
return new TypeCheck(schema, references, checkFunction, generatedCode);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TypeBoxError } from '../type/error/index';
|
|
1
2
|
import type { TSchema } from '../type/schema/index';
|
|
2
3
|
export declare enum ValueErrorType {
|
|
3
4
|
ArrayContains = 0,
|
|
@@ -71,7 +72,7 @@ export interface ValueError {
|
|
|
71
72
|
value: unknown;
|
|
72
73
|
message: string;
|
|
73
74
|
}
|
|
74
|
-
export declare class ValueErrorsUnknownTypeError extends
|
|
75
|
+
export declare class ValueErrorsUnknownTypeError extends TypeBoxError {
|
|
75
76
|
readonly schema: TSchema;
|
|
76
77
|
constructor(schema: TSchema);
|
|
77
78
|
}
|