@sinclair/typebox 0.27.5 → 0.27.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.27.5",
3
+ "version": "0.27.7",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -402,7 +402,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
402
402
  ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
403
403
  │ const T = Type.Record( │ type T = Record< │ const T = { │
404
404
  │ Type.String(), │ string, │ type: 'object', │
405
- │ Type.Number() │ number, │ patternProperties: { │
405
+ │ Type.Number() │ number │ patternProperties: { │
406
406
  │ ) │ > │ '^.*$': { │
407
407
  │ │ │ type: 'number' │
408
408
  │ │ │ } │
package/typebox.d.ts CHANGED
@@ -183,7 +183,7 @@ export type TKeyOfTuple<T extends TSchema> = {
183
183
  } extends infer U ? UnionToTuple<Exclude<{
184
184
  [K in keyof U]: U[K];
185
185
  }[keyof U], undefined>> : never;
186
- export type TKeyOf<T extends TSchema = TSchema> = (T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : T extends TRecord<infer K> ? [K] : [
186
+ export type TKeyOf<T extends TSchema = TSchema> = (T extends TRecursive<infer S> ? TKeyOfTuple<S> : T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : T extends TRecord<infer K> ? [K] : [
187
187
  ]) extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
188
188
  export type TLiteralValue = string | number | boolean;
189
189
  export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
@@ -248,7 +248,7 @@ export type TOmitArray<T extends TSchema[], K extends keyof any> = Assert<{
248
248
  [K2 in keyof T]: TOmit<Assert<T[K2], TSchema>, K>;
249
249
  }, TSchema[]>;
250
250
  export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<Assert<Omit<T, K>, TProperties>>;
251
- export type TOmit<T extends TSchema, K extends keyof any> = T extends TComposite<infer S> ? TComposite<TOmitArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
251
+ export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TComposite<infer S> ? TComposite<TOmitArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
252
252
  export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
253
253
  export type TPartialObjectArray<T extends TObject[]> = Assert<{
254
254
  [K in keyof T]: TPartial<Assert<T[K], TObject>>;
@@ -259,14 +259,14 @@ export type TPartialArray<T extends TSchema[]> = Assert<{
259
259
  export type TPartialProperties<T extends TProperties> = Evaluate<Assert<{
260
260
  [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
261
261
  }, TProperties>>;
262
- export type TPartial<T extends TSchema> = T extends TComposite<infer S> ? TComposite<TPartialArray<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
262
+ export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TComposite<infer S> ? TComposite<TPartialArray<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
263
263
  export type TPickArray<T extends TSchema[], K extends keyof any> = {
264
264
  [K2 in keyof T]: TPick<Assert<T[K2], TSchema>, K>;
265
265
  };
266
266
  export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
267
267
  [K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
268
268
  }) : never;
269
- export type TPick<T extends TSchema, K extends keyof any> = T extends TComposite<infer S> ? TComposite<TPickArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
269
+ export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TComposite<infer S> ? TComposite<TPickArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
270
270
  export interface TPromise<T extends TSchema = TSchema> extends TSchema {
271
271
  [Kind]: 'Promise';
272
272
  static: Promise<Static<T, this['params']>>;
@@ -303,6 +303,7 @@ export interface TThis extends TSchema {
303
303
  }
304
304
  export type TRecursiveReduce<T extends TSchema> = Static<T, [TRecursiveReduce<T>]>;
305
305
  export interface TRecursive<T extends TSchema> extends TSchema {
306
+ [Hint]: 'Recursive';
306
307
  static: TRecursiveReduce<T>;
307
308
  }
308
309
  export interface TRef<T extends TSchema = TSchema> extends TSchema {
@@ -317,7 +318,7 @@ export type TRequiredArray<T extends TSchema[]> = Assert<{
317
318
  export type TRequiredProperties<T extends TProperties> = Evaluate<Assert<{
318
319
  [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
319
320
  }, TProperties>>;
320
- export type TRequired<T extends TSchema> = T extends TComposite<infer S> ? TComposite<TRequiredArray<S>> : T extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : T extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T;
321
+ export type TRequired<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TRequired<S>> : T extends TComposite<infer S> ? TComposite<TRequiredArray<S>> : T extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : T extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : T extends TObject<infer S> ? TObject<TRequiredProperties<S>> : T;
321
322
  export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
322
323
  export interface StringOptions<Format extends string> extends SchemaOptions {
323
324
  minLength?: number;
package/typebox.js CHANGED
@@ -2049,7 +2049,7 @@ class StandardTypeBuilder extends TypeBuilder {
2049
2049
  options.$id = `T${TypeOrdinal++}`;
2050
2050
  const thisType = callback({ [exports.Kind]: 'This', $ref: `${options.$id}` });
2051
2051
  thisType.$id = options.$id;
2052
- return this.Create({ ...options, ...thisType });
2052
+ return this.Create({ ...options, [exports.Hint]: 'Recursive', ...thisType });
2053
2053
  }
2054
2054
  /** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
2055
2055
  Ref(schema, options = {}) {
package/value/create.js CHANGED
@@ -49,7 +49,7 @@ class ValueCreateNeverTypeError extends Error {
49
49
  exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
50
50
  class ValueCreateIntersectTypeError extends Error {
51
51
  constructor(schema) {
52
- super('ValueCreate: Can only create values for intersected objects and non-varying primitive types. Consider using a default value.');
52
+ super('ValueCreate: Intersect produced invalid value. Consider using a default value.');
53
53
  this.schema = schema;
54
54
  }
55
55
  }
@@ -179,7 +179,13 @@ var ValueCreate;
179
179
  return schema.default;
180
180
  }
181
181
  else {
182
- const value = schema.type === 'object' ? schema.allOf.reduce((acc, schema) => ({ ...acc, ...Visit(schema, references) }), {}) : schema.allOf.reduce((_, schema) => Visit(schema, references), undefined);
182
+ // Note: The best we can do here is attempt to instance each sub type and apply through object assign. For non-object
183
+ // sub types, we just escape the assignment and just return the value. In the latter case, this is typically going to
184
+ // be a consequence of an illogical intersection.
185
+ const value = schema.allOf.reduce((acc, schema) => {
186
+ const next = Visit(schema, references);
187
+ return typeof next === 'object' ? { ...acc, ...next } : next;
188
+ }, {});
183
189
  if (!check_1.ValueCheck.Check(schema, references, value))
184
190
  throw new ValueCreateIntersectTypeError(schema);
185
191
  return value;