@sinclair/typebox 0.32.12 → 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.
@@ -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);
@@ -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';
@@ -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) {
@@ -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?: SchemaOptions): TRegExp;
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?: SchemaOptions): TRegExp;
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?: SchemaOptions): TRegExp;
45
+ RegExp(pattern: string, options?: RegExpOptions): TRegExp;
46
46
  /** `[JavaScript]` Creates a RegExp type */
47
- RegExp(regex: RegExp, options?: SchemaOptions): TRegExp;
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 */
@@ -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);
@@ -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';
@@ -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 */
@@ -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?: SchemaOptions): TRegExp;
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?: SchemaOptions): TRegExp;
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?: SchemaOptions): TRegExp;
45
+ RegExp(pattern: string, options?: RegExpOptions): TRegExp;
46
46
  /** `[JavaScript]` Creates a RegExp type */
47
- RegExp(regex: RegExp, options?: SchemaOptions): TRegExp;
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 */
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.12",
3
+ "version": "0.32.13",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",