🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@sinclair/typebox

Package Overview
Dependencies
Maintainers
1
Versions
372
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinclair/typebox - npm Package Compare versions

Comparing version

to
0.20.6

8

package.json
{
"name": "@sinclair/typebox",
"version": "0.20.5",
"version": "0.20.6",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",

@@ -26,3 +26,3 @@ "keywords": [

"devDependencies": {
"@sinclair/hammer": "^0.12.1",
"@sinclair/hammer": "^0.15.7",
"@types/chai": "^4.2.16",

@@ -34,5 +34,5 @@ "@types/mocha": "^8.2.2",

"chai": "^4.3.4",
"mocha": "^8.3.2",
"typescript": "^4.3.5"
"mocha": "^9.1.2",
"typescript": "^4.5.2"
}
}

@@ -62,2 +62,3 @@ <div align='center'>

- [Validation](#Validation)
- [OpenAPI](#OpenAPI)

@@ -311,3 +312,3 @@ <a name="Example"></a>

│ }) │ │ } │
│ ) │ │ } │
│ ) │ │ }, │
│ │ │ required: ['x', 'y'] │

@@ -746,2 +747,45 @@ │ │ │ } │

Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for more information on using this validator.
Please refer to the official AJV [documentation](https://ajv.js.org/guide/getting-started.html) for additional information.
### OpenAPI
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.
```typescript
import { Type, Static, TNull, TLiteral, TUnion, TSchema } from '@sinclair/typebox'
//--------------------------------------------------------------------------------------------
//
// Nullable<T>
//
//--------------------------------------------------------------------------------------------
function Nullable<T extends TSchema>(schema: T): TUnion<[T, TNull]> {
return { ...schema, nullable: true } as any
}
const T = Nullable(Type.String()) // const T = {
// type: 'string',
// nullable: true
// }
type T = Static<typeof T> // type T = string | null
//--------------------------------------------------------------------------------------------
//
// StringUnion<[...]>
//
//--------------------------------------------------------------------------------------------
type IntoStringUnion<T> = {[K in keyof T]: T[K] extends string ? TLiteral<T[K]>: never }
function StringUnion<T extends string[]>(values: [...T]): TUnion<IntoStringUnion<T>> {
return { enum: values } as any
}
const T = StringUnion(['A', 'B', 'C']) // const T = {
// enum: ['A', 'B', 'C']
// }
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
```

@@ -239,8 +239,4 @@ export declare const ReadonlyOptionalModifier: unique symbol;

export declare type StaticLiteral<T extends TValue> = T;
export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...{
[K in keyof T]: Static<T[K]>;
}]) => Static<U>;
export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: [...{
[K in keyof T]: Static<T[K]>;
}]) => Static<U>;
export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: unknown[]) => Static<U>;
export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: unknown[]) => Static<U>;
export declare type StaticPromise<T extends TSchema> = Promise<Static<T>>;

@@ -247,0 +243,0 @@ 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;