typebox 1.1.4 → 1.1.6
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.
|
@@ -15,13 +15,13 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
15
15
|
IsAccelerated(): boolean;
|
|
16
16
|
/** Returns the Context for this validator. */
|
|
17
17
|
Context(): Context;
|
|
18
|
-
/** Returns the Type
|
|
18
|
+
/** Returns the underlying Type used to construct this Validator. */
|
|
19
19
|
Type(): Type;
|
|
20
20
|
/** Returns the generated code for this validator. */
|
|
21
21
|
Code(): string;
|
|
22
|
-
/**
|
|
22
|
+
/** Performs a type-guard check on the provided value. */
|
|
23
23
|
Check(value: unknown): value is Encode;
|
|
24
|
-
/**
|
|
24
|
+
/** Inspects a value and returns a detailed list of validation errors. */
|
|
25
25
|
Errors(value: unknown): TLocalizedValidationError[];
|
|
26
26
|
/** Cleans a value using the Validator type. */
|
|
27
27
|
Clean(value: unknown): unknown;
|
|
@@ -33,7 +33,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
33
33
|
Default(value: unknown): unknown;
|
|
34
34
|
/** Clones this validator. */
|
|
35
35
|
Clone(): Validator<Context, Type>;
|
|
36
|
-
/**
|
|
36
|
+
/** Validates a value and returns it. Will throw if invalid. */
|
|
37
37
|
Parse(value: unknown): Encode;
|
|
38
38
|
/** Decodes a value */
|
|
39
39
|
Decode(value: unknown): Decode;
|
|
@@ -50,7 +50,7 @@ export class Validator extends Base {
|
|
|
50
50
|
Context() {
|
|
51
51
|
return this.context;
|
|
52
52
|
}
|
|
53
|
-
/** Returns the Type
|
|
53
|
+
/** Returns the underlying Type used to construct this Validator. */
|
|
54
54
|
Type() {
|
|
55
55
|
return this.type;
|
|
56
56
|
}
|
|
@@ -64,11 +64,11 @@ export class Validator extends Base {
|
|
|
64
64
|
// ----------------------------------------------------------------
|
|
65
65
|
// Base<...>
|
|
66
66
|
// ----------------------------------------------------------------
|
|
67
|
-
/**
|
|
67
|
+
/** Performs a type-guard check on the provided value. */
|
|
68
68
|
Check(value) {
|
|
69
69
|
return this.check(value);
|
|
70
70
|
}
|
|
71
|
-
/**
|
|
71
|
+
/** Inspects a value and returns a detailed list of validation errors. */
|
|
72
72
|
Errors(value) {
|
|
73
73
|
if (Environment.CanAccelerate() && this.check(value))
|
|
74
74
|
return [];
|
|
@@ -94,7 +94,7 @@ export class Validator extends Base {
|
|
|
94
94
|
Clone() {
|
|
95
95
|
return new Validator(this.context, this.type, this.isAccelerated, this.hasCodec, this.code, this.check);
|
|
96
96
|
}
|
|
97
|
-
/**
|
|
97
|
+
/** Validates a value and returns it. Will throw if invalid. */
|
|
98
98
|
Parse(value) {
|
|
99
99
|
const checked = this.Check(value);
|
|
100
100
|
if (checked)
|
|
@@ -7,11 +7,13 @@ export declare class Validator<Schema extends Schema.XSchema = Schema.XSchema, V
|
|
|
7
7
|
constructor(context: Record<string, Schema.XSchema>, schema: Schema);
|
|
8
8
|
/** Returns true if this Validator is using JIT acceleration. */
|
|
9
9
|
IsAccelerated(): boolean;
|
|
10
|
-
/**
|
|
10
|
+
/** Returns the underlying Schema used to construct this Validator. */
|
|
11
|
+
Schema(): Schema;
|
|
12
|
+
/** Performs a type-guard check on the provided value. */
|
|
11
13
|
Check(value: unknown): value is Value;
|
|
12
|
-
/**
|
|
14
|
+
/** Validates a value and returns it. Will throw if invalid. */
|
|
13
15
|
Parse(value: unknown): Value;
|
|
14
|
-
/**
|
|
16
|
+
/** Inspects a value and returns a detailed list of validation errors. */
|
|
15
17
|
Errors(value: unknown): [result: boolean, errors: TLocalizedValidationError[]];
|
|
16
18
|
}
|
|
17
19
|
/** Compiles this schema into a high performance Validator */
|
package/build/schema/compile.mjs
CHANGED
|
@@ -16,18 +16,22 @@ export class Validator {
|
|
|
16
16
|
IsAccelerated() {
|
|
17
17
|
return this.result.IsAccelerated;
|
|
18
18
|
}
|
|
19
|
-
/**
|
|
19
|
+
/** Returns the underlying Schema used to construct this Validator. */
|
|
20
|
+
Schema() {
|
|
21
|
+
return this.build.Schema();
|
|
22
|
+
}
|
|
23
|
+
/** Performs a type-guard check on the provided value. */
|
|
20
24
|
Check(value) {
|
|
21
25
|
return this.result.Check(value);
|
|
22
26
|
}
|
|
23
|
-
/**
|
|
27
|
+
/** Validates a value and returns it. Will throw if invalid. */
|
|
24
28
|
Parse(value) {
|
|
25
29
|
if (this.result.Check(value))
|
|
26
30
|
return value;
|
|
27
31
|
const [_result, errors] = Errors(this.build.Context(), this.build.Schema(), value);
|
|
28
32
|
throw new ParseError(this.build.Schema(), value, errors);
|
|
29
33
|
}
|
|
30
|
-
/**
|
|
34
|
+
/** Inspects a value and returns a detailed list of validation errors. */
|
|
31
35
|
Errors(value) {
|
|
32
36
|
return Errors(this.build.Context(), this.build.Schema(), value);
|
|
33
37
|
}
|
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
import { type TSchema } from './schema.mjs';
|
|
2
2
|
import { type XGuard, type XGuardInterface } from '../../schema/types/index.mjs';
|
|
3
3
|
export type StaticBase<Value extends unknown> = Value;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated Use Type.Refine() + Type.Unsafe() instead.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* **Reason:** It is noted that JavaScript class instances do not behave like
|
|
9
|
+
* plain objects during structural clone or when the TB compositor needs to
|
|
10
|
+
* assign dynamic modifier properties (such as '~optional').
|
|
11
|
+
*
|
|
12
|
+
* Because the TypeBox compositor needs to transform schematics via object clone /
|
|
13
|
+
* property spread, these operations can result in class instance types losing
|
|
14
|
+
* methods on the prototype (via clone), which can lead to unexpected structures being
|
|
15
|
+
* returned. This has led to special-case (non-clone) handling for Base which needs
|
|
16
|
+
* to be removed as it has proven orthogonal to the TypeBox 1.x design.
|
|
17
|
+
*
|
|
18
|
+
* The Base type was introduced in 1.x to try integrate / embed Standard Schema into JSON
|
|
19
|
+
* Schema; however, support for integrated Standard Schema embedding will not be continued
|
|
20
|
+
* in TypeBox. This type will be removed in the next minor revision of TypeBox.
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // (Deprecated)
|
|
24
|
+
* class DateType extends Type.Base<Date> { Check(value) { return value instanceof Date } }
|
|
25
|
+
*
|
|
26
|
+
* // (Future)
|
|
27
|
+
* const DateType = Type.Refine(Type.Unsafe<Date>({}), value => value instanceof Date)
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
5
30
|
export declare class Base<Value extends unknown = unknown> implements TSchema, XGuard<Value> {
|
|
6
31
|
readonly '~kind': 'Base';
|
|
7
32
|
readonly '~guard': XGuardInterface<Value>;
|
|
@@ -12,7 +12,32 @@ function BaseProperty(value) {
|
|
|
12
12
|
value
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Use Type.Refine() + Type.Unsafe() instead.
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* **Reason:** It is noted that JavaScript class instances do not behave like
|
|
20
|
+
* plain objects during structural clone or when the TB compositor needs to
|
|
21
|
+
* assign dynamic modifier properties (such as '~optional').
|
|
22
|
+
*
|
|
23
|
+
* Because the TypeBox compositor needs to transform schematics via object clone /
|
|
24
|
+
* property spread, these operations can result in class instance types losing
|
|
25
|
+
* methods on the prototype (via clone), which can lead to unexpected structures being
|
|
26
|
+
* returned. This has led to special-case (non-clone) handling for Base which needs
|
|
27
|
+
* to be removed as it has proven orthogonal to the TypeBox 1.x design.
|
|
28
|
+
*
|
|
29
|
+
* The Base type was introduced in 1.x to try integrate / embed Standard Schema into JSON
|
|
30
|
+
* Schema; however, support for integrated Standard Schema embedding will not be continued
|
|
31
|
+
* in TypeBox. This type will be removed in the next minor revision of TypeBox.
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // (Deprecated)
|
|
35
|
+
* class DateType extends Type.Base<Date> { Check(value) { return value instanceof Date } }
|
|
36
|
+
*
|
|
37
|
+
* // (Future)
|
|
38
|
+
* const DateType = Type.Refine(Type.Unsafe<Date>({}), value => value instanceof Date)
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
16
41
|
export class Base {
|
|
17
42
|
constructor() {
|
|
18
43
|
globalThis.Object.defineProperty(this, '~kind', BaseProperty('Base'));
|