typebox 1.0.65 → 1.0.67
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/guard/emit.d.mts +2 -1
- package/build/guard/emit.mjs +6 -3
- package/build/guard/guard.d.mts +6 -2
- package/build/guard/guard.mjs +12 -3
- package/build/guard/string.d.mts +10 -0
- package/build/guard/string.mjs +154 -0
- package/build/schema/engine/maxLength.mjs +2 -2
- package/build/schema/engine/minLength.mjs +2 -2
- package/package.json +1 -1
- package/readme.md +33 -9
package/build/guard/emit.d.mts
CHANGED
|
@@ -34,7 +34,8 @@ export declare function IsGreaterThan(left: string, right: string): string;
|
|
|
34
34
|
export declare function IsLessThan(left: string, right: string): string;
|
|
35
35
|
export declare function IsLessEqualThan(left: string, right: string): string;
|
|
36
36
|
export declare function IsGreaterEqualThan(left: string, right: string): string;
|
|
37
|
-
export declare function
|
|
37
|
+
export declare function IsMinLength(value: string, length: string): string;
|
|
38
|
+
export declare function IsMaxLength(value: string, length: string): string;
|
|
38
39
|
export declare function Every(value: string, offset: string, params: [value: string, index: string], expression: string): string;
|
|
39
40
|
export declare function Entries(value: string): string;
|
|
40
41
|
export declare function Keys(value: string): string;
|
package/build/guard/emit.mjs
CHANGED
|
@@ -104,8 +104,11 @@ export function IsGreaterEqualThan(left, right) {
|
|
|
104
104
|
// --------------------------------------------------------------------------
|
|
105
105
|
// String
|
|
106
106
|
// --------------------------------------------------------------------------
|
|
107
|
-
export function
|
|
108
|
-
return `Guard.
|
|
107
|
+
export function IsMinLength(value, length) {
|
|
108
|
+
return `Guard.IsMinLength(${value}, ${length})`;
|
|
109
|
+
}
|
|
110
|
+
export function IsMaxLength(value, length) {
|
|
111
|
+
return `Guard.IsMaxLength(${value}, ${length})`;
|
|
109
112
|
}
|
|
110
113
|
// --------------------------------------------------------------------------
|
|
111
114
|
// Array
|
|
@@ -181,7 +184,7 @@ export function ReduceOr(operands) {
|
|
|
181
184
|
return G.IsEqual(operands.length, 0) ? 'false' : operands.reduce((left, right) => Or(left, right));
|
|
182
185
|
}
|
|
183
186
|
// --------------------------------------------------------------------------
|
|
184
|
-
//
|
|
187
|
+
// Arithmetic
|
|
185
188
|
// --------------------------------------------------------------------------
|
|
186
189
|
export function PrefixIncrement(expression) {
|
|
187
190
|
return `++${expression}`;
|
package/build/guard/guard.d.mts
CHANGED
|
@@ -37,8 +37,12 @@ export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint
|
|
|
37
37
|
/** Returns true if the value appears to be an instance of a class. */
|
|
38
38
|
export declare function IsClassInstance(value: unknown): boolean;
|
|
39
39
|
export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
|
|
40
|
-
/** Returns the number of
|
|
41
|
-
export declare function
|
|
40
|
+
/** Returns the number of grapheme clusters in the string */
|
|
41
|
+
export declare function GraphemeCount(value: string): number;
|
|
42
|
+
/** Returns true if the string has at most the given number of graphemes */
|
|
43
|
+
export declare function IsMaxLength(value: string, length: number): boolean;
|
|
44
|
+
/** Returns true if the string has at least the given number of graphemes */
|
|
45
|
+
export declare function IsMinLength(value: string, length: number): boolean;
|
|
42
46
|
export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
43
47
|
export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
44
48
|
/** Returns true if this value has this property key */
|
package/build/guard/guard.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as String from './string.mjs';
|
|
1
2
|
// --------------------------------------------------------------------------
|
|
2
3
|
// Guards
|
|
3
4
|
// --------------------------------------------------------------------------
|
|
@@ -129,9 +130,17 @@ export function IsValueLike(value) {
|
|
|
129
130
|
// --------------------------------------------------------------------------
|
|
130
131
|
// String
|
|
131
132
|
// --------------------------------------------------------------------------
|
|
132
|
-
/** Returns the number of
|
|
133
|
-
export function
|
|
134
|
-
return
|
|
133
|
+
/** Returns the number of grapheme clusters in the string */
|
|
134
|
+
export function GraphemeCount(value) {
|
|
135
|
+
return String.GraphemeCount(value);
|
|
136
|
+
}
|
|
137
|
+
/** Returns true if the string has at most the given number of graphemes */
|
|
138
|
+
export function IsMaxLength(value, length) {
|
|
139
|
+
return String.IsMaxLengthFast(value, length);
|
|
140
|
+
}
|
|
141
|
+
/** Returns true if the string has at least the given number of graphemes */
|
|
142
|
+
export function IsMinLength(value, length) {
|
|
143
|
+
return String.IsMinLengthFast(value, length);
|
|
135
144
|
}
|
|
136
145
|
// --------------------------------------------------------------------------
|
|
137
146
|
// Array
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Returns the number of grapheme clusters in a string */
|
|
2
|
+
export declare function GraphemeCount(value: string): number;
|
|
3
|
+
/** Checks if a string has at least a minimum number of grapheme clusters */
|
|
4
|
+
export declare function IsMinLength(value: string, minLength: number): boolean;
|
|
5
|
+
/** Checks if a string has at most a maximum number of grapheme clusters */
|
|
6
|
+
export declare function IsMaxLength(value: string, maxLength: number): boolean;
|
|
7
|
+
/** Fast check for minimum grapheme length, falls back to full check if needed */
|
|
8
|
+
export declare function IsMinLengthFast(value: string, minLength: number): boolean;
|
|
9
|
+
/** Fast check for maximum grapheme length, falls back to full check if needed */
|
|
10
|
+
export declare function IsMaxLengthFast(value: string, maxLength: number): boolean;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// --------------------------------------------------------------------------
|
|
2
|
+
// IsBetween
|
|
3
|
+
// --------------------------------------------------------------------------
|
|
4
|
+
function IsBetween(value, min, max) {
|
|
5
|
+
return value >= min && value <= max;
|
|
6
|
+
}
|
|
7
|
+
// --------------------------------------------------------------------------
|
|
8
|
+
// IsRegionalIndicator
|
|
9
|
+
// --------------------------------------------------------------------------
|
|
10
|
+
function IsRegionalIndicator(value) {
|
|
11
|
+
return IsBetween(value, 0x1F1E6, 0x1F1FF);
|
|
12
|
+
}
|
|
13
|
+
// --------------------------------------------------------------------------
|
|
14
|
+
// IsVariationSelector
|
|
15
|
+
// --------------------------------------------------------------------------
|
|
16
|
+
function IsVariationSelector(value) {
|
|
17
|
+
return IsBetween(value, 0xFE00, 0xFE0F);
|
|
18
|
+
}
|
|
19
|
+
// --------------------------------------------------------------------------
|
|
20
|
+
// IsCombiningMark
|
|
21
|
+
// --------------------------------------------------------------------------
|
|
22
|
+
function IsCombiningMark(value) {
|
|
23
|
+
return (IsBetween(value, 0x0300, 0x036F) ||
|
|
24
|
+
IsBetween(value, 0x1AB0, 0x1AFF) ||
|
|
25
|
+
IsBetween(value, 0x1DC0, 0x1DFF) ||
|
|
26
|
+
IsBetween(value, 0xFE20, 0xFE2F));
|
|
27
|
+
}
|
|
28
|
+
// --------------------------------------------------------------------------
|
|
29
|
+
// CodePointLength
|
|
30
|
+
// --------------------------------------------------------------------------
|
|
31
|
+
function CodePointLength(value) {
|
|
32
|
+
return value > 0xFFFF ? 2 : 1;
|
|
33
|
+
}
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
35
|
+
// ConsumeModifiers (helper)
|
|
36
|
+
// --------------------------------------------------------------------------
|
|
37
|
+
function ConsumeModifiers(value, index) {
|
|
38
|
+
while (index < value.length) {
|
|
39
|
+
const point = value.codePointAt(index);
|
|
40
|
+
if (IsCombiningMark(point) || IsVariationSelector(point)) {
|
|
41
|
+
index += CodePointLength(point);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return index;
|
|
48
|
+
}
|
|
49
|
+
// --------------------------------------------------------------------------
|
|
50
|
+
// NextGraphemeClusterIndex
|
|
51
|
+
// --------------------------------------------------------------------------
|
|
52
|
+
function NextGraphemeClusterIndex(value, clusterStart) {
|
|
53
|
+
const startCP = value.codePointAt(clusterStart);
|
|
54
|
+
let clusterEnd = clusterStart + CodePointLength(startCP);
|
|
55
|
+
// Consume combining marks & variation selectors
|
|
56
|
+
clusterEnd = ConsumeModifiers(value, clusterEnd);
|
|
57
|
+
// Handle multi-ZWJ sequences
|
|
58
|
+
while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
|
|
59
|
+
const nextCP = value.codePointAt(clusterEnd + 1);
|
|
60
|
+
clusterEnd += 1 + CodePointLength(nextCP);
|
|
61
|
+
clusterEnd = ConsumeModifiers(value, clusterEnd);
|
|
62
|
+
}
|
|
63
|
+
// Handle regional indicator pairs (flags)
|
|
64
|
+
if (IsRegionalIndicator(startCP) &&
|
|
65
|
+
clusterEnd < value.length &&
|
|
66
|
+
IsRegionalIndicator(value.codePointAt(clusterEnd))) {
|
|
67
|
+
clusterEnd += CodePointLength(value.codePointAt(clusterEnd));
|
|
68
|
+
}
|
|
69
|
+
return clusterEnd;
|
|
70
|
+
}
|
|
71
|
+
// --------------------------------------------------------------------------
|
|
72
|
+
// IsGraphemeCodePoint
|
|
73
|
+
// --------------------------------------------------------------------------
|
|
74
|
+
function IsGraphemeCodePoint(value) {
|
|
75
|
+
return (IsBetween(value, 0xD800, 0xDBFF) || // High surrogate
|
|
76
|
+
IsBetween(value, 0x0300, 0x036F) || // Combining diacritical marks
|
|
77
|
+
(value === 0x200D) // Zero-width joiner
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
// --------------------------------------------------------------------------
|
|
81
|
+
// GraphemeCount
|
|
82
|
+
// --------------------------------------------------------------------------
|
|
83
|
+
/** Returns the number of grapheme clusters in a string */
|
|
84
|
+
export function GraphemeCount(value) {
|
|
85
|
+
let count = 0;
|
|
86
|
+
let index = 0;
|
|
87
|
+
while (index < value.length) {
|
|
88
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
89
|
+
count++;
|
|
90
|
+
}
|
|
91
|
+
return count;
|
|
92
|
+
}
|
|
93
|
+
// --------------------------------------------------------------------------
|
|
94
|
+
// IsMinLength
|
|
95
|
+
// --------------------------------------------------------------------------
|
|
96
|
+
/** Checks if a string has at least a minimum number of grapheme clusters */
|
|
97
|
+
export function IsMinLength(value, minLength) {
|
|
98
|
+
let count = 0;
|
|
99
|
+
let index = 0;
|
|
100
|
+
while (index < value.length) {
|
|
101
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
102
|
+
count++;
|
|
103
|
+
if (count >= minLength)
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
// --------------------------------------------------------------------------
|
|
109
|
+
// IsMaxLength
|
|
110
|
+
// --------------------------------------------------------------------------
|
|
111
|
+
/** Checks if a string has at most a maximum number of grapheme clusters */
|
|
112
|
+
export function IsMaxLength(value, maxLength) {
|
|
113
|
+
let count = 0;
|
|
114
|
+
let index = 0;
|
|
115
|
+
while (index < value.length) {
|
|
116
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
117
|
+
count++;
|
|
118
|
+
if (count > maxLength)
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
// --------------------------------------------------------------------------
|
|
124
|
+
// IsMinLengthFast
|
|
125
|
+
// --------------------------------------------------------------------------
|
|
126
|
+
/** Fast check for minimum grapheme length, falls back to full check if needed */
|
|
127
|
+
export function IsMinLengthFast(value, minLength) {
|
|
128
|
+
let index = 0;
|
|
129
|
+
while (index < value.length) {
|
|
130
|
+
if (IsGraphemeCodePoint(value.charCodeAt(index))) {
|
|
131
|
+
return IsMinLength(value, minLength);
|
|
132
|
+
}
|
|
133
|
+
index++;
|
|
134
|
+
if (index >= minLength)
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
// --------------------------------------------------------------------------
|
|
140
|
+
// IsMaxLengthFast
|
|
141
|
+
// --------------------------------------------------------------------------
|
|
142
|
+
/** Fast check for maximum grapheme length, falls back to full check if needed */
|
|
143
|
+
export function IsMaxLengthFast(value, maxLength) {
|
|
144
|
+
let index = 0;
|
|
145
|
+
while (index < value.length) {
|
|
146
|
+
if (IsGraphemeCodePoint(value.charCodeAt(index))) {
|
|
147
|
+
return IsMaxLength(value, maxLength);
|
|
148
|
+
}
|
|
149
|
+
index++;
|
|
150
|
+
if (index > maxLength)
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
|
|
|
4
4
|
// Build
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
export function BuildMaxLength(stack, context, schema, value) {
|
|
7
|
-
return E.
|
|
7
|
+
return E.IsMaxLength(value, E.Constant(schema.maxLength));
|
|
8
8
|
}
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Check
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
export function CheckMaxLength(stack, context, schema, value) {
|
|
13
|
-
return G.
|
|
13
|
+
return G.IsMaxLength(value, schema.maxLength);
|
|
14
14
|
}
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
// Error
|
|
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
|
|
|
4
4
|
// Build
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
export function BuildMinLength(stack, context, schema, value) {
|
|
7
|
-
return E.
|
|
7
|
+
return E.IsMinLength(value, E.Constant(schema.minLength));
|
|
8
8
|
}
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Check
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
export function CheckMinLength(stack, context, schema, value) {
|
|
13
|
-
return G.
|
|
13
|
+
return G.IsMinLength(value, schema.minLength);
|
|
14
14
|
}
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
// Error
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -25,9 +25,13 @@ $ npm install typebox
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
+
A TypeScript engine for Json Schema [Reference](https://tsplay.dev/wOyMRm)
|
|
29
|
+
|
|
28
30
|
```typescript
|
|
29
31
|
import Type from 'typebox'
|
|
30
32
|
|
|
33
|
+
// Json Schema
|
|
34
|
+
|
|
31
35
|
const T = Type.Object({ // const T = {
|
|
32
36
|
x: Type.Number(), // type: 'object',
|
|
33
37
|
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
@@ -43,6 +47,26 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
43
47
|
// y: number,
|
|
44
48
|
// z: number
|
|
45
49
|
// }
|
|
50
|
+
|
|
51
|
+
// TypeScript
|
|
52
|
+
|
|
53
|
+
const { S } = Type.Script({ T }, `
|
|
54
|
+
type RenameKey<K> =
|
|
55
|
+
K extends 'x' ? 'a' :
|
|
56
|
+
K extends 'y' ? 'b' :
|
|
57
|
+
K extends 'z' ? 'c' :
|
|
58
|
+
K
|
|
59
|
+
|
|
60
|
+
type S = {
|
|
61
|
+
readonly [K in keyof T as RenameKey<K>]: string
|
|
62
|
+
}
|
|
63
|
+
`)
|
|
64
|
+
|
|
65
|
+
type S = Type.Static<typeof S> // type S = {
|
|
66
|
+
// readonly a: string,
|
|
67
|
+
// readonly b: string,
|
|
68
|
+
// readonly c: string
|
|
69
|
+
// }
|
|
46
70
|
```
|
|
47
71
|
|
|
48
72
|
## Overview
|
|
@@ -75,9 +99,9 @@ If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the fo
|
|
|
75
99
|
|
|
76
100
|
## Type
|
|
77
101
|
|
|
78
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://
|
|
102
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://tsplay.dev/NaMoBN)
|
|
79
103
|
|
|
80
|
-
TypeBox
|
|
104
|
+
TypeBox provides many functions to create Json Schema types. Each function returns a small Json Schema fragment that can be composed into more complex types. TypeBox includes a set of functions that are used to construct Json Schema compliant schematics as well as a set of extended functions that return schematics for constructs native to JavaScript.
|
|
81
105
|
|
|
82
106
|
## Example
|
|
83
107
|
|
|
@@ -123,9 +147,9 @@ const S = Type.Number({ // const S = {
|
|
|
123
147
|
|
|
124
148
|
## Value
|
|
125
149
|
|
|
126
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://
|
|
150
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://tsplay.dev/W4YE1w)
|
|
127
151
|
|
|
128
|
-
The Value
|
|
152
|
+
The Value submodule provides functions for validation and other typed operations on JavaScript values. It includes functions such as Check, Parse, Clone, Encode, and Decode, as well as advanced functions for performing structural Diff and Patch operations on dynamic JavaScript values.
|
|
129
153
|
|
|
130
154
|
```typescript
|
|
131
155
|
import Value from 'typebox/value'
|
|
@@ -154,9 +178,9 @@ const A = Value.Parse(T, { // const A: {
|
|
|
154
178
|
|
|
155
179
|
## Compile
|
|
156
180
|
|
|
157
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://
|
|
181
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
|
|
158
182
|
|
|
159
|
-
The Compile
|
|
183
|
+
The Compile submodule is a high-performance Json Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
|
|
160
184
|
|
|
161
185
|
```typescript
|
|
162
186
|
import { Compile } from 'typebox/compile'
|
|
@@ -182,9 +206,9 @@ const A = C.Parse({ // const A: {
|
|
|
182
206
|
|
|
183
207
|
## Script
|
|
184
208
|
|
|
185
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://
|
|
209
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://tsplay.dev/Wk6L1m) | [Example 2](https://tsplay.dev/NnrJoN)
|
|
186
210
|
|
|
187
|
-
TypeBox
|
|
211
|
+
TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute Json Schema using native TypeScript syntax. The engine is implemented symmetrically at runtime and within the TypeScript type system, and is intended for use with the TypeScript 7 native compiler and above.
|
|
188
212
|
|
|
189
213
|
```typescript
|
|
190
214
|
// Scripted Type
|
|
@@ -228,7 +252,7 @@ type S = Type.Static<typeof S> // type S = {
|
|
|
228
252
|
|
|
229
253
|
## Schema
|
|
230
254
|
|
|
231
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://
|
|
255
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://tsplay.dev/Wvrv3W) | [Example 2](https://tsplay.dev/m3g0ym)
|
|
232
256
|
|
|
233
257
|
TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of Json Schema schematics. TypeBox implements Draft 3 to 2020-12 and is compliance tested via the official Json Schema [Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite). It offers high-performance JIT compilation with automatic fallback to dynamic checking in JIT restricted environments.
|
|
234
258
|
|