@sinclair/typebox 0.21.0 → 0.21.1

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.21.0",
3
+ "version": "0.21.1",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
package/readme.md CHANGED
@@ -774,9 +774,7 @@ type T = Static<typeof T> // type T = string | null
774
774
  //
775
775
  //--------------------------------------------------------------------------------------------
776
776
 
777
- type IntoStringUnion<T> = {[K in keyof T]: T[K] extends string ? TLiteral<T[K]>: never }
778
-
779
- function StringUnion<T extends string[]>(values: [...T]): TUnion<IntoStringUnion<T>> {
777
+ function StringUnion<T extends string[]>(values: [...T]): TUnion<{[K in keyof T]: T[K] }[number]> {
780
778
  return { enum: values } as any
781
779
  }
782
780
 
package/typebox.d.ts CHANGED
@@ -63,20 +63,20 @@ export declare type IntersectOptions = {
63
63
  export declare type ObjectOptions = {
64
64
  additionalProperties?: boolean;
65
65
  } & CustomOptions;
66
+ export declare type TDefinitions = {
67
+ [key: string]: TSchema;
68
+ };
66
69
  export declare type TNamespace<T extends TDefinitions> = {
67
70
  kind: typeof BoxKind;
68
71
  definitions: T;
69
72
  } & CustomOptions;
70
- export declare type TDefinitions = {
71
- [key: string]: TSchema;
72
- };
73
73
  export declare type Infer<T> = {
74
74
  '_infer': T;
75
75
  };
76
76
  export declare type TEnumType = Record<string, string | number>;
77
77
  export declare type TKey = string | number;
78
78
  export declare type TValue = string | number | boolean;
79
- export declare type TRecordKey = TString | TNumber | TKeyOf<any>;
79
+ export declare type TRecordKey = TString | TNumber | TKeyOf<any> | TUnion<string | number>;
80
80
  export declare type TEnumKey<T = TKey> = {
81
81
  type: 'number' | 'string';
82
82
  const: T;
@@ -84,7 +84,7 @@ export declare type TEnumKey<T = TKey> = {
84
84
  export declare type TProperties = {
85
85
  [key: string]: TSchema;
86
86
  };
87
- export declare type TTuple<T> = Infer<T> & {
87
+ export declare type TTuple<I> = Infer<I> & {
88
88
  kind: typeof TupleKind;
89
89
  type: 'array';
90
90
  items?: TSchema[];
@@ -92,43 +92,43 @@ export declare type TTuple<T> = Infer<T> & {
92
92
  minItems: number;
93
93
  maxItems: number;
94
94
  } & CustomOptions;
95
- export declare type TObject<T> = Infer<T> & {
95
+ export declare type TObject<I> = Infer<I> & {
96
96
  kind: typeof ObjectKind;
97
97
  type: 'object';
98
98
  properties: TProperties;
99
99
  required?: string[];
100
100
  } & ObjectOptions;
101
- export declare type TUnion<T> = Infer<T> & {
101
+ export declare type TUnion<I> = Infer<I> & {
102
102
  kind: typeof UnionKind;
103
103
  anyOf: TSchema[];
104
104
  } & CustomOptions;
105
- export declare type TIntersect<T> = Infer<T> & {
105
+ export declare type TIntersect<I> = Infer<I> & {
106
106
  kind: typeof IntersectKind;
107
107
  type: 'object';
108
108
  allOf: TSchema[];
109
109
  } & IntersectOptions;
110
- export declare type TKeyOf<T> = Infer<T> & {
110
+ export declare type TKeyOf<I> = Infer<I> & {
111
111
  kind: typeof KeyOfKind;
112
112
  type: 'string';
113
113
  enum: string[];
114
114
  } & CustomOptions;
115
- export declare type TRecord<T> = Infer<T> & {
115
+ export declare type TRecord<I> = Infer<I> & {
116
116
  kind: typeof RecordKind;
117
117
  type: 'object';
118
118
  patternProperties: {
119
119
  [pattern: string]: TSchema;
120
120
  };
121
121
  } & ObjectOptions;
122
- export declare type TArray<T> = Infer<T> & {
122
+ export declare type TArray<I> = Infer<I> & {
123
123
  kind: typeof ArrayKind;
124
124
  type: 'array';
125
125
  items: any;
126
126
  } & ArrayOptions;
127
- export declare type TLiteral<T> = Infer<T> & {
127
+ export declare type TLiteral<I> = Infer<I> & {
128
128
  kind: typeof LiteralKind;
129
- const: TSchema;
129
+ const: TValue;
130
130
  } & CustomOptions;
131
- export declare type TEnum<T> = Infer<T> & {
131
+ export declare type TEnum<I> = Infer<I> & {
132
132
  kind: typeof EnumKind;
133
133
  anyOf: TSchema;
134
134
  } & CustomOptions;
@@ -219,7 +219,7 @@ export declare type StaticTuple<T extends readonly TSchema[]> = {
219
219
  [K in keyof T]: Static<T[K]>;
220
220
  };
221
221
  export declare type StaticObject<T extends TProperties> = StaticProperties<StaticProperties<T>>;
222
- export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ? Record<string, Static<T>> : K extends TNumber ? Record<number, Static<T>> : K extends TKeyOf<any> ? Record<K['_infer'], Static<T>> : never;
222
+ export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ? Record<string, Static<T>> : K extends TNumber ? Record<number, Static<T>> : K extends TKeyOf<any> ? Record<K['_infer'], Static<T>> : K extends TUnion<any> ? Record<K['_infer'], Static<T>> : never;
223
223
  export declare type StaticArray<T extends TSchema> = Array<Static<T>>;
224
224
  export declare type StaticLiteral<T extends TValue> = T;
225
225
  export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...{
package/typebox.js CHANGED
@@ -55,7 +55,7 @@ exports.NullKind = Symbol('NullKind');
55
55
  exports.UnknownKind = Symbol('UnknownKind');
56
56
  exports.AnyKind = Symbol('AnyKind');
57
57
  // ------------------------------------------------------------------------
58
- // Schema Extended
58
+ // Extended Schema Types
59
59
  // ------------------------------------------------------------------------
60
60
  exports.ConstructorKind = Symbol('ConstructorKind');
61
61
  exports.FunctionKind = Symbol('FunctionKind');
@@ -179,9 +179,15 @@ class TypeBuilder {
179
179
  }
180
180
  /** `standard` Creates a record type */
181
181
  Record(key, value, options = {}) {
182
- const pattern = key.kind === exports.KeyOfKind ? `^${key.enum.join('|')}$` :
183
- key.kind === exports.NumberKind ? '^(0|[1-9][0-9]*)$' :
184
- key.pattern ? key.pattern : '^.*$';
182
+ const pattern = (() => {
183
+ switch (key.kind) {
184
+ case exports.UnionKind: return `^${key.anyOf.map(literal => literal.const).join('|')}$`;
185
+ case exports.KeyOfKind: return `^${key.enum.join('|')}$`;
186
+ case exports.NumberKind: return '^(0|[1-9][0-9]*)$';
187
+ case exports.StringKind: return key.pattern ? key.pattern : '^.*$';
188
+ default: throw Error('Invalid Record Key');
189
+ }
190
+ })();
185
191
  return { ...options, kind: exports.RecordKind, type: 'object', patternProperties: { [pattern]: value } };
186
192
  }
187
193
  /** `standard` Makes all properties in the given object type required */