@sinclair/typebox 0.34.6 → 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.
@@ -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) {
@@ -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.6",
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
  ```