@sinclair/typebox 0.20.5 → 0.20.6

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.20.5",
3
+ "version": "0.20.6",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
@@ -23,14 +23,14 @@
23
23
  "test": "hammer task spec"
24
24
  },
25
25
  "devDependencies": {
26
- "@sinclair/hammer": "^0.12.1",
26
+ "@sinclair/hammer": "^0.15.7",
27
27
  "@types/chai": "^4.2.16",
28
28
  "@types/mocha": "^8.2.2",
29
29
  "@types/node": "^14.14.37",
30
30
  "ajv": "^8.6.2",
31
31
  "ajv-formats": "^2.0.2",
32
32
  "chai": "^4.3.4",
33
- "mocha": "^8.3.2",
34
- "typescript": "^4.3.5"
33
+ "mocha": "^9.1.2",
34
+ "typescript": "^4.5.2"
35
35
  }
36
36
  }
package/readme.md CHANGED
@@ -59,6 +59,7 @@ License MIT
59
59
  - [Extended Types](#Extended-Types)
60
60
  - [Strict](#Strict)
61
61
  - [Validation](#Validation)
62
+ - [OpenAPI](#OpenAPI)
62
63
 
63
64
  <a name="Example"></a>
64
65
 
@@ -307,7 +308,7 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
307
308
  │ Type.Number() │ │ y: { │
308
309
  │ ) │ │ type: 'number' │
309
310
  │ }) │ │ } │
310
- │ ) │ │ }
311
+ │ ) │ │ },
311
312
  │ │ │ required: ['x', 'y'] │
312
313
  │ │ │ } │
313
314
  │ │ │ │
@@ -741,4 +742,47 @@ const ok = ajv.validate(User, {
741
742
 
742
743
  ```
743
744
 
744
- Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for more information on using this validator.
745
+ Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for additional information.
746
+
747
+ ### OpenAPI
748
+
749
+ TypeBox can be used to create schemas for OpenAPI, however users should be aware of the various differences between the JSON Schema and OpenAPI specifications. Two common instances where OpenAPI diverges from the JSON Schema specification is OpenAPI's handling of `string enum` and `nullable`. The following shows how you can use TypeBox to construct these types.
750
+
751
+ ```typescript
752
+ import { Type, Static, TNull, TLiteral, TUnion, TSchema } from '@sinclair/typebox'
753
+
754
+ //--------------------------------------------------------------------------------------------
755
+ //
756
+ // Nullable<T>
757
+ //
758
+ //--------------------------------------------------------------------------------------------
759
+
760
+ function Nullable<T extends TSchema>(schema: T): TUnion<[T, TNull]> {
761
+ return { ...schema, nullable: true } as any
762
+ }
763
+
764
+ const T = Nullable(Type.String()) // const T = {
765
+ // type: 'string',
766
+ // nullable: true
767
+ // }
768
+
769
+ type T = Static<typeof T> // type T = string | null
770
+
771
+ //--------------------------------------------------------------------------------------------
772
+ //
773
+ // StringUnion<[...]>
774
+ //
775
+ //--------------------------------------------------------------------------------------------
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>> {
780
+ return { enum: values } as any
781
+ }
782
+
783
+ const T = StringUnion(['A', 'B', 'C']) // const T = {
784
+ // enum: ['A', 'B', 'C']
785
+ // }
786
+
787
+ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
788
+ ```
package/typebox.d.ts CHANGED
@@ -236,12 +236,8 @@ export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K ex
236
236
  } : never : never;
237
237
  export declare type StaticArray<T extends TSchema> = Array<Static<T>>;
238
238
  export declare type StaticLiteral<T extends TValue> = T;
239
- export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...{
240
- [K in keyof T]: Static<T[K]>;
241
- }]) => Static<U>;
242
- export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: [...{
243
- [K in keyof T]: Static<T[K]>;
244
- }]) => Static<U>;
239
+ export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: unknown[]) => Static<U>;
240
+ export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: unknown[]) => Static<U>;
245
241
  export declare type StaticPromise<T extends TSchema> = Promise<Static<T>>;
246
242
  export declare type Static<T> = T extends TKeyOf<infer U> ? StaticKeyOf<U> : T extends TIntersect<infer U> ? StaticIntersect<U> : T extends TUnion<infer U> ? StaticUnion<U> : T extends TTuple<infer U> ? StaticTuple<U> : T extends TObject<infer U> ? StaticObject<U> : T extends TRecord<infer K, infer U> ? StaticRecord<K, U> : T extends TArray<infer U> ? StaticArray<U> : T extends TEnum<infer U> ? StaticEnum<U> : T extends TLiteral<infer U> ? StaticLiteral<U> : T extends TString ? string : T extends TNumber ? number : T extends TInteger ? number : T extends TBoolean ? boolean : T extends TNull ? null : T extends TUnknown ? unknown : T extends TAny ? any : T extends TConstructor<infer U, infer R> ? StaticConstructor<U, R> : T extends TFunction<infer U, infer R> ? StaticFunction<U, R> : T extends TPromise<infer U> ? StaticPromise<U> : T extends TUndefined ? undefined : T extends TVoid ? void : never;
247
243
  export declare class TypeBuilder {