@sinclair/typebox 0.32.11 → 0.32.13
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/import/compiler/compiler.mjs +4 -0
- package/build/import/errors/errors.mjs +8 -0
- package/build/import/index.d.mts +1 -1
- package/build/import/type/array/array.d.mts +5 -2
- package/build/import/type/array/array.mjs +1 -1
- package/build/import/type/guard/type.mjs +3 -1
- package/build/import/type/record/record.d.mts +3 -1
- package/build/import/type/regexp/regexp.d.mts +8 -2
- package/build/import/type/type/javascript.d.mts +3 -3
- package/build/import/type/type/json.mjs +1 -1
- package/build/import/value/check/check.mjs +8 -0
- package/build/require/compiler/compiler.js +4 -0
- package/build/require/errors/errors.js +8 -0
- package/build/require/index.d.ts +1 -1
- package/build/require/type/array/array.d.ts +5 -2
- package/build/require/type/array/array.js +1 -1
- package/build/require/type/guard/type.js +3 -1
- package/build/require/type/record/record.d.ts +3 -1
- package/build/require/type/regexp/regexp.d.ts +8 -2
- package/build/require/type/type/javascript.d.ts +3 -3
- package/build/require/type/type/json.js +1 -1
- package/build/require/value/check/check.js +8 -0
- package/package.json +1 -1
|
@@ -387,6 +387,10 @@ export var TypeCompiler;
|
|
|
387
387
|
function* FromRegExp(schema, references, value) {
|
|
388
388
|
const variable = CreateVariable(`${new RegExp(schema.source, schema.flags)};`);
|
|
389
389
|
yield `(typeof ${value} === 'string')`;
|
|
390
|
+
if (IsNumber(schema.maxLength))
|
|
391
|
+
yield `${value}.length <= ${schema.maxLength}`;
|
|
392
|
+
if (IsNumber(schema.minLength))
|
|
393
|
+
yield `${value}.length >= ${schema.minLength}`;
|
|
390
394
|
yield `${variable}.test(${value})`;
|
|
391
395
|
}
|
|
392
396
|
function* FromString(schema, references, value) {
|
|
@@ -393,6 +393,14 @@ function* FromRef(schema, references, path, value) {
|
|
|
393
393
|
yield* Visit(Deref(schema, references), references, path, value);
|
|
394
394
|
}
|
|
395
395
|
function* FromRegExp(schema, references, path, value) {
|
|
396
|
+
if (!IsString(value))
|
|
397
|
+
return yield Create(ValueErrorType.String, schema, path, value);
|
|
398
|
+
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
399
|
+
yield Create(ValueErrorType.StringMinLength, schema, path, value);
|
|
400
|
+
}
|
|
401
|
+
if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
402
|
+
yield Create(ValueErrorType.StringMaxLength, schema, path, value);
|
|
403
|
+
}
|
|
396
404
|
const regex = new RegExp(schema.source, schema.flags);
|
|
397
405
|
if (!regex.test(value)) {
|
|
398
406
|
return yield Create(ValueErrorType.RegExp, schema, path, value);
|
package/build/import/index.d.mts
CHANGED
|
@@ -48,7 +48,7 @@ export { ReadonlyOptional, type TReadonlyOptional } from './type/readonly-option
|
|
|
48
48
|
export { Record, type TRecord, type TRecordOrObject } from './type/record/index.mjs';
|
|
49
49
|
export { Recursive, type TRecursive, type TThis } from './type/recursive/index.mjs';
|
|
50
50
|
export { Ref, type TRef } from './type/ref/index.mjs';
|
|
51
|
-
export { RegExp, type TRegExp } from './type/regexp/index.mjs';
|
|
51
|
+
export { RegExp, type TRegExp, type RegExpOptions } from './type/regexp/index.mjs';
|
|
52
52
|
export { Required, type TRequired, type TRequiredFromMappedResult } from './type/required/index.mjs';
|
|
53
53
|
export { Rest, type TRest } from './type/rest/index.mjs';
|
|
54
54
|
export { ReturnType, type TReturnType } from './type/return-type/index.mjs';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Ensure } from '../helpers/index.mjs';
|
|
2
|
+
import type { SchemaOptions, TSchema } from '../schema/index.mjs';
|
|
2
3
|
import type { Static } from '../static/index.mjs';
|
|
3
4
|
import { Kind } from '../symbols/index.mjs';
|
|
4
5
|
export interface ArrayOptions extends SchemaOptions {
|
|
@@ -15,11 +16,13 @@ export interface ArrayOptions extends SchemaOptions {
|
|
|
15
16
|
/** A maximum number of contains schema matches */
|
|
16
17
|
maxContains?: number;
|
|
17
18
|
}
|
|
19
|
+
type ArrayStatic<T extends TSchema, P extends unknown[]> = Ensure<Static<T, P>[]>;
|
|
18
20
|
export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
|
|
19
21
|
[Kind]: 'Array';
|
|
20
|
-
static:
|
|
22
|
+
static: ArrayStatic<T, this['params']>;
|
|
21
23
|
type: 'array';
|
|
22
24
|
items: T;
|
|
23
25
|
}
|
|
24
26
|
/** `[Json]` Creates an Array type */
|
|
25
27
|
export declare function Array<T extends TSchema>(schema: T, options?: ArrayOptions): TArray<T>;
|
|
28
|
+
export {};
|
|
@@ -331,7 +331,9 @@ export function IsRegExp(value) {
|
|
|
331
331
|
return (IsKindOf(value, 'RegExp') &&
|
|
332
332
|
IsOptionalString(value.$id) &&
|
|
333
333
|
ValueGuard.IsString(value.source) &&
|
|
334
|
-
ValueGuard.IsString(value.flags)
|
|
334
|
+
ValueGuard.IsString(value.flags) &&
|
|
335
|
+
IsOptionalNumber(value.maxLength) &&
|
|
336
|
+
IsOptionalNumber(value.minLength));
|
|
335
337
|
}
|
|
336
338
|
/** Returns true if the given value is TString */
|
|
337
339
|
export function IsString(value) {
|
|
@@ -35,7 +35,9 @@ type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TReg
|
|
|
35
35
|
type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
|
|
36
36
|
type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
37
37
|
type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
38
|
-
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<
|
|
38
|
+
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
|
|
39
|
+
[_ in Assert<Static<K>, PropertyKey>]: Static<T, P>;
|
|
40
|
+
}>);
|
|
39
41
|
export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
|
|
40
42
|
[Kind]: 'Record';
|
|
41
43
|
static: RecordStatic<K, T, this['params']>;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { SchemaOptions } from '../schema/index.mjs';
|
|
2
2
|
import type { TSchema } from '../schema/index.mjs';
|
|
3
3
|
import { Kind } from '../symbols/index.mjs';
|
|
4
|
+
export interface RegExpOptions extends SchemaOptions {
|
|
5
|
+
/** The maximum length of the string */
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
/** The minimum length of the string */
|
|
8
|
+
minLength?: number;
|
|
9
|
+
}
|
|
4
10
|
export interface TRegExp extends TSchema {
|
|
5
11
|
[Kind]: 'RegExp';
|
|
6
12
|
static: `${string}`;
|
|
@@ -9,6 +15,6 @@ export interface TRegExp extends TSchema {
|
|
|
9
15
|
flags: string;
|
|
10
16
|
}
|
|
11
17
|
/** `[JavaScript]` Creates a RegExp type */
|
|
12
|
-
export declare function RegExp(pattern: string, options?:
|
|
18
|
+
export declare function RegExp(pattern: string, options?: RegExpOptions): TRegExp;
|
|
13
19
|
/** `[JavaScript]` Creates a RegExp type */
|
|
14
|
-
export declare function RegExp(regex: RegExp, options?:
|
|
20
|
+
export declare function RegExp(regex: RegExp, options?: RegExpOptions): TRegExp;
|
|
@@ -10,7 +10,7 @@ import { type TInstanceType } from '../instance-type/index.mjs';
|
|
|
10
10
|
import { type TIterator } from '../iterator/index.mjs';
|
|
11
11
|
import { type TParameters } from '../parameters/index.mjs';
|
|
12
12
|
import { type TPromise } from '../promise/index.mjs';
|
|
13
|
-
import { type TRegExp } from '../regexp/index.mjs';
|
|
13
|
+
import { type TRegExp, RegExpOptions } from '../regexp/index.mjs';
|
|
14
14
|
import { type TReturnType } from '../return-type/index.mjs';
|
|
15
15
|
import { type TSchema, type SchemaOptions } from '../schema/index.mjs';
|
|
16
16
|
import { type TSymbol } from '../symbol/index.mjs';
|
|
@@ -42,9 +42,9 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
42
42
|
/** `[JavaScript]` Creates a Promise type */
|
|
43
43
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
44
44
|
/** `[JavaScript]` Creates a RegExp type */
|
|
45
|
-
RegExp(pattern: string, options?:
|
|
45
|
+
RegExp(pattern: string, options?: RegExpOptions): TRegExp;
|
|
46
46
|
/** `[JavaScript]` Creates a RegExp type */
|
|
47
|
-
RegExp(regex: RegExp, options?:
|
|
47
|
+
RegExp(regex: RegExp, options?: RegExpOptions): TRegExp;
|
|
48
48
|
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
49
49
|
ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>;
|
|
50
50
|
/** `[JavaScript]` Creates a Symbol type */
|
|
@@ -172,7 +172,7 @@ export class JsonTypeBuilder {
|
|
|
172
172
|
}
|
|
173
173
|
/** `[Json]` Creates a Record type */
|
|
174
174
|
Record(key, schema, options = {}) {
|
|
175
|
-
return Record(key, schema);
|
|
175
|
+
return Record(key, schema, options);
|
|
176
176
|
}
|
|
177
177
|
/** `[Json]` Creates a Recursive type */
|
|
178
178
|
Recursive(callback, options = {}) {
|
|
@@ -287,6 +287,14 @@ function FromRef(schema, references, value) {
|
|
|
287
287
|
}
|
|
288
288
|
function FromRegExp(schema, references, value) {
|
|
289
289
|
const regex = new RegExp(schema.source, schema.flags);
|
|
290
|
+
if (IsDefined(schema.minLength)) {
|
|
291
|
+
if (!(value.length >= schema.minLength))
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
if (IsDefined(schema.maxLength)) {
|
|
295
|
+
if (!(value.length <= schema.maxLength))
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
290
298
|
return regex.test(value);
|
|
291
299
|
}
|
|
292
300
|
function FromString(schema, references, value) {
|
|
@@ -387,6 +387,10 @@ var TypeCompiler;
|
|
|
387
387
|
function* FromRegExp(schema, references, value) {
|
|
388
388
|
const variable = CreateVariable(`${new RegExp(schema.source, schema.flags)};`);
|
|
389
389
|
yield `(typeof ${value} === 'string')`;
|
|
390
|
+
if ((0, index_11.IsNumber)(schema.maxLength))
|
|
391
|
+
yield `${value}.length <= ${schema.maxLength}`;
|
|
392
|
+
if ((0, index_11.IsNumber)(schema.minLength))
|
|
393
|
+
yield `${value}.length >= ${schema.minLength}`;
|
|
390
394
|
yield `${variable}.test(${value})`;
|
|
391
395
|
}
|
|
392
396
|
function* FromString(schema, references, value) {
|
|
@@ -397,6 +397,14 @@ function* FromRef(schema, references, path, value) {
|
|
|
397
397
|
yield* Visit((0, index_5.Deref)(schema, references), references, path, value);
|
|
398
398
|
}
|
|
399
399
|
function* FromRegExp(schema, references, path, value) {
|
|
400
|
+
if (!(0, index_9.IsString)(value))
|
|
401
|
+
return yield Create(ValueErrorType.String, schema, path, value);
|
|
402
|
+
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
403
|
+
yield Create(ValueErrorType.StringMinLength, schema, path, value);
|
|
404
|
+
}
|
|
405
|
+
if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
406
|
+
yield Create(ValueErrorType.StringMaxLength, schema, path, value);
|
|
407
|
+
}
|
|
400
408
|
const regex = new RegExp(schema.source, schema.flags);
|
|
401
409
|
if (!regex.test(value)) {
|
|
402
410
|
return yield Create(ValueErrorType.RegExp, schema, path, value);
|
package/build/require/index.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export { ReadonlyOptional, type TReadonlyOptional } from './type/readonly-option
|
|
|
48
48
|
export { Record, type TRecord, type TRecordOrObject } from './type/record/index';
|
|
49
49
|
export { Recursive, type TRecursive, type TThis } from './type/recursive/index';
|
|
50
50
|
export { Ref, type TRef } from './type/ref/index';
|
|
51
|
-
export { RegExp, type TRegExp } from './type/regexp/index';
|
|
51
|
+
export { RegExp, type TRegExp, type RegExpOptions } from './type/regexp/index';
|
|
52
52
|
export { Required, type TRequired, type TRequiredFromMappedResult } from './type/required/index';
|
|
53
53
|
export { Rest, type TRest } from './type/rest/index';
|
|
54
54
|
export { ReturnType, type TReturnType } from './type/return-type/index';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Ensure } from '../helpers/index';
|
|
2
|
+
import type { SchemaOptions, TSchema } from '../schema/index';
|
|
2
3
|
import type { Static } from '../static/index';
|
|
3
4
|
import { Kind } from '../symbols/index';
|
|
4
5
|
export interface ArrayOptions extends SchemaOptions {
|
|
@@ -15,11 +16,13 @@ export interface ArrayOptions extends SchemaOptions {
|
|
|
15
16
|
/** A maximum number of contains schema matches */
|
|
16
17
|
maxContains?: number;
|
|
17
18
|
}
|
|
19
|
+
type ArrayStatic<T extends TSchema, P extends unknown[]> = Ensure<Static<T, P>[]>;
|
|
18
20
|
export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptions {
|
|
19
21
|
[Kind]: 'Array';
|
|
20
|
-
static:
|
|
22
|
+
static: ArrayStatic<T, this['params']>;
|
|
21
23
|
type: 'array';
|
|
22
24
|
items: T;
|
|
23
25
|
}
|
|
24
26
|
/** `[Json]` Creates an Array type */
|
|
25
27
|
export declare function Array<T extends TSchema>(schema: T, options?: ArrayOptions): TArray<T>;
|
|
28
|
+
export {};
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Array = void 0;
|
|
5
|
-
const index_1 = require("../symbols/index");
|
|
6
5
|
const type_1 = require("../clone/type");
|
|
6
|
+
const index_1 = require("../symbols/index");
|
|
7
7
|
/** `[Json]` Creates an Array type */
|
|
8
8
|
function Array(schema, options = {}) {
|
|
9
9
|
return {
|
|
@@ -367,7 +367,9 @@ function IsRegExp(value) {
|
|
|
367
367
|
return (IsKindOf(value, 'RegExp') &&
|
|
368
368
|
IsOptionalString(value.$id) &&
|
|
369
369
|
ValueGuard.IsString(value.source) &&
|
|
370
|
-
ValueGuard.IsString(value.flags)
|
|
370
|
+
ValueGuard.IsString(value.flags) &&
|
|
371
|
+
IsOptionalNumber(value.maxLength) &&
|
|
372
|
+
IsOptionalNumber(value.minLength));
|
|
371
373
|
}
|
|
372
374
|
exports.IsRegExp = IsRegExp;
|
|
373
375
|
/** Returns true if the given value is TString */
|
|
@@ -35,7 +35,9 @@ type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TReg
|
|
|
35
35
|
type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
|
|
36
36
|
type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
37
37
|
type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
38
|
-
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<
|
|
38
|
+
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
|
|
39
|
+
[_ in Assert<Static<K>, PropertyKey>]: Static<T, P>;
|
|
40
|
+
}>);
|
|
39
41
|
export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
|
|
40
42
|
[Kind]: 'Record';
|
|
41
43
|
static: RecordStatic<K, T, this['params']>;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { SchemaOptions } from '../schema/index';
|
|
2
2
|
import type { TSchema } from '../schema/index';
|
|
3
3
|
import { Kind } from '../symbols/index';
|
|
4
|
+
export interface RegExpOptions extends SchemaOptions {
|
|
5
|
+
/** The maximum length of the string */
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
/** The minimum length of the string */
|
|
8
|
+
minLength?: number;
|
|
9
|
+
}
|
|
4
10
|
export interface TRegExp extends TSchema {
|
|
5
11
|
[Kind]: 'RegExp';
|
|
6
12
|
static: `${string}`;
|
|
@@ -9,6 +15,6 @@ export interface TRegExp extends TSchema {
|
|
|
9
15
|
flags: string;
|
|
10
16
|
}
|
|
11
17
|
/** `[JavaScript]` Creates a RegExp type */
|
|
12
|
-
export declare function RegExp(pattern: string, options?:
|
|
18
|
+
export declare function RegExp(pattern: string, options?: RegExpOptions): TRegExp;
|
|
13
19
|
/** `[JavaScript]` Creates a RegExp type */
|
|
14
|
-
export declare function RegExp(regex: RegExp, options?:
|
|
20
|
+
export declare function RegExp(regex: RegExp, options?: RegExpOptions): TRegExp;
|
|
@@ -10,7 +10,7 @@ import { type TInstanceType } from '../instance-type/index';
|
|
|
10
10
|
import { type TIterator } from '../iterator/index';
|
|
11
11
|
import { type TParameters } from '../parameters/index';
|
|
12
12
|
import { type TPromise } from '../promise/index';
|
|
13
|
-
import { type TRegExp } from '../regexp/index';
|
|
13
|
+
import { type TRegExp, RegExpOptions } from '../regexp/index';
|
|
14
14
|
import { type TReturnType } from '../return-type/index';
|
|
15
15
|
import { type TSchema, type SchemaOptions } from '../schema/index';
|
|
16
16
|
import { type TSymbol } from '../symbol/index';
|
|
@@ -42,9 +42,9 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
42
42
|
/** `[JavaScript]` Creates a Promise type */
|
|
43
43
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
44
44
|
/** `[JavaScript]` Creates a RegExp type */
|
|
45
|
-
RegExp(pattern: string, options?:
|
|
45
|
+
RegExp(pattern: string, options?: RegExpOptions): TRegExp;
|
|
46
46
|
/** `[JavaScript]` Creates a RegExp type */
|
|
47
|
-
RegExp(regex: RegExp, options?:
|
|
47
|
+
RegExp(regex: RegExp, options?: RegExpOptions): TRegExp;
|
|
48
48
|
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
49
49
|
ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>;
|
|
50
50
|
/** `[JavaScript]` Creates a Symbol type */
|
|
@@ -176,7 +176,7 @@ class JsonTypeBuilder {
|
|
|
176
176
|
}
|
|
177
177
|
/** `[Json]` Creates a Record type */
|
|
178
178
|
Record(key, schema, options = {}) {
|
|
179
|
-
return (0, index_29.Record)(key, schema);
|
|
179
|
+
return (0, index_29.Record)(key, schema, options);
|
|
180
180
|
}
|
|
181
181
|
/** `[Json]` Creates a Recursive type */
|
|
182
182
|
Recursive(callback, options = {}) {
|
|
@@ -291,6 +291,14 @@ function FromRef(schema, references, value) {
|
|
|
291
291
|
}
|
|
292
292
|
function FromRegExp(schema, references, value) {
|
|
293
293
|
const regex = new RegExp(schema.source, schema.flags);
|
|
294
|
+
if (IsDefined(schema.minLength)) {
|
|
295
|
+
if (!(value.length >= schema.minLength))
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
if (IsDefined(schema.maxLength)) {
|
|
299
|
+
if (!(value.length <= schema.maxLength))
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
294
302
|
return regex.test(value);
|
|
295
303
|
}
|
|
296
304
|
function FromString(schema, references, value) {
|