typebox 1.0.81 → 1.1.0
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.
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Settings } from '../system/settings/index.mjs';
|
|
2
3
|
import { Arguments } from '../system/arguments/index.mjs';
|
|
3
4
|
import { Environment } from '../system/environment/index.mjs';
|
|
4
5
|
import { Base } from '../type/index.mjs';
|
|
5
|
-
import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser } from '../value/index.mjs';
|
|
6
|
+
import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser, ParseError } from '../value/index.mjs';
|
|
6
7
|
import { Build } from '../schema/index.mjs';
|
|
7
8
|
// ------------------------------------------------------------------
|
|
8
9
|
// Validator<...>
|
|
@@ -93,13 +94,14 @@ export class Validator extends Base {
|
|
|
93
94
|
Clone() {
|
|
94
95
|
return new Validator(this.context, this.type, this.isEvaluated, this.hasCodec, this.code, this.check);
|
|
95
96
|
}
|
|
96
|
-
// ----------------------------------------------------------------
|
|
97
|
-
// Parse | Decode | Encode
|
|
98
|
-
// ----------------------------------------------------------------
|
|
99
97
|
/** Parses a value */
|
|
100
98
|
Parse(value) {
|
|
101
|
-
const
|
|
102
|
-
|
|
99
|
+
const checked = this.Check(value);
|
|
100
|
+
if (checked)
|
|
101
|
+
return value;
|
|
102
|
+
if (Settings.Get().correctiveParse)
|
|
103
|
+
return Parser(this.context, this.type, value);
|
|
104
|
+
throw new ParseError(value, this.Errors(value));
|
|
103
105
|
}
|
|
104
106
|
/** Decodes a value */
|
|
105
107
|
Decode(value) {
|
|
@@ -38,6 +38,15 @@ export interface TSettings {
|
|
|
38
38
|
* @default false
|
|
39
39
|
*/
|
|
40
40
|
enumerableKind: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Controls whether TypeBox uses corrective Parse. When enabled, TypeBox will attempt to recover invalid
|
|
43
|
+
* values during Parse by running a sequence of `Value.*` operations to `Convert`, `Default`, and `Clean`
|
|
44
|
+
* the value, followed by a subsequent `Assert`. Enabling this option may incur significant performance
|
|
45
|
+
* overhead for invalid values. It is recommended to keep this disabled in performance-sensitive
|
|
46
|
+
* applications.
|
|
47
|
+
* @default false
|
|
48
|
+
*/
|
|
49
|
+
correctiveParse: boolean;
|
|
41
50
|
}
|
|
42
51
|
/** Resets system settings to defaults */
|
|
43
52
|
export declare function Reset(): void;
|
|
@@ -5,7 +5,8 @@ const settings = {
|
|
|
5
5
|
maxErrors: 8,
|
|
6
6
|
useEval: true,
|
|
7
7
|
exactOptionalPropertyTypes: false,
|
|
8
|
-
enumerableKind: false
|
|
8
|
+
enumerableKind: false,
|
|
9
|
+
correctiveParse: false
|
|
9
10
|
};
|
|
10
11
|
/** Resets system settings to defaults */
|
|
11
12
|
export function Reset() {
|
|
@@ -14,6 +15,7 @@ export function Reset() {
|
|
|
14
15
|
settings.useEval = true;
|
|
15
16
|
settings.exactOptionalPropertyTypes = false;
|
|
16
17
|
settings.enumerableKind = false;
|
|
18
|
+
settings.correctiveParse = false;
|
|
17
19
|
}
|
|
18
20
|
/** Sets system settings */
|
|
19
21
|
export function Set(options) {
|
|
@@ -5,17 +5,7 @@ export declare class ParseError extends AssertError {
|
|
|
5
5
|
constructor(value: unknown, errors: TLocalizedValidationError[]);
|
|
6
6
|
}
|
|
7
7
|
export declare const Parser: import("../pipeline/pipeline.mjs").PipelineInterface;
|
|
8
|
-
/**
|
|
9
|
-
* Parses a value with the given type. The function will Check the value and return
|
|
10
|
-
* early if it matches the provided type. If the value does not match, it is processed
|
|
11
|
-
* through a sequence of Clone, Default, Convert, and Clean operations and checked again.
|
|
12
|
-
* A `ParseError` is thrown if the value fails to match after the processing sequence.
|
|
13
|
-
*/
|
|
8
|
+
/** Parses a value with the given type. */
|
|
14
9
|
export declare function Parse<const Type extends TSchema, Result extends unknown = StaticParse<Type>>(type: Type, value: unknown): Result;
|
|
15
|
-
/**
|
|
16
|
-
* Parses a value with the given type. The function will Check the value and return
|
|
17
|
-
* early if it matches the provided type. If the value does not match, it is processed
|
|
18
|
-
* through a sequence of Clone, Default, Convert, and Clean operations and checked again.
|
|
19
|
-
* A `ParseError` is thrown if the value fails to match after the processing sequence.
|
|
20
|
-
*/
|
|
10
|
+
/** Parses a value with the given type. */
|
|
21
11
|
export declare function Parse<Context extends TProperties, const Type extends TSchema, Result extends unknown = StaticParse<Type, Context>>(context: Context, type: Type, value: unknown): Result;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Settings } from '../../system/system.mjs';
|
|
2
3
|
import { Arguments } from '../../system/arguments/index.mjs';
|
|
3
4
|
import { AssertError } from '../assert/index.mjs';
|
|
4
5
|
import { Check } from '../check/index.mjs';
|
|
@@ -31,17 +32,16 @@ export const Parser = Pipeline([
|
|
|
31
32
|
(context, type, value) => Clean(context, type, value),
|
|
32
33
|
(context, type, value) => Assert(context, type, value)
|
|
33
34
|
]);
|
|
34
|
-
/**
|
|
35
|
-
* Parses a value with the given type. The function will Check the value and return
|
|
36
|
-
* early if it matches the provided type. If the value does not match, it is processed
|
|
37
|
-
* through a sequence of Clone, Default, Convert, and Clean operations and checked again.
|
|
38
|
-
* A `ParseError` is thrown if the value fails to match after the processing sequence.
|
|
39
|
-
*/
|
|
35
|
+
/** Parses a value with the given type. */
|
|
40
36
|
export function Parse(...args) {
|
|
41
37
|
const [context, type, value] = Arguments.Match(args, {
|
|
42
38
|
3: (context, type, value) => [context, type, value],
|
|
43
39
|
2: (type, value) => [{}, type, value],
|
|
44
40
|
});
|
|
45
|
-
const
|
|
46
|
-
|
|
41
|
+
const checked = Check(context, type, value);
|
|
42
|
+
if (checked)
|
|
43
|
+
return value;
|
|
44
|
+
if (Settings.Get().correctiveParse)
|
|
45
|
+
return Parser(context, type, value);
|
|
46
|
+
throw new ParseError(value, Errors(context, type, value));
|
|
47
47
|
}
|