typebox 1.0.5 → 1.0.7
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 +10 -3
- package/build/compile/validator.mjs +16 -7
- package/build/guard/guard.d.mts +2 -0
- package/build/guard/guard.mjs +14 -0
- package/build/value/check/check.d.mts +2 -2
- package/build/value/check/check.mjs +1 -1
- package/build/value/clone/clone.mjs +23 -2
- package/build/value/codec/has.mjs +8 -0
- package/package.json +1 -1
- package/readme.md +6 -3
|
@@ -4,6 +4,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
4
4
|
private readonly context;
|
|
5
5
|
private readonly type;
|
|
6
6
|
private readonly isEvaluated;
|
|
7
|
+
private readonly hasCodec;
|
|
7
8
|
private readonly code;
|
|
8
9
|
private readonly check;
|
|
9
10
|
constructor(context: Context, type: Type);
|
|
@@ -15,16 +16,22 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
15
16
|
Type(): Type;
|
|
16
17
|
/** Returns the generated code for this validator */
|
|
17
18
|
Code(): string;
|
|
19
|
+
/** Checks a value matches the Validator type. */
|
|
18
20
|
Check(value: unknown): value is StaticEncode<Type, Context>;
|
|
21
|
+
/** Returns errors for the given value. */
|
|
19
22
|
Errors(value: unknown): TLocalizedValidationError[];
|
|
23
|
+
/** Cleans a value using the Validator type. */
|
|
20
24
|
Clean(value: unknown): unknown;
|
|
25
|
+
/** Converts a value using the Validator type. */
|
|
21
26
|
Convert(value: unknown): unknown;
|
|
27
|
+
/** Creates a value using the Validator type. */
|
|
22
28
|
Create(): StaticEncode<Type, Context>;
|
|
29
|
+
/** Creates defaults using the Validator type. */
|
|
23
30
|
Default(value: unknown): unknown;
|
|
24
|
-
/** Parses a value
|
|
31
|
+
/** Parses a value */
|
|
25
32
|
Parse(value: unknown): StaticDecode<Type, Context>;
|
|
26
|
-
/** Decodes a value
|
|
33
|
+
/** Decodes a value */
|
|
27
34
|
Decode(value: unknown): StaticDecode<Type, Context>;
|
|
28
|
-
/** Encodes a value
|
|
35
|
+
/** Encodes a value */
|
|
29
36
|
Encode(value: unknown): StaticEncode<Type, Context>;
|
|
30
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Environment } from '../system/environment/index.mjs';
|
|
3
3
|
import { Base } from '../type/index.mjs';
|
|
4
|
-
import { Errors, Clean, Convert, Create, Default,
|
|
4
|
+
import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser } from '../value/index.mjs';
|
|
5
5
|
import { Build } from '../schema/index.mjs';
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// ValidatorType<...>
|
|
@@ -12,6 +12,7 @@ export class Validator extends Base {
|
|
|
12
12
|
this.context = context;
|
|
13
13
|
this.type = type;
|
|
14
14
|
const result = Build(context, type).Evaluate();
|
|
15
|
+
this.hasCodec = HasCodec(context, type);
|
|
15
16
|
this.isEvaluated = result.IsEvaluated;
|
|
16
17
|
this.code = result.Code;
|
|
17
18
|
this.check = result.Check;
|
|
@@ -44,40 +45,48 @@ export class Validator extends Base {
|
|
|
44
45
|
// ----------------------------------------------------------------
|
|
45
46
|
// Base<...>
|
|
46
47
|
// ----------------------------------------------------------------
|
|
48
|
+
/** Checks a value matches the Validator type. */
|
|
47
49
|
Check(value) {
|
|
48
50
|
return this.check(value);
|
|
49
51
|
}
|
|
52
|
+
/** Returns errors for the given value. */
|
|
50
53
|
Errors(value) {
|
|
51
54
|
if (Environment.CanEvaluate() && this.check(value))
|
|
52
55
|
return [];
|
|
53
56
|
return Errors(this.context, this.type, value);
|
|
54
57
|
}
|
|
58
|
+
/** Cleans a value using the Validator type. */
|
|
55
59
|
Clean(value) {
|
|
56
60
|
return Clean(this.context, this.type, value);
|
|
57
61
|
}
|
|
62
|
+
/** Converts a value using the Validator type. */
|
|
58
63
|
Convert(value) {
|
|
59
64
|
return Convert(this.context, this.type, value);
|
|
60
65
|
}
|
|
66
|
+
/** Creates a value using the Validator type. */
|
|
61
67
|
Create() {
|
|
62
68
|
return Create(this.context, this.type);
|
|
63
69
|
}
|
|
70
|
+
/** Creates defaults using the Validator type. */
|
|
64
71
|
Default(value) {
|
|
65
72
|
return Default(this.context, this.type, value);
|
|
66
73
|
}
|
|
67
74
|
// ----------------------------------------------------------------
|
|
68
|
-
// Parse
|
|
75
|
+
// Parse | Decode | Encode
|
|
69
76
|
// ----------------------------------------------------------------
|
|
70
|
-
/** Parses a value
|
|
77
|
+
/** Parses a value */
|
|
71
78
|
Parse(value) {
|
|
72
79
|
const result = this.Check(value) ? value : Parser(this.context, this.type, value);
|
|
73
80
|
return result;
|
|
74
81
|
}
|
|
75
|
-
/** Decodes a value
|
|
82
|
+
/** Decodes a value */
|
|
76
83
|
Decode(value) {
|
|
77
|
-
|
|
84
|
+
const result = this.hasCodec ? Decode(this.context, this.type, value) : this.Parse(value);
|
|
85
|
+
return result;
|
|
78
86
|
}
|
|
79
|
-
/** Encodes a value
|
|
87
|
+
/** Encodes a value */
|
|
80
88
|
Encode(value) {
|
|
81
|
-
|
|
89
|
+
const result = this.hasCodec ? Encode(this.context, this.type, value) : this.Parse(value);
|
|
90
|
+
return result;
|
|
82
91
|
}
|
|
83
92
|
}
|
package/build/guard/guard.d.mts
CHANGED
|
@@ -34,6 +34,8 @@ export declare function IsLessThan<Type extends number | bigint>(left: Type, rig
|
|
|
34
34
|
export declare function IsLessEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
|
|
35
35
|
export declare function IsGreaterEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
|
|
36
36
|
export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint | number): boolean;
|
|
37
|
+
/** Returns true if the value appears to be an instance of a class. */
|
|
38
|
+
export declare function IsClassInstance(value: unknown): boolean;
|
|
37
39
|
export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
|
|
38
40
|
/** Returns the number of Unicode Grapheme Clusters */
|
|
39
41
|
export declare function StringGraphemeCount(value: string): number;
|
package/build/guard/guard.mjs
CHANGED
|
@@ -102,6 +102,20 @@ export function IsMultipleOf(dividend, divisor) {
|
|
|
102
102
|
return Math.min(Math.abs(mod), Math.abs(mod - divisor)) < tolerance;
|
|
103
103
|
}
|
|
104
104
|
// ------------------------------------------------------------------
|
|
105
|
+
// IsClassInstance
|
|
106
|
+
// ------------------------------------------------------------------
|
|
107
|
+
/** Returns true if the value appears to be an instance of a class. */
|
|
108
|
+
export function IsClassInstance(value) {
|
|
109
|
+
if (!IsObject(value))
|
|
110
|
+
return false;
|
|
111
|
+
const proto = globalThis.Object.getPrototypeOf(value);
|
|
112
|
+
if (IsNull(proto))
|
|
113
|
+
return false;
|
|
114
|
+
return IsEqual(typeof proto.constructor, 'function') &&
|
|
115
|
+
!(IsEqual(proto.constructor, globalThis.Object) ||
|
|
116
|
+
IsEqual(proto.constructor.name, 'Object'));
|
|
117
|
+
}
|
|
118
|
+
// ------------------------------------------------------------------
|
|
105
119
|
// IsValueLike
|
|
106
120
|
// ------------------------------------------------------------------
|
|
107
121
|
export function IsValueLike(value) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Static, TProperties, TSchema } from '../../type/index.mjs';
|
|
2
|
-
/** Checks a value matches the provided type.
|
|
2
|
+
/** Checks a value matches the provided type. */
|
|
3
3
|
export declare function Check<const Type extends TSchema, Result extends unknown = Static<Type>>(type: Type, value: unknown): value is Result;
|
|
4
|
-
/** Checks a value matches the provided type.
|
|
4
|
+
/** Checks a value matches the provided type. */
|
|
5
5
|
export declare function Check<Context extends TProperties, const Type extends TSchema, Result extends unknown = Static<Type, Context>>(context: Context, type: Type, value: unknown): value is Result;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Arguments } from '../../system/arguments/index.mjs';
|
|
3
3
|
import { Check as SchemaCheck } from '../../schema/index.mjs';
|
|
4
|
-
/** Checks a value matches the provided type.
|
|
4
|
+
/** Checks a value matches the provided type. */
|
|
5
5
|
export function Check(...args) {
|
|
6
6
|
const [context, type, value] = Arguments.Match(args, {
|
|
7
7
|
3: (context, type, value) => [context, type, value],
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard, GlobalsGuard } from '../../guard/index.mjs';
|
|
3
3
|
// ------------------------------------------------------------------
|
|
4
|
-
//
|
|
4
|
+
// ClassInstance
|
|
5
|
+
//
|
|
6
|
+
// TypeBox does not support cloning arbitrary class instances. It treats
|
|
7
|
+
// class instances as atomic values, similar to number, boolean, and
|
|
8
|
+
// string. In the future, an implementation could detect the presence of
|
|
9
|
+
// a .clone() method, but no formal specification for this behavior
|
|
10
|
+
// exists, so we don't.
|
|
11
|
+
//
|
|
5
12
|
// ------------------------------------------------------------------
|
|
6
|
-
function
|
|
13
|
+
function FromClassInstance(value) {
|
|
14
|
+
return value; // atomic
|
|
15
|
+
}
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
// ObjectInstance
|
|
18
|
+
// ------------------------------------------------------------------
|
|
19
|
+
function FromObjectInstance(value) {
|
|
7
20
|
const result = {};
|
|
8
21
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
9
22
|
result[key] = Clone(value[key]);
|
|
@@ -14,6 +27,14 @@ function FromObject(value) {
|
|
|
14
27
|
return result;
|
|
15
28
|
}
|
|
16
29
|
// ------------------------------------------------------------------
|
|
30
|
+
// Object
|
|
31
|
+
// ------------------------------------------------------------------
|
|
32
|
+
function FromObject(value) {
|
|
33
|
+
return (Guard.IsClassInstance(value)
|
|
34
|
+
? FromClassInstance(value)
|
|
35
|
+
: FromObjectInstance(value));
|
|
36
|
+
}
|
|
37
|
+
// ------------------------------------------------------------------
|
|
17
38
|
// Array
|
|
18
39
|
// ------------------------------------------------------------------
|
|
19
40
|
function FromArray(value) {
|
|
@@ -46,6 +46,9 @@ function FromRecord(context, type) {
|
|
|
46
46
|
// Ref
|
|
47
47
|
// ------------------------------------------------------------------
|
|
48
48
|
function FromRef(context, type) {
|
|
49
|
+
if (visited.has(type.$ref))
|
|
50
|
+
return false;
|
|
51
|
+
visited.add(type.$ref);
|
|
49
52
|
return IsCodec(type) || (Guard.HasPropertyKey(context, type.$ref)
|
|
50
53
|
&& FromType(context, context[type.$ref]));
|
|
51
54
|
}
|
|
@@ -75,11 +78,16 @@ function FromType(context, type) {
|
|
|
75
78
|
IsUnion(type) ? FromUnion(context, type) :
|
|
76
79
|
IsCodec(type));
|
|
77
80
|
}
|
|
81
|
+
// ------------------------------------------------------------------
|
|
82
|
+
// Visited
|
|
83
|
+
// ------------------------------------------------------------------
|
|
84
|
+
const visited = new Set();
|
|
78
85
|
/** Returns true if this type contains a Codec */
|
|
79
86
|
export function HasCodec(...args) {
|
|
80
87
|
const [context, type] = Arguments.Match(args, {
|
|
81
88
|
2: (context, type) => [context, type],
|
|
82
89
|
1: (type) => [{}, type]
|
|
83
90
|
});
|
|
91
|
+
visited.clear();
|
|
84
92
|
return FromType(context, type);
|
|
85
93
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -33,7 +33,7 @@ import Type, { type Static } from 'typebox'
|
|
|
33
33
|
const T = Type.Object({ // const T = {
|
|
34
34
|
x: Type.Number(), // type: 'object',
|
|
35
35
|
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
36
|
-
z: Type.Number()
|
|
36
|
+
z: Type.Number() // properties: {
|
|
37
37
|
}) // x: { type: 'number' },
|
|
38
38
|
// y: { type: 'number' },
|
|
39
39
|
// z: { type: 'number' }
|
|
@@ -55,19 +55,22 @@ TypeBox is a runtime type system that creates in-memory Json Schema objects that
|
|
|
55
55
|
|
|
56
56
|
This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
58
|
License: MIT
|
|
61
59
|
|
|
62
60
|
## Contents
|
|
63
61
|
|
|
62
|
+
- [Upgrade](#Upgrade)
|
|
64
63
|
- [Type](#Type)
|
|
65
64
|
- [Script](#Script)
|
|
66
65
|
- [Value](#Value)
|
|
67
66
|
- [Compile](#Compile)
|
|
68
67
|
- [Contribute](#Contribute)
|
|
69
68
|
|
|
69
|
+
## Upgrade
|
|
70
|
+
|
|
71
|
+
Refer to the following URL for information on upgrading from 0.34.x to 1.0.
|
|
70
72
|
|
|
73
|
+
[1.0 Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
71
74
|
|
|
72
75
|
<a name="Type"></a>
|
|
73
76
|
|