@sinclair/typebox
Advanced tools
Comparing version 0.9.9 to 0.9.10
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.9.9", | ||
"version": "0.9.10", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
"keywords": [ | ||
"json-schema", | ||
"typescript", | ||
"static-types", | ||
"runtime-typechecking" | ||
], | ||
"author": "sinclairzx81", | ||
@@ -25,3 +31,3 @@ "license": "MIT", | ||
"chai": "^4.1.2", | ||
"mocha": "^5.2.0", | ||
"mocha": "^7.1.1", | ||
"smoke-task": "^1.1.2", | ||
@@ -28,0 +34,0 @@ "typescript": "^3.8.3", |
@@ -8,3 +8,3 @@ <div align='center'> | ||
[![npm version](https://badge.fury.io/js/%40sinclair%2Ftypebox.svg)](https://badge.fury.io/js/%40sinclair%2Ftypebox) | ||
[![Build Status](https://travis-ci.org/sinclairzx81/typebox.svg?branch=master)](https://travis-ci.org/sinclairzx81/TypeBox) | ||
[![GitHub CI](https://github.com/sinclairzx81/typebox/workflows/GitHub%20CI/badge.svg)](https://github.com/sinclairzx81/typebox/actions) | ||
@@ -309,2 +309,29 @@ <img src='./doc/example.gif'></img> | ||
### Enums | ||
It is possible to define TypeScript enums and use them as part of your TypeBox schema. | ||
Both number and string-valued enums are supported. | ||
```ts | ||
enum Color { | ||
Red = 'red', | ||
Blue = 'blue' | ||
} | ||
const T = Type.Enum(Color); // -> json-schema: `{ enum: ['red','green'] }` | ||
``` | ||
Note that the generated json-schema will only permit the *values* of the enum, not its *keys*. | ||
In TypeScript, if you omit the *value* for an enum option, TypeScript will implicitly assign the option a numeric value. | ||
E.g.: | ||
```ts | ||
enum Color { | ||
Red, // implicitly gets value `0` | ||
Blue // implicitly gets value `1` | ||
} | ||
const T = Type.Enum(Color); // -> json-schema: `{ enum: [0, 1] }` | ||
``` | ||
### User Defined Schema Properties | ||
@@ -311,0 +338,0 @@ |
@@ -225,3 +225,3 @@ export interface UserDefinedOptions { | ||
export declare type TModifier = TOptional<any> | TReadonly<any> | TReadonlyOptional<any>; | ||
export 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 declare type FormatOption = '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'; | ||
export declare type ArrayOptions = { | ||
@@ -239,2 +239,5 @@ minItems?: number; | ||
} & UserDefinedOptions; | ||
/** Augmentation support for UserDefinedOptions. Used specifically for adding custom string formats. */ | ||
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; | ||
declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true; | ||
export declare type StringOptions = { | ||
@@ -244,4 +247,4 @@ minLength?: number; | ||
pattern?: string; | ||
format?: FormatOption; | ||
} & UserDefinedOptions; | ||
format?: IsUnion<UserDefinedOptions['format']> extends true ? UserDefinedOptions['format'] | FormatOption : FormatOption; | ||
}; | ||
export declare type TLiteral = TStringLiteral<string> | TNumberLiteral<number> | TBooleanLiteral<boolean>; | ||
@@ -276,2 +279,5 @@ export declare type TStringLiteral<T> = { | ||
} & ArrayOptions; | ||
export declare type TEnum<T> = { | ||
enum: T[keyof T][]; | ||
}; | ||
export declare type TNumber = { | ||
@@ -293,3 +299,3 @@ type: 'number'; | ||
export declare type TAny = {} & UserDefinedOptions; | ||
export declare type TSchema = TLiteral | TNumber | TInteger | TBoolean | TString | TObject<any> | TArray<any> | TMap<any> | TNull | TAny; | ||
export declare type TSchema = TLiteral | TNumber | TInteger | TBoolean | TString | TObject<any> | TArray<any> | TEnum<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; | ||
@@ -327,3 +333,3 @@ 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; | ||
[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 TInteger ? number : T extends TBoolean ? boolean : T extends TNull ? null : T extends TAny ? any : never; | ||
} : T extends TArray<infer U> ? Array<Static<U>> : T extends TEnum<infer U> ? 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; | ||
@@ -440,2 +446,4 @@ export declare type Static<T extends TStatic> = T extends TContract ? StaticContract<T> : T extends TComposite ? StaticComposite<T> : T extends TSchema ? StaticSchema<T> : never; | ||
static Array<T extends TSchema | TUnion | TIntersect | TTuple>(items: T, options?: ArrayOptions): TArray<T>; | ||
/** Creates an `Enum<T>` from an existing TypeScript enum definition. */ | ||
static Enum<T extends Record<string, string | number>>(item: T, options?: UserDefinedOptions): TEnum<T>; | ||
/** Creates a `string` type. */ | ||
@@ -455,5 +463,9 @@ static String(options?: StringOptions): TString; | ||
static Pattern(regex: RegExp): TString; | ||
/** Creates a `string` type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` */ | ||
/** | ||
* Deprecated: Use `Type.String({ format: 'uuid' })` | ||
* | ||
* Creates a `string` type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` | ||
*/ | ||
static Guid(): TString; | ||
} | ||
export {}; |
@@ -117,2 +117,10 @@ "use strict"; | ||
} | ||
/** Creates an `Enum<T>` from an existing TypeScript enum definition. */ | ||
static Enum(item, options) { | ||
// We explicitly want to ignore reverse-lookup entries for number enums hence we are | ||
// getting only keys which are non-numeric and retrieve their value. Credits to | ||
// https://github.com/UselessPickles/ts-enum-util (Jeff Lau) for inspiration. | ||
const values = Object.keys(item).filter(key => isNaN(key)).map(key => item[key]); | ||
return { ...options, enum: values }; | ||
} | ||
/** Creates a `string` type. */ | ||
@@ -148,3 +156,7 @@ static String(options = {}) { | ||
} | ||
/** Creates a `string` type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` */ | ||
/** | ||
* Deprecated: Use `Type.String({ format: 'uuid' })` | ||
* | ||
* Creates a `string` type that validate a Guid. Alias for ```Type.String({ pattern: '...' })``` | ||
*/ | ||
static Guid() { | ||
@@ -151,0 +163,0 @@ 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}$/; |
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
66322
650
594