typebox 1.0.64 → 1.0.66
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/guard.d.mts
CHANGED
|
@@ -37,8 +37,6 @@ 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 Unicode Grapheme Clusters */
|
|
41
|
-
export declare function StringGraphemeCount(value: string): number;
|
|
42
40
|
export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
43
41
|
export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
44
42
|
/** Returns true if this value has this property key */
|
|
@@ -57,3 +55,5 @@ export declare function Symbols(value: Record<PropertyKey, unknown>): symbol[];
|
|
|
57
55
|
export declare function Values(value: Record<PropertyKey, unknown>): unknown[];
|
|
58
56
|
/** Tests values for deep equality */
|
|
59
57
|
export declare function IsDeepEqual(left: unknown, right: unknown): boolean;
|
|
58
|
+
/** Returns the number of Unicode Grapheme Clusters */
|
|
59
|
+
export declare function StringGraphemeCount(value: string): number;
|
package/build/guard/guard.mjs
CHANGED
|
@@ -127,13 +127,6 @@ export function IsValueLike(value) {
|
|
|
127
127
|
IsUndefined(value);
|
|
128
128
|
}
|
|
129
129
|
// --------------------------------------------------------------------------
|
|
130
|
-
// String
|
|
131
|
-
// --------------------------------------------------------------------------
|
|
132
|
-
/** Returns the number of Unicode Grapheme Clusters */
|
|
133
|
-
export function StringGraphemeCount(value) {
|
|
134
|
-
return Array.from(value).length;
|
|
135
|
-
}
|
|
136
|
-
// --------------------------------------------------------------------------
|
|
137
130
|
// Array
|
|
138
131
|
// --------------------------------------------------------------------------
|
|
139
132
|
export function Every(value, offset, callback) {
|
|
@@ -197,3 +190,84 @@ function DeepEqualArray(left, right) {
|
|
|
197
190
|
export function IsDeepEqual(left, right) {
|
|
198
191
|
return (IsArray(left) ? DeepEqualArray(left, right) : IsObject(left) ? DeepEqualObject(left, right) : IsEqual(left, right));
|
|
199
192
|
}
|
|
193
|
+
// --------------------------------------------------------------------------
|
|
194
|
+
// StringGraphemeCountIntl - Intl.Segmenter Polyfill
|
|
195
|
+
// --------------------------------------------------------------------------
|
|
196
|
+
//
|
|
197
|
+
// const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
198
|
+
// function StringGraphemeCountIntl(value: string): number {
|
|
199
|
+
// const iterator = segmenter.segment(value)[Symbol.iterator]()
|
|
200
|
+
// let length = 0
|
|
201
|
+
// while (!iterator.next().done) length++
|
|
202
|
+
// return length
|
|
203
|
+
// }
|
|
204
|
+
//
|
|
205
|
+
// --------------------------------------------------------------------------
|
|
206
|
+
function IsRegionalIndicator(value) {
|
|
207
|
+
return value >= 0x1F1E6 && value <= 0x1F1FF;
|
|
208
|
+
}
|
|
209
|
+
function IsVariationSelector(value) {
|
|
210
|
+
return value >= 0xFE00 && value <= 0xFE0F;
|
|
211
|
+
}
|
|
212
|
+
function IsCombiningMark(value) {
|
|
213
|
+
return ((value >= 0x0300 && value <= 0x036F) ||
|
|
214
|
+
(value >= 0x1AB0 && value <= 0x1AFF) ||
|
|
215
|
+
(value >= 0x1DC0 && value <= 0x1DFF) ||
|
|
216
|
+
(value >= 0xFE20 && value <= 0xFE2F));
|
|
217
|
+
}
|
|
218
|
+
function CodePointLength(value) {
|
|
219
|
+
return value > 0xFFFF ? 2 : 1;
|
|
220
|
+
}
|
|
221
|
+
function StringGraphemeCountIntl(value) {
|
|
222
|
+
let count = 0;
|
|
223
|
+
let index = 0;
|
|
224
|
+
while (index < value.length) {
|
|
225
|
+
const start = value.codePointAt(index);
|
|
226
|
+
let clusterEnd = index + CodePointLength(start);
|
|
227
|
+
// Combining marks & variation selectors
|
|
228
|
+
while (clusterEnd < value.length) {
|
|
229
|
+
const next = value.codePointAt(clusterEnd);
|
|
230
|
+
if (IsCombiningMark(next) || IsVariationSelector(next)) {
|
|
231
|
+
clusterEnd += CodePointLength(next);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// ZWJ sequences
|
|
238
|
+
while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
|
|
239
|
+
const next = value.codePointAt(clusterEnd + 1);
|
|
240
|
+
clusterEnd += 1 + CodePointLength(next);
|
|
241
|
+
}
|
|
242
|
+
// Regional indicator pairs (flags)
|
|
243
|
+
const first = IsRegionalIndicator(start);
|
|
244
|
+
const second = clusterEnd < value.length && IsRegionalIndicator(value.codePointAt(clusterEnd));
|
|
245
|
+
if (first && second) {
|
|
246
|
+
const next = value.codePointAt(clusterEnd);
|
|
247
|
+
clusterEnd += CodePointLength(next);
|
|
248
|
+
}
|
|
249
|
+
count++;
|
|
250
|
+
index = clusterEnd;
|
|
251
|
+
}
|
|
252
|
+
return count;
|
|
253
|
+
}
|
|
254
|
+
// --------------------------------------------------------------------------
|
|
255
|
+
// StringGraphemeCount
|
|
256
|
+
// --------------------------------------------------------------------------
|
|
257
|
+
function IsComplexGraphemeCodeUnit(value) {
|
|
258
|
+
return ((value >= 0xD800 && value <= 0xDBFF) || // High surrogate
|
|
259
|
+
(value >= 0x0300 && value <= 0x036F) || // Combining diacritical marks
|
|
260
|
+
(value === 0x200D) // Zero-width joiner
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
/** Returns the number of Unicode Grapheme Clusters */
|
|
264
|
+
export function StringGraphemeCount(value) {
|
|
265
|
+
let count = 0;
|
|
266
|
+
for (let index = 0; index < value.length; index++) {
|
|
267
|
+
if (IsComplexGraphemeCodeUnit(value.charCodeAt(index))) {
|
|
268
|
+
return StringGraphemeCountIntl(value);
|
|
269
|
+
}
|
|
270
|
+
count++;
|
|
271
|
+
}
|
|
272
|
+
return count;
|
|
273
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { XSchema } from '../types/schema.mjs';
|
|
2
|
+
import type { XStaticSchema } from './schema.mjs';
|
|
3
|
+
import type { XIf } from '../types/if.mjs';
|
|
4
|
+
import type { XElse } from '../types/else.mjs';
|
|
5
|
+
import type { XThen } from '../types/then.mjs';
|
|
6
|
+
type XStaticIfReduce<Types extends unknown[], Result extends unknown = never> = (Types extends [infer Left extends unknown, ...infer Right extends unknown[]] ? XStaticIfReduce<Right, Result | Left> : Result);
|
|
7
|
+
export type XStaticIf<Stack extends string[], Root extends XSchema, Schema extends XIf, IfSchema extends XSchema, Then extends unknown[] = Schema extends XThen<infer ThenSchema extends XSchema> ? [XStaticSchema<Stack, Root, IfSchema> & XStaticSchema<Stack, Root, ThenSchema>] : [], Else extends unknown[] = Schema extends XElse<infer ElseSchema extends XSchema> ? [...Then, XStaticSchema<Stack, Root, ElseSchema>] : Then, Result extends unknown = Else extends [] ? unknown : XStaticIfReduce<Else>> = Result;
|
|
8
|
+
export {};
|
|
@@ -3,6 +3,7 @@ import type { XAnyOf } from '../types/anyOf.mjs';
|
|
|
3
3
|
import type { XAllOf } from '../types/allOf.mjs';
|
|
4
4
|
import type { XConst } from '../types/const.mjs';
|
|
5
5
|
import type { XEnum } from '../types/enum.mjs';
|
|
6
|
+
import type { XIf } from '../types/if.mjs';
|
|
6
7
|
import type { XItems } from '../types/items.mjs';
|
|
7
8
|
import type { XOneOf } from '../types/oneOf.mjs';
|
|
8
9
|
import type { XPatternProperties } from '../types/patternProperties.mjs';
|
|
@@ -18,6 +19,7 @@ import type { XStaticAllOf } from './allOf.mjs';
|
|
|
18
19
|
import type { XStaticAnyOf } from './anyOf.mjs';
|
|
19
20
|
import type { XStaticConst } from './const.mjs';
|
|
20
21
|
import type { XStaticEnum } from './enum.mjs';
|
|
22
|
+
import type { XStaticIf } from './if.mjs';
|
|
21
23
|
import type { XStaticItems } from './items.mjs';
|
|
22
24
|
import type { XStaticOneOf } from './oneOf.mjs';
|
|
23
25
|
import type { XStaticPatternProperties } from './patternProperties.mjs';
|
|
@@ -32,7 +34,7 @@ type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends
|
|
|
32
34
|
Schema extends XAllOf<infer Types extends XSchema[]> ? XStaticAllOf<Stack, Root, Types> : unknown,
|
|
33
35
|
Schema extends XAnyOf<infer Types extends XSchema[]> ? XStaticAnyOf<Stack, Root, Types> : unknown,
|
|
34
36
|
Schema extends XConst<infer Value extends unknown> ? XStaticConst<Value> : unknown,
|
|
35
|
-
Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
|
|
37
|
+
Schema extends XIf<infer Type extends XSchema> ? XStaticIf<Stack, Root, Schema, Type> : Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
|
|
36
38
|
Schema extends XItems<infer Types extends XSchema[] | XSchema> ? XStaticItems<Stack, Root, Schema, Types> : unknown,
|
|
37
39
|
Schema extends XOneOf<infer Types extends XSchema[]> ? XStaticOneOf<Stack, Root, Types> : unknown,
|
|
38
40
|
Schema extends XPatternProperties<infer Properties extends Record<PropertyKey, XSchema>> ? XStaticPatternProperties<Stack, Root, Properties> : unknown,
|
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
|
|