typebox 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/compile/validator.d.mts +3 -3
- package/build/compile/validator.mjs +8 -8
- package/build/schema/build.d.mts +1 -1
- package/build/schema/build.mjs +2 -2
- package/build/schema/compile.d.mts +2 -0
- package/build/schema/compile.mjs +4 -0
- package/build/schema/engine/schema.mjs +6 -6
- package/build/system/environment/can-evaluate.d.mts +1 -1
- package/build/system/environment/can-evaluate.mjs +2 -2
- package/build/system/settings/settings.d.mts +1 -1
- package/build/system/settings/settings.mjs +2 -2
- package/package.json +30 -30
|
@@ -3,7 +3,7 @@ import { type StaticDecode, type StaticEncode, type TProperties, type TSchema, B
|
|
|
3
3
|
export declare class Validator<Context extends TProperties = TProperties, Type extends TSchema = TSchema, Encode extends unknown = StaticEncode<Type, Context>, Decode extends unknown = StaticDecode<Type, Context>> extends Base<Encode> {
|
|
4
4
|
private readonly context;
|
|
5
5
|
private readonly type;
|
|
6
|
-
private readonly
|
|
6
|
+
private readonly isAccelerated;
|
|
7
7
|
private readonly hasCodec;
|
|
8
8
|
private readonly code;
|
|
9
9
|
private readonly check;
|
|
@@ -11,8 +11,8 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
11
11
|
constructor(context: Context, type: Type);
|
|
12
12
|
/** Constructs a Validator with the given arguments. */
|
|
13
13
|
constructor(context: Context, type: Type, isEvaluated: boolean, hasCodec: boolean, code: string, check: (value: unknown) => boolean);
|
|
14
|
-
/** Returns true if this
|
|
15
|
-
|
|
14
|
+
/** Returns true if this Validator is using JIT acceleration. */
|
|
15
|
+
IsAccelerated(): boolean;
|
|
16
16
|
/** Returns the Context for this validator. */
|
|
17
17
|
Context(): Context;
|
|
18
18
|
/** Returns the Type for this validator. */
|
|
@@ -20,7 +20,7 @@ export class Validator extends Base {
|
|
|
20
20
|
const [context, type, isEvaluated, hasCodec, code, check] = matched;
|
|
21
21
|
this.context = context;
|
|
22
22
|
this.type = type;
|
|
23
|
-
this.
|
|
23
|
+
this.isAccelerated = isEvaluated;
|
|
24
24
|
this.hasCodec = hasCodec;
|
|
25
25
|
this.code = code;
|
|
26
26
|
this.check = check;
|
|
@@ -31,17 +31,17 @@ export class Validator extends Base {
|
|
|
31
31
|
this.hasCodec = HasCodec(context, type);
|
|
32
32
|
this.context = context;
|
|
33
33
|
this.type = type;
|
|
34
|
-
this.
|
|
34
|
+
this.isAccelerated = result.IsAccelerated;
|
|
35
35
|
this.code = result.Code;
|
|
36
36
|
this.check = result.Check;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
// ----------------------------------------------------------------
|
|
40
|
-
//
|
|
40
|
+
// IsAccelerated
|
|
41
41
|
// ----------------------------------------------------------------
|
|
42
|
-
/** Returns true if this
|
|
43
|
-
|
|
44
|
-
return this.
|
|
42
|
+
/** Returns true if this Validator is using JIT acceleration. */
|
|
43
|
+
IsAccelerated() {
|
|
44
|
+
return this.isAccelerated;
|
|
45
45
|
}
|
|
46
46
|
// ----------------------------------------------------------------
|
|
47
47
|
// Context | Type
|
|
@@ -70,7 +70,7 @@ export class Validator extends Base {
|
|
|
70
70
|
}
|
|
71
71
|
/** Returns errors for the given value. */
|
|
72
72
|
Errors(value) {
|
|
73
|
-
if (Environment.
|
|
73
|
+
if (Environment.CanAccelerate() && this.check(value))
|
|
74
74
|
return [];
|
|
75
75
|
return Errors(this.context, this.type, value);
|
|
76
76
|
}
|
|
@@ -92,7 +92,7 @@ export class Validator extends Base {
|
|
|
92
92
|
}
|
|
93
93
|
/** Clones this validator. */
|
|
94
94
|
Clone() {
|
|
95
|
-
return new Validator(this.context, this.type, this.
|
|
95
|
+
return new Validator(this.context, this.type, this.isAccelerated, this.hasCodec, this.code, this.check);
|
|
96
96
|
}
|
|
97
97
|
/** Parses a value */
|
|
98
98
|
Parse(value) {
|
package/build/schema/build.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import * as Engine from './engine/index.mjs';
|
|
|
2
2
|
import * as Schema from './types/index.mjs';
|
|
3
3
|
export type CheckFunction = (value: unknown) => boolean;
|
|
4
4
|
export interface EvaluateResult {
|
|
5
|
-
|
|
5
|
+
IsAccelerated: boolean;
|
|
6
6
|
Code: string;
|
|
7
7
|
Check: CheckFunction;
|
|
8
8
|
}
|
package/build/schema/build.mjs
CHANGED
|
@@ -35,7 +35,7 @@ function CreateDynamicCheck(build) {
|
|
|
35
35
|
// CreateCheck
|
|
36
36
|
// ------------------------------------------------------------------
|
|
37
37
|
function CreateCheck(build, code) {
|
|
38
|
-
return Environment.
|
|
38
|
+
return Environment.CanAccelerate()
|
|
39
39
|
? CreateEvaluatedCheck(build, code)
|
|
40
40
|
: CreateDynamicCheck(build);
|
|
41
41
|
}
|
|
@@ -79,7 +79,7 @@ export class BuildResult {
|
|
|
79
79
|
Evaluate() {
|
|
80
80
|
const Code = CreateCode(this);
|
|
81
81
|
const Check = CreateCheck(this, Code);
|
|
82
|
-
return {
|
|
82
|
+
return { IsAccelerated: Environment.CanAccelerate(), Code, Check };
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
/** Builds a schema into a optimized runtime validator */
|
|
@@ -5,6 +5,8 @@ export declare class Validator<Schema extends Schema.XSchema, Value extends unkn
|
|
|
5
5
|
private readonly build;
|
|
6
6
|
private readonly result;
|
|
7
7
|
constructor(context: Record<string, Schema.XSchema>, schema: Schema);
|
|
8
|
+
/** Returns true if this Validator is using JIT acceleration. */
|
|
9
|
+
IsAccelerated(): boolean;
|
|
8
10
|
/** Checks this value is valid */
|
|
9
11
|
Check(value: unknown): value is Value;
|
|
10
12
|
/** Parses this value and throw if invalid */
|
package/build/schema/compile.mjs
CHANGED
|
@@ -12,6 +12,10 @@ export class Validator {
|
|
|
12
12
|
this.build = Build.Build(context, schema);
|
|
13
13
|
this.result = this.build.Evaluate();
|
|
14
14
|
}
|
|
15
|
+
/** Returns true if this Validator is using JIT acceleration. */
|
|
16
|
+
IsAccelerated() {
|
|
17
|
+
return this.result.IsAccelerated;
|
|
18
|
+
}
|
|
15
19
|
/** Checks this value is valid */
|
|
16
20
|
Check(value) {
|
|
17
21
|
return this.result.Check(value);
|
|
@@ -176,12 +176,12 @@ export function BuildSchema(stack, context, schema, value) {
|
|
|
176
176
|
}
|
|
177
177
|
if (HasStringKeywords(schema)) {
|
|
178
178
|
const constraints = [];
|
|
179
|
-
if (Schema.IsFormat(schema))
|
|
180
|
-
constraints.push(BuildFormat(stack, context, schema, value));
|
|
181
179
|
if (Schema.IsMaxLength(schema))
|
|
182
180
|
constraints.push(BuildMaxLength(stack, context, schema, value));
|
|
183
181
|
if (Schema.IsMinLength(schema))
|
|
184
182
|
constraints.push(BuildMinLength(stack, context, schema, value));
|
|
183
|
+
if (Schema.IsFormat(schema))
|
|
184
|
+
constraints.push(BuildFormat(stack, context, schema, value));
|
|
185
185
|
if (Schema.IsPattern(schema))
|
|
186
186
|
constraints.push(BuildPattern(stack, context, schema, value));
|
|
187
187
|
const reduced = E.ReduceAnd(constraints);
|
|
@@ -259,9 +259,9 @@ export function CheckSchema(stack, context, schema, value) {
|
|
|
259
259
|
(!Schema.IsMinItems(schema) || CheckMinItems(stack, context, schema, value)) &&
|
|
260
260
|
(!Schema.IsPrefixItems(schema) || CheckPrefixItems(stack, context, schema, value)) &&
|
|
261
261
|
(!Schema.IsUniqueItems(schema) || CheckUniqueItems(stack, context, schema, value)))) &&
|
|
262
|
-
(!G.IsString(value) || ((!Schema.
|
|
263
|
-
(!Schema.IsMaxLength(schema) || CheckMaxLength(stack, context, schema, value)) &&
|
|
262
|
+
(!G.IsString(value) || ((!Schema.IsMaxLength(schema) || CheckMaxLength(stack, context, schema, value)) &&
|
|
264
263
|
(!Schema.IsMinLength(schema) || CheckMinLength(stack, context, schema, value)) &&
|
|
264
|
+
(!Schema.IsFormat(schema) || CheckFormat(stack, context, schema, value)) &&
|
|
265
265
|
(!Schema.IsPattern(schema) || CheckPattern(stack, context, schema, value)))) &&
|
|
266
266
|
(!(G.IsNumber(value) || G.IsBigInt(value)) || ((!Schema.IsExclusiveMaximum(schema) || CheckExclusiveMaximum(stack, context, schema, value)) &&
|
|
267
267
|
(!Schema.IsExclusiveMinimum(schema) || CheckExclusiveMinimum(stack, context, schema, value)) &&
|
|
@@ -309,9 +309,9 @@ export function ErrorSchema(stack, context, schemaPath, instancePath, schema, va
|
|
|
309
309
|
+(!Schema.IsMinItems(schema) || ErrorMinItems(stack, context, schemaPath, instancePath, schema, value)) &
|
|
310
310
|
+(!Schema.IsPrefixItems(schema) || ErrorPrefixItems(stack, context, schemaPath, instancePath, schema, value)) &
|
|
311
311
|
+(!Schema.IsUniqueItems(schema) || ErrorUniqueItems(stack, context, schemaPath, instancePath, schema, value)))) &
|
|
312
|
-
+(!G.IsString(value) || !!(+(!Schema.
|
|
313
|
-
+(!Schema.IsMaxLength(schema) || ErrorMaxLength(stack, context, schemaPath, instancePath, schema, value)) &
|
|
312
|
+
+(!G.IsString(value) || !!(+(!Schema.IsMaxLength(schema) || ErrorMaxLength(stack, context, schemaPath, instancePath, schema, value)) &
|
|
314
313
|
+(!Schema.IsMinLength(schema) || ErrorMinLength(stack, context, schemaPath, instancePath, schema, value)) &
|
|
314
|
+
+(!Schema.IsFormat(schema) || ErrorFormat(stack, context, schemaPath, instancePath, schema, value)) &
|
|
315
315
|
+(!Schema.IsPattern(schema) || ErrorPattern(stack, context, schemaPath, instancePath, schema, value)))) &
|
|
316
316
|
+(!(G.IsNumber(value) || G.IsBigInt(value)) || !!(+(!Schema.IsExclusiveMaximum(schema) || ErrorExclusiveMaximum(stack, context, schemaPath, instancePath, schema, value)) &
|
|
317
317
|
+(!Schema.IsExclusiveMinimum(schema) || ErrorExclusiveMinimum(stack, context, schemaPath, instancePath, schema, value)) &
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Returns true if the environment supports dynamic JavaScript evaluation */
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function CanAccelerate(): boolean;
|
|
@@ -19,8 +19,8 @@ function TryEval() {
|
|
|
19
19
|
// CanEvaluate
|
|
20
20
|
// ------------------------------------------------------------------
|
|
21
21
|
/** Returns true if the environment supports dynamic JavaScript evaluation */
|
|
22
|
-
export function
|
|
22
|
+
export function CanAccelerate() {
|
|
23
23
|
if (Guard.IsUndefined(supported))
|
|
24
24
|
supported = TryEval();
|
|
25
|
-
return supported && Settings.Get().
|
|
25
|
+
return supported && Settings.Get().useAcceleration;
|
|
26
26
|
}
|
|
@@ -22,7 +22,7 @@ export interface TSettings {
|
|
|
22
22
|
* restrict runtime code evaluation, regardless of Content Security Policy (CSP).
|
|
23
23
|
* @default true
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
useAcceleration: boolean;
|
|
26
26
|
/**
|
|
27
27
|
* Enables or disables 'exactOptionalPropertyTypes' check semantics. By default, TypeScript
|
|
28
28
|
* allows optional properties to be assigned 'undefined'. While this behavior differs from the
|
|
@@ -3,7 +3,7 @@ import { Guard } from '../../guard/index.mjs';
|
|
|
3
3
|
const settings = {
|
|
4
4
|
immutableTypes: false,
|
|
5
5
|
maxErrors: 8,
|
|
6
|
-
|
|
6
|
+
useAcceleration: true,
|
|
7
7
|
exactOptionalPropertyTypes: false,
|
|
8
8
|
enumerableKind: false,
|
|
9
9
|
correctiveParse: false
|
|
@@ -12,7 +12,7 @@ const settings = {
|
|
|
12
12
|
export function Reset() {
|
|
13
13
|
settings.immutableTypes = false;
|
|
14
14
|
settings.maxErrors = 8;
|
|
15
|
-
settings.
|
|
15
|
+
settings.useAcceleration = true;
|
|
16
16
|
settings.exactOptionalPropertyTypes = false;
|
|
17
17
|
settings.enumerableKind = false;
|
|
18
18
|
settings.correctiveParse = false;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typebox",
|
|
3
3
|
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"jsonschema"
|
|
@@ -16,37 +16,37 @@
|
|
|
16
16
|
"types": "./build/index.d.mts",
|
|
17
17
|
"module": "./build/index.mjs",
|
|
18
18
|
"exports": {
|
|
19
|
-
"./
|
|
20
|
-
"import": "./build/
|
|
21
|
-
"default": "./build/
|
|
22
|
-
},
|
|
23
|
-
"./schema": {
|
|
24
|
-
"import": "./build/schema/index.mjs",
|
|
25
|
-
"default": "./build/schema/index.mjs"
|
|
19
|
+
"./type": {
|
|
20
|
+
"import": "./build/type/index.mjs",
|
|
21
|
+
"default": "./build/type/index.mjs"
|
|
26
22
|
},
|
|
27
23
|
"./value": {
|
|
28
24
|
"import": "./build/value/index.mjs",
|
|
29
25
|
"default": "./build/value/index.mjs"
|
|
30
26
|
},
|
|
27
|
+
"./guard": {
|
|
28
|
+
"import": "./build/guard/index.mjs",
|
|
29
|
+
"default": "./build/guard/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./system": {
|
|
32
|
+
"import": "./build/system/index.mjs",
|
|
33
|
+
"default": "./build/system/index.mjs"
|
|
34
|
+
},
|
|
31
35
|
"./format": {
|
|
32
36
|
"import": "./build/format/index.mjs",
|
|
33
37
|
"default": "./build/format/index.mjs"
|
|
34
38
|
},
|
|
35
|
-
"./
|
|
36
|
-
"import": "./build/
|
|
37
|
-
"default": "./build/
|
|
39
|
+
"./compile": {
|
|
40
|
+
"import": "./build/compile/index.mjs",
|
|
41
|
+
"default": "./build/compile/index.mjs"
|
|
38
42
|
},
|
|
39
43
|
"./error": {
|
|
40
44
|
"import": "./build/error/index.mjs",
|
|
41
45
|
"default": "./build/error/index.mjs"
|
|
42
46
|
},
|
|
43
|
-
"./
|
|
44
|
-
"import": "./build/
|
|
45
|
-
"default": "./build/
|
|
46
|
-
},
|
|
47
|
-
"./compile": {
|
|
48
|
-
"import": "./build/compile/index.mjs",
|
|
49
|
-
"default": "./build/compile/index.mjs"
|
|
47
|
+
"./schema": {
|
|
48
|
+
"import": "./build/schema/index.mjs",
|
|
49
|
+
"default": "./build/schema/index.mjs"
|
|
50
50
|
},
|
|
51
51
|
".": {
|
|
52
52
|
"import": "./build/index.mjs",
|
|
@@ -55,29 +55,29 @@
|
|
|
55
55
|
},
|
|
56
56
|
"typesVersions": {
|
|
57
57
|
"*": {
|
|
58
|
-
"
|
|
59
|
-
"./build/
|
|
60
|
-
],
|
|
61
|
-
"schema": [
|
|
62
|
-
"./build/schema/index.d.mts"
|
|
58
|
+
"type": [
|
|
59
|
+
"./build/type/index.d.mts"
|
|
63
60
|
],
|
|
64
61
|
"value": [
|
|
65
62
|
"./build/value/index.d.mts"
|
|
66
63
|
],
|
|
64
|
+
"guard": [
|
|
65
|
+
"./build/guard/index.d.mts"
|
|
66
|
+
],
|
|
67
|
+
"system": [
|
|
68
|
+
"./build/system/index.d.mts"
|
|
69
|
+
],
|
|
67
70
|
"format": [
|
|
68
71
|
"./build/format/index.d.mts"
|
|
69
72
|
],
|
|
70
|
-
"
|
|
71
|
-
"./build/
|
|
73
|
+
"compile": [
|
|
74
|
+
"./build/compile/index.d.mts"
|
|
72
75
|
],
|
|
73
76
|
"error": [
|
|
74
77
|
"./build/error/index.d.mts"
|
|
75
78
|
],
|
|
76
|
-
"
|
|
77
|
-
"./build/
|
|
78
|
-
],
|
|
79
|
-
"compile": [
|
|
80
|
-
"./build/compile/index.d.mts"
|
|
79
|
+
"schema": [
|
|
80
|
+
"./build/schema/index.d.mts"
|
|
81
81
|
],
|
|
82
82
|
".": [
|
|
83
83
|
"./build/index.d.mts"
|