Socket
Socket
Sign inDemoInstall

@sinclair/typebox

Package Overview
Dependencies
0
Maintainers
1
Versions
306
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.9.2 to 0.9.3

2

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

@@ -5,0 +5,0 @@ "author": "sinclairzx81",

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

- [Types](#Types)
- [More Types](#Intrinsics)
- [Function Types](#Contracts)
- [Functions](#Functions)

@@ -66,3 +66,3 @@ - [Generics](#Generics)

address: Type.String(),
quantity: Type.Range(1, 99),
quantity: Type.Number({ minimum: 1, maximum: 99 }),
option: Type.Union(

@@ -188,7 +188,2 @@ Type.Literal('pizza'),

<tr>
<td>Range</td>
<td><code>const T = Type.Range(20, 30)</code></td>
<td><code>type T = number</code></td>
</tr>
<tr>
<td>Format</td>

@@ -283,7 +278,2 @@ <td><code>const T = Type.Format('date-time')</code></td>

<tr>
<td>Range</td>
<td><code>const T = Type.Range(20, 30)</code></td>
<td><code>{ type: 'number', minimum: 20, maximum: 30 }</code></td>
</tr>
<tr>
<td>Format</td>

@@ -302,10 +292,27 @@ <td><code>const T = Type.Format('date-time')</code></td>

<a name="Intrinsics"></a>
<a name="Contracts"></a>
## More Types
## Function Types
In addition to the JSONSchema functions, TypeBox also provides some non-standard schemas that provide reflectable metadata for function signatures. These functions allow TypeBox to express `function` and `constructor` signatures where the arguments and return types may be JSONSchema.
TypeBox allows function signatures to be composed in a similar way to other types. It uses a custom schema represenation to achieve this. Note, this format is not JSONSchema, rather it uses JSONSchema to encode function `arguments` and `return` types. It also provides additional types; `Type.Constructor()`, `Type.Void()`, `Type.Undefined()`, and `Type.Promise()`.
For more information on their use, see the [Functions](#Functions) and [Generics](#Generics) sections below.
For more information on their using functions, see the [Functions](#Functions) and [Generics](#Generics) sections below.
### Format
The following is an example of how TypeBox encodes function signatures.
```typescript
type T = (a: string, b: number) => boolean
{
"type": "function",
"returns": { "type": "boolean" },
"arguments": [
{"type": "string" },
{"type": "number" },
]
}
```
### TypeBox > TypeScript

@@ -350,3 +357,3 @@

### TypeBox > TypeBox Schema
### TypeBox > JSON Function

@@ -358,3 +365,3 @@ <table>

<th>TypeBox</th>
<th>Metadata</th>
<th>JSON Function</th>
</tr>

@@ -361,0 +368,0 @@ </thead>

@@ -93,3 +93,3 @@ interface TFunction8<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, T7 extends TSchema, U extends TSchema> {

export declare type TConstructor = TConstructor8<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> | TConstructor7<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> | TConstructor6<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> | TConstructor5<TSchema, TSchema, TSchema, TSchema, TSchema, TSchema> | TConstructor4<TSchema, TSchema, TSchema, TSchema, TSchema> | TConstructor3<TSchema, TSchema, TSchema, TSchema> | TConstructor2<TSchema, TSchema, TSchema> | TConstructor1<TSchema, TSchema> | TConstructor0<TSchema>;
export declare type TIntrinsic = TFunction | TConstructor | TPromise<any> | TVoid | TUndefined;
export declare type TContract = TFunction | TConstructor | TPromise<any> | TVoid | TUndefined;
export interface TPromise<T extends TSchema | TVoid | TUndefined> {

@@ -220,63 +220,73 @@ type: 'promise';

export declare type TModifier = TOptional<any> | TReadonly<any>;
declare type SchemaFormat = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
declare type FormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
export interface ArrayOptions {
[prop: string]: any;
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
}
export interface NumberOptions {
[prop: string]: any;
minimum?: number;
exclusiveMinimum?: number;
maximum?: number;
exclusiveMaximum?: number;
multipleOf?: number;
}
export interface StringOptions {
[prop: string]: any;
minLength?: number;
maxLength?: number;
pattern?: string;
format?: FormatOption;
}
export declare type TLiteral = TStringLiteral<string> | TNumberLiteral<number> | TBooleanLiteral<boolean>;
export interface TStringLiteral<T> {
export declare type TStringLiteral<T> = {
type: 'string';
enum: [T];
}
export interface TNumberLiteral<T> {
};
export declare type TNumberLiteral<T> = {
type: 'number';
enum: [T];
}
export interface TBooleanLiteral<T> {
};
export declare type TBooleanLiteral<T> = {
type: 'boolean';
enum: [T];
}
export interface TProperties {
};
export declare type TProperties = {
[key: string]: TSchema | TComposite | TOptional<TSchema | TComposite> | TReadonly<TSchema | TComposite>;
}
export interface TObject<T extends TProperties> {
};
export declare type TObject<T extends TProperties> = {
type: 'object';
properties: T;
required: string[];
}
export interface TMap<T extends TSchema | TComposite> {
};
export declare type TMap<T extends TSchema | TComposite> = {
type: 'object';
additionalProperties: T;
}
export interface TArray<T extends TSchema | TComposite> {
};
export declare type TArray<T extends TSchema | TComposite> = {
type: 'array';
items: T;
}
export interface TNumber {
} & ArrayOptions;
export declare type TNumber = {
type: 'number';
}
export interface TString {
} & NumberOptions;
export declare type TInteger = {
type: 'integer';
};
export declare type TString = {
type: 'string';
}
export interface TBoolean {
} & StringOptions;
export declare type TBoolean = {
type: 'boolean';
}
export interface TPattern {
type: 'string';
pattern: string;
}
export interface TFormat {
type: 'string';
format: SchemaFormat;
}
export interface TRange {
type: 'number';
minimum: number;
maximum: number;
}
export interface TNull {
};
export declare type TNull = {
type: 'null';
}
export interface TAny {
}
export declare type TSchema = TLiteral | TNumber | TBoolean | TString | TObject<any> | TArray<any> | TMap<any> | TPattern | TRange | TFormat | TNull | TAny;
};
export declare type TAny = {};
export declare type TSchema = TLiteral | TNumber | TInteger | TBoolean | TString | TObject<any> | TArray<any> | TMap<any> | TNull | TAny;
declare type StaticFunction<T> = T extends TFunction8<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer U7, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>, arg7: Static<U7>) => Static<R> : T extends TFunction7<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>) => Static<R> : T extends TFunction6<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>) => Static<R> : T extends TFunction5<infer U0, infer U1, infer U2, infer U3, infer U4, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>) => Static<R> : T extends TFunction4<infer U0, infer U1, infer U2, infer U3, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>) => Static<R> : T extends TFunction3<infer U0, infer U1, infer U2, infer R> ? (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>) => Static<R> : T extends TFunction2<infer U0, infer U1, infer R> ? (arg0: Static<U0>, arg1: Static<U1>) => Static<R> : T extends TFunction1<infer U0, infer R> ? (arg0: Static<U0>) => Static<R> : T extends TFunction0<infer R> ? () => Static<R> : never;
declare type StaticConstructor<T> = T extends TConstructor8<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer U7, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>, arg7: Static<U7>) => Static<R> : T extends TConstructor7<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>, arg6: Static<U6>) => Static<R> : T extends TConstructor6<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>, arg5: Static<U5>) => Static<R> : T extends TConstructor5<infer U0, infer U1, infer U2, infer U3, infer U4, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>, arg4: Static<U4>) => Static<R> : T extends TConstructor4<infer U0, infer U1, infer U2, infer U3, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>, arg3: Static<U3>) => Static<R> : T extends TConstructor3<infer U0, infer U1, infer U2, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>, arg2: Static<U2>) => Static<R> : T extends TConstructor2<infer U0, infer U1, infer R> ? new (arg0: Static<U0>, arg1: Static<U1>) => Static<R> : T extends TConstructor1<infer U0, infer R> ? new (arg0: Static<U0>) => Static<R> : T extends TConstructor0<infer R> ? new () => Static<R> : never;
declare type StaticInstrinsic<T extends TIntrinsic> = T extends TFunction ? StaticFunction<T> : T extends TConstructor ? StaticConstructor<T> : T extends TPromise<infer U> ? Promise<Static<U>> : T extends TVoid ? void : T extends TUndefined ? undefined : never;
declare type StaticContract<T extends TContract> = T extends TFunction ? StaticFunction<T> : T extends TConstructor ? StaticConstructor<T> : T extends TPromise<infer U> ? Promise<Static<U>> : T extends TVoid ? void : T extends TUndefined ? undefined : never;
declare type StaticIntersect<T> = T extends TIntersect8<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer U7> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> & StaticSchema<U3> & StaticSchema<U4> & StaticSchema<U5> & StaticSchema<U6> & StaticSchema<U7> : T extends TIntersect7<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> & StaticSchema<U3> & StaticSchema<U4> & StaticSchema<U5> & StaticSchema<U6> : T extends TIntersect6<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> & StaticSchema<U3> & StaticSchema<U4> & StaticSchema<U5> : T extends TIntersect5<infer U0, infer U1, infer U2, infer U3, infer U4> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> & StaticSchema<U3> & StaticSchema<U4> : T extends TIntersect4<infer U0, infer U1, infer U2, infer U3> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> & StaticSchema<U3> : T extends TIntersect3<infer U0, infer U1, infer U2> ? StaticSchema<U0> & StaticSchema<U1> & StaticSchema<U2> : T extends TIntersect2<infer U0, infer U1> ? StaticSchema<U1> & StaticSchema<U0> : T extends TIntersect1<infer U0> ? StaticSchema<U0> : never;

@@ -303,5 +313,5 @@ declare type StaticUnion<T> = T extends TUnion8<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6, infer U7> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> | StaticSchema<U3> | StaticSchema<U4> | StaticSchema<U5> | StaticSchema<U6> | StaticSchema<U7> : T extends TUnion7<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5, infer U6> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> | StaticSchema<U3> | StaticSchema<U4> | StaticSchema<U5> | StaticSchema<U6> : T extends TUnion6<infer U0, infer U1, infer U2, infer U3, infer U4, infer U5> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> | StaticSchema<U3> | StaticSchema<U4> | StaticSchema<U5> : T extends TUnion5<infer U0, infer U1, infer U2, infer U3, infer U4> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> | StaticSchema<U3> | StaticSchema<U4> : T extends TUnion4<infer U0, infer U1, infer U2, infer U3> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> | StaticSchema<U3> : T extends TUnion3<infer U0, infer U1, infer U2> ? StaticSchema<U0> | StaticSchema<U1> | StaticSchema<U2> : T extends TUnion2<infer U0, infer U1> ? StaticSchema<U0> | StaticSchema<U1> : T extends TUnion1<infer U0> ? StaticSchema<U0> : never;

[key: string]: Static<U>;
} : T extends TArray<infer U> ? Array<Static<U>> : T extends TLiteral ? StaticLiteral<T> : T extends TString ? string : T extends TNumber ? number : T extends TBoolean ? boolean : T extends TNull ? null : T extends TAny ? any : T extends TPattern ? string : T extends TFormat ? string : T extends TRange ? number : never;
export declare type TStatic = TComposite | TSchema | TIntrinsic | TModifier;
export declare type Static<T extends TStatic> = T extends TIntrinsic ? StaticInstrinsic<T> : T extends TComposite ? StaticComposite<T> : T extends TSchema ? StaticSchema<T> : never;
} : T extends TArray<infer U> ? Array<Static<U>> : T extends TLiteral ? StaticLiteral<T> : T extends TString ? string : T extends TNumber ? number : T extends TInteger ? number : T extends TBoolean ? boolean : T extends TNull ? null : T extends TAny ? any : never;
export declare type TStatic = TComposite | TSchema | TContract | TModifier;
export declare type Static<T extends TStatic> = T extends TContract ? StaticContract<T> : T extends TComposite ? StaticComposite<T> : T extends TSchema ? StaticSchema<T> : never;
export declare class Type {

@@ -312,12 +322,20 @@ /** Modifies the inner type T into an optional T. */

static Readonly<T extends TSchema | TUnion | TIntersect>(item: T): TReadonly<T>;
/** Creates a StringLiteral for the given value. */
static Literal<T extends string>(value: T): TStringLiteral<T>;
/** Creates a NumberLiteral for the given value. */
static Literal<T extends number>(value: T): TNumberLiteral<T>;
/** Creates a BooleanLiteral for the given value. */
static Literal<T extends boolean>(value: T): TBooleanLiteral<T>;
/** Creates a Object type with the given properties. */
static Object<T extends TProperties>(properties: T): TObject<T>;
/** Creates a Map type of the given type. Keys are indexed with type string. */
static Map<T extends TSchema | TUnion | TIntersect | TTuple>(additionalProperties: T): TMap<T>;
static Map<T extends TSchema | TUnion | TIntersect | TTuple>(item: T): TMap<T>;
/** Creates an Array type of the given argument T. */
static Array<T extends TSchema | TUnion | TIntersect | TTuple>(items: T): TArray<T>;
static Array<T extends TSchema | TUnion | TIntersect | TTuple>(items: T, options?: ArrayOptions): TArray<T>;
/** Creates a String type. */
static String(): TString;
static String(options?: StringOptions): TString;
/** Creates a Number type. */
static Number(): TNumber;
static Number(options?: NumberOptions): TNumber;
/** Creates a Integer type. */
static Integer(options?: NumberOptions): TInteger;
/** Creates a Boolean type. */

@@ -329,8 +347,2 @@ static Boolean(): TBoolean;

static Any(): TAny;
/** Creates a StringLiteral for the given value. */
static Literal<T extends string>(value: T): TStringLiteral<T>;
/** Creates a NumberLiteral for the given value. */
static Literal<T extends number>(value: T): TNumberLiteral<T>;
/** Creates a BooleanLiteral for the given value. */
static Literal<T extends boolean>(value: T): TBooleanLiteral<T>;
/** Creates a Union type for the given arguments. */

@@ -426,11 +438,9 @@ static Union<T0 extends TSchema, T1 extends TSchema, T2 extends TSchema, T3 extends TSchema, T4 extends TSchema, T5 extends TSchema, T6 extends TSchema, T7 extends TSchema>(t0: T0, t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7): TUnion8<T0, T1, T2, T3, T4, T5, T6, T7>;

static Undefined(): TUndefined;
/** Creates a Pattern type that resolves to a string. */
static Pattern(regex: RegExp): TPattern;
/** Creates a Format type that resolves to a string. */
static Format(format: SchemaFormat): TFormat;
/** Creates a Range type that resolves to a number. */
static Range(minimum: number, maximum: number): TRange;
/** Creates a Pattern type to validate UUID-4. */
static Guid(): TPattern;
/** Creates a String type that validates with the given format. Alias for ```Type.String({ format: '...' })``` */
static Format(format: FormatOption): TString;
/** Creates a String type that validates for the given regular expression. Alias for ```Type.String({ pattern: '...' })``` */
static Pattern(regex: RegExp): TString;
/** Creates a String type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` */
static Guid(): TString;
}
export {};

@@ -38,5 +38,4 @@ "use strict";

}
// Type Builder
class Type {
// #region Modifiers
// #region TModifier
/** Modifies the inner type T into an optional T. */

@@ -50,4 +49,10 @@ static Optional(item) {

}
// #endregion
// #region Primitives
/** Creates a Literal for the given value. */
static Literal(value) {
const type = reflect(value);
if (type === 'unknown') {
throw Error('Invalid literal value');
}
return { type, enum: [value] };
}
/** Creates a Object type with the given properties. */

@@ -64,17 +69,21 @@ static Object(properties) {

/** Creates a Map type of the given type. Keys are indexed with type string. */
static Map(additionalProperties) {
return { type: 'object', additionalProperties };
static Map(item) {
return { type: 'object', additionalProperties: item };
}
/** Creates an Array type of the given argument T. */
static Array(items) {
return { type: 'array', items };
static Array(items, options = {}) {
return { type: 'array', items, ...options };
}
/** Creates a String type. */
static String() {
return { type: 'string' };
static String(options = {}) {
return { type: 'string', ...options };
}
/** Creates a Number type. */
static Number() {
return { type: 'number' };
static Number(options = {}) {
return { type: 'number', ...options };
}
/** Creates a Integer type. */
static Integer(options = {}) {
return { type: 'integer', ...options };
}
/** Creates a Boolean type. */

@@ -92,10 +101,2 @@ static Boolean() {

}
/** Creates a Literal for the given value. */
static Literal(value) {
const type = reflect(value);
if (type === 'unknown') {
throw Error('Invalid literal value');
}
return { type, enum: [value] };
}
/** Creates a Union type for the given arguments. */

@@ -138,22 +139,17 @@ static Union(...types) {

// #endregion
// #region Extended
/** Creates a Pattern type that resolves to a string. */
static Pattern(regex) {
return { type: 'string', pattern: regex.source };
}
/** Creates a Format type that resolves to a string. */
// #region Aliases
/** Creates a String type that validates with the given format. Alias for ```Type.String({ format: '...' })``` */
static Format(format) {
return { type: 'string', format };
return this.String({ format });
}
/** Creates a Range type that resolves to a number. */
static Range(minimum, maximum) {
return { type: 'number', minimum, maximum };
/** Creates a String type that validates for the given regular expression. Alias for ```Type.String({ pattern: '...' })``` */
static Pattern(regex) {
return this.String({ pattern: regex.source });
}
// #endregion
// #region Experimental
/** Creates a Pattern type to validate UUID-4. */
/** Creates a String type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` */
static Guid() {
return this.Pattern(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
return this.String({ pattern: regex.source });
}
}
exports.Type = Type;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc