@sinclair/typebox 0.34.5 → 0.34.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.
@@ -247,6 +247,7 @@ function IsSchema(value) {
247
247
  IsBoolean(value) ||
248
248
  IsBigInt(value) ||
249
249
  IsAsyncIterator(value) ||
250
+ IsComputed(value) ||
250
251
  IsConstructor(value) ||
251
252
  IsDate(value) ||
252
253
  IsFunction(value) ||
@@ -199,7 +199,11 @@ function IsBoolean(value) {
199
199
  }
200
200
  /** Returns true if the given value is TComputed */
201
201
  function IsComputed(value) {
202
- return IsKindOf(value, 'Computed') && IsString(value.target) && ValueGuard.IsArray(value.parameters) && value.parameters.every((schema) => IsSchema(schema));
202
+ // prettier-ignore
203
+ return (IsKindOf(value, 'Computed') &&
204
+ ValueGuard.IsString(value.target) &&
205
+ ValueGuard.IsArray(value.parameters) &&
206
+ value.parameters.every((schema) => IsSchema(schema)));
203
207
  }
204
208
  /** Returns true if the given value is TConstructor */
205
209
  function IsConstructor(value) {
@@ -517,6 +521,7 @@ function IsSchema(value) {
517
521
  IsBoolean(value) ||
518
522
  IsBigInt(value) ||
519
523
  IsAsyncIterator(value) ||
524
+ IsComputed(value) ||
520
525
  IsConstructor(value) ||
521
526
  IsDate(value) ||
522
527
  IsFunction(value) ||
@@ -1,9 +1,28 @@
1
1
  import type { TSchema, SchemaOptions } from '../schema/index';
2
2
  import { Kind } from '../symbols/index';
3
+ import { TUnsafe } from '../unsafe/index';
4
+ import { Static } from '../static/index';
3
5
  export interface TRef<Ref extends string = string> extends TSchema {
4
6
  [Kind]: 'Ref';
5
7
  static: unknown;
6
8
  $ref: Ref;
7
9
  }
8
- /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
10
+ export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
11
+ /** `[Json]` Creates a Ref type.*/
9
12
  export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
13
+ /**
14
+ * @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
15
+ * deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
16
+ * can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
17
+ * of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
18
+ *
19
+ * ```typescript
20
+ * const R = Type.Ref(T)
21
+ * ```
22
+ * to
23
+ *
24
+ * ```typescript
25
+ * const R = Type.Unsafe<Static<T>>(T.$id)
26
+ * ```
27
+ */
28
+ export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
@@ -2,9 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Ref = Ref;
5
+ const index_1 = require("../error/index");
5
6
  const type_1 = require("../create/type");
6
- const index_1 = require("../symbols/index");
7
+ const index_2 = require("../symbols/index");
7
8
  /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
8
- function Ref($ref, options) {
9
- return (0, type_1.CreateType)({ [index_1.Kind]: 'Ref', $ref }, options);
9
+ function Ref(...args) {
10
+ const [$ref, options] = typeof args[0] === 'string' ? [args[0], args[1]] : [args[0].$id, args[1]];
11
+ if (typeof $ref !== 'string')
12
+ throw new index_1.TypeBoxError('Ref: $ref must be a string');
13
+ return (0, type_1.CreateType)({ [index_2.Kind]: 'Ref', $ref }, options);
10
14
  }
@@ -29,7 +29,7 @@ import { type TReadonlyWithFlag, type TReadonlyFromMappedResult } from '../reado
29
29
  import { type TReadonlyOptional } from '../readonly-optional/index';
30
30
  import { type TRecordOrObject } from '../record/index';
31
31
  import { type TRecursive, type TThis } from '../recursive/index';
32
- import { type TRef } from '../ref/index';
32
+ import { type TRef, type TRefUnsafe } from '../ref/index';
33
33
  import { type TRequired, type TRequiredFromMappedResult } from '../required/index';
34
34
  import { type TRest } from '../rest/index';
35
35
  import { type TSchema, type SchemaOptions } from '../schema/index';
@@ -142,8 +142,24 @@ export declare class JsonTypeBuilder {
142
142
  Record<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: ObjectOptions): TRecordOrObject<Key, Value>;
143
143
  /** `[Json]` Creates a Recursive type */
144
144
  Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>;
145
- /** `[Json]` Creates a Ref type. */
145
+ /** `[Json]` Creates a Ref type.*/
146
146
  Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
147
+ /**
148
+ * @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
149
+ * deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
150
+ * can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
151
+ * of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
152
+ *
153
+ * ```typescript
154
+ * const R = Type.Ref(T)
155
+ * ```
156
+ * to
157
+ *
158
+ * ```typescript
159
+ * const R = Type.Unsafe<Static<T>>(T.$id)
160
+ * ```
161
+ */
162
+ Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
147
163
  /** `[Json]` Constructs a type where all properties are required */
148
164
  Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
149
165
  /** `[Json]` Constructs a type where all properties are required */
@@ -174,9 +174,9 @@ class JsonTypeBuilder {
174
174
  Recursive(callback, options) {
175
175
  return (0, index_30.Recursive)(callback, options);
176
176
  }
177
- /** `[Json]` Creates a Ref type. */
178
- Ref($ref, options) {
179
- return (0, index_31.Ref)($ref, options);
177
+ /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
178
+ Ref(...args) {
179
+ return (0, index_31.Ref)(args[0], args[1]);
180
180
  }
181
181
  /** `[Json]` Constructs a type where all properties are required */
182
182
  Required(type, options) {
@@ -196,6 +196,7 @@ export function IsSchema(value) {
196
196
  IsBoolean(value) ||
197
197
  IsBigInt(value) ||
198
198
  IsAsyncIterator(value) ||
199
+ IsComputed(value) ||
199
200
  IsConstructor(value) ||
200
201
  IsDate(value) ||
201
202
  IsFunction(value) ||
@@ -145,7 +145,11 @@ export function IsBoolean(value) {
145
145
  }
146
146
  /** Returns true if the given value is TComputed */
147
147
  export function IsComputed(value) {
148
- return IsKindOf(value, 'Computed') && IsString(value.target) && ValueGuard.IsArray(value.parameters) && value.parameters.every((schema) => IsSchema(schema));
148
+ // prettier-ignore
149
+ return (IsKindOf(value, 'Computed') &&
150
+ ValueGuard.IsString(value.target) &&
151
+ ValueGuard.IsArray(value.parameters) &&
152
+ value.parameters.every((schema) => IsSchema(schema)));
149
153
  }
150
154
  /** Returns true if the given value is TConstructor */
151
155
  export function IsConstructor(value) {
@@ -463,6 +467,7 @@ export function IsSchema(value) {
463
467
  IsBoolean(value) ||
464
468
  IsBigInt(value) ||
465
469
  IsAsyncIterator(value) ||
470
+ IsComputed(value) ||
466
471
  IsConstructor(value) ||
467
472
  IsDate(value) ||
468
473
  IsFunction(value) ||
@@ -4,7 +4,7 @@ import { Awaited } from '../awaited/index.mjs';
4
4
  import { AsyncIterator } from '../async-iterator/index.mjs';
5
5
  import { Constructor } from '../constructor/index.mjs';
6
6
  import { Index } from '../indexed/index.mjs';
7
- import { Function } from '../function/index.mjs';
7
+ import { Function as FunctionType } from '../function/index.mjs';
8
8
  import { Intersect } from '../intersect/index.mjs';
9
9
  import { Iterator } from '../iterator/index.mjs';
10
10
  import { KeyOf } from '../keyof/index.mjs';
@@ -93,7 +93,7 @@ function FromConstructor(moduleProperties, parameters, instanceType) {
93
93
  }
94
94
  // prettier-ignore
95
95
  function FromFunction(moduleProperties, parameters, returnType) {
96
- return Function(FromRest(moduleProperties, parameters), FromType(moduleProperties, returnType));
96
+ return FunctionType(FromRest(moduleProperties, parameters), FromType(moduleProperties, returnType));
97
97
  }
98
98
  function FromTuple(moduleProperties, types) {
99
99
  return Tuple(FromRest(moduleProperties, types));
@@ -1,9 +1,28 @@
1
1
  import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
2
  import { Kind } from '../symbols/index.mjs';
3
+ import { TUnsafe } from '../unsafe/index.mjs';
4
+ import { Static } from '../static/index.mjs';
3
5
  export interface TRef<Ref extends string = string> extends TSchema {
4
6
  [Kind]: 'Ref';
5
7
  static: unknown;
6
8
  $ref: Ref;
7
9
  }
8
- /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
10
+ export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
11
+ /** `[Json]` Creates a Ref type.*/
9
12
  export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
13
+ /**
14
+ * @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
15
+ * deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
16
+ * can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
17
+ * of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
18
+ *
19
+ * ```typescript
20
+ * const R = Type.Ref(T)
21
+ * ```
22
+ * to
23
+ *
24
+ * ```typescript
25
+ * const R = Type.Unsafe<Static<T>>(T.$id)
26
+ * ```
27
+ */
28
+ export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
@@ -1,6 +1,10 @@
1
+ import { TypeBoxError } from '../error/index.mjs';
1
2
  import { CreateType } from '../create/type.mjs';
2
3
  import { Kind } from '../symbols/index.mjs';
3
4
  /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
4
- export function Ref($ref, options) {
5
+ export function Ref(...args) {
6
+ const [$ref, options] = typeof args[0] === 'string' ? [args[0], args[1]] : [args[0].$id, args[1]];
7
+ if (typeof $ref !== 'string')
8
+ throw new TypeBoxError('Ref: $ref must be a string');
5
9
  return CreateType({ [Kind]: 'Ref', $ref }, options);
6
10
  }
@@ -29,7 +29,7 @@ import { type TReadonlyWithFlag, type TReadonlyFromMappedResult } from '../reado
29
29
  import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
30
30
  import { type TRecordOrObject } from '../record/index.mjs';
31
31
  import { type TRecursive, type TThis } from '../recursive/index.mjs';
32
- import { type TRef } from '../ref/index.mjs';
32
+ import { type TRef, type TRefUnsafe } from '../ref/index.mjs';
33
33
  import { type TRequired, type TRequiredFromMappedResult } from '../required/index.mjs';
34
34
  import { type TRest } from '../rest/index.mjs';
35
35
  import { type TSchema, type SchemaOptions } from '../schema/index.mjs';
@@ -142,8 +142,24 @@ export declare class JsonTypeBuilder {
142
142
  Record<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: ObjectOptions): TRecordOrObject<Key, Value>;
143
143
  /** `[Json]` Creates a Recursive type */
144
144
  Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>;
145
- /** `[Json]` Creates a Ref type. */
145
+ /** `[Json]` Creates a Ref type.*/
146
146
  Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
147
+ /**
148
+ * @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
149
+ * deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
150
+ * can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
151
+ * of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
152
+ *
153
+ * ```typescript
154
+ * const R = Type.Ref(T)
155
+ * ```
156
+ * to
157
+ *
158
+ * ```typescript
159
+ * const R = Type.Unsafe<Static<T>>(T.$id)
160
+ * ```
161
+ */
162
+ Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
147
163
  /** `[Json]` Constructs a type where all properties are required */
148
164
  Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
149
165
  /** `[Json]` Constructs a type where all properties are required */
@@ -170,9 +170,9 @@ export class JsonTypeBuilder {
170
170
  Recursive(callback, options) {
171
171
  return Recursive(callback, options);
172
172
  }
173
- /** `[Json]` Creates a Ref type. */
174
- Ref($ref, options) {
175
- return Ref($ref, options);
173
+ /** `[Json]` Creates a Ref type. The referenced type must contain a $id */
174
+ Ref(...args) {
175
+ return Ref(args[0], args[1]);
176
176
  }
177
177
  /** `[Json]` Constructs a type where all properties are required */
178
178
  Required(type, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.5",
3
+ "version": "0.34.7",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -337,7 +337,7 @@ The following table lists the supported Json types. These types are fully compat
337
337
  │ }), │ y: number │ required: ['x'], │
338
338
  │ Type.Object({ │ } │ properties: { │
339
339
  │ y: Type.Number() │ │ x: { │
340
- ]) │ │ type: 'number' │
340
+ }) │ │ type: 'number' │
341
341
  │ ]) │ │ } │
342
342
  │ │ │ } │
343
343
  │ │ │ }, { │
@@ -534,15 +534,7 @@ The following table lists the supported Json types. These types are fully compat
534
534
  │ │ │ } │
535
535
  │ │ │ │
536
536
  ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
537
- │ const T = Type.Object({ │ type T = { │ const R = {
538
- │ x: Type.Number(), │ x: number, │ $ref: 'T' │
539
- │ y: Type.Number() │ y: number │ } │
540
- │ }, { $id: 'T' }) | } │ │
541
- │ │ │ │
542
- │ const R = Type.Ref(T) │ type R = T │ │
543
- │ │ │ │
544
- │ │ │ │
545
- │ │ │ │
537
+ │ const R = Type.Ref('T') │ type R = unknown │ const R = { $ref: 'T' }
546
538
  │ │ │ │
547
539
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
548
540
  ```