Socket
Socket
Sign inDemoInstall

@sinclair/typebox

Package Overview
Dependencies
Maintainers
1
Versions
324
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 0.20.6 to 0.21.0

2

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

@@ -5,0 +5,0 @@ "keywords": [

@@ -450,6 +450,6 @@ <div align='center'>

It can be helpful to organize shared referenced types under a common namespace. The `Type.Box(...)` function can be used to create a shared definition container for related types. The following creates a `Math3D` container and a `Vertex` structure that references types in the container.
It can be helpful to organize shared referenced types under a common namespace. The `Type.Namespace(...)` function can be used to create a shared definition container for related types. The following creates a `Math3D` container and a `Vertex` structure that references types in the container.
```typescript
const Math3D = Type.Box({ // const Math3D = {
const Math3D = Type.Namespace({ // const Math3D = {
Vector4: Type.Object({ // $id: 'Math3D',

@@ -695,3 +695,3 @@ x: Type.Number(), // definitions: {

Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.Box(...)` and registers the box with AJV.
Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.Namespace(...)` and registers the box with AJV.

@@ -705,3 +705,3 @@ ```typescript

const Shared = Type.Box({
const Shared = Type.Namespace({
UserId: Type.String({ format: 'uuid' }),

@@ -708,0 +708,0 @@ Email: Type.String({ format: 'email' })

@@ -66,6 +66,16 @@ export declare const ReadonlyOptionalModifier: unique symbol;

} & CustomOptions;
export declare type TNamespace<T extends TDefinitions> = {
kind: typeof BoxKind;
definitions: T;
} & CustomOptions;
export declare type TDefinitions = {
[key: string]: TSchema;
};
export declare type Infer<T> = {
'_infer': T;
};
export declare type TEnumType = Record<string, string | number>;
export declare type TKey = string | number;
export declare type TValue = string | number | boolean;
export declare type TRecordKey = TString | TNumber | TUnion<TLiteral<string | number>[]>;
export declare type TRecordKey = TString | TNumber | TKeyOf<any>;
export declare type TEnumKey<T = TKey> = {

@@ -75,16 +85,9 @@ type: 'number' | 'string';

};
export declare type TDefinitions = {
[key: string]: TSchema;
};
export declare type TProperties = {
[key: string]: TSchema;
};
export declare type TBox<T extends TDefinitions> = {
kind: typeof BoxKind;
definitions: T;
} & CustomOptions;
export declare type TTuple<T extends TSchema[]> = {
export declare type TTuple<T> = Infer<T> & {
kind: typeof TupleKind;
type: 'array';
items?: [...T];
items?: TSchema[];
additionalItems?: false;

@@ -94,66 +97,66 @@ minItems: number;

} & CustomOptions;
export declare type TObject<T extends TProperties> = {
export declare type TObject<T> = Infer<T> & {
kind: typeof ObjectKind;
type: 'object';
properties: T;
properties: TProperties;
required?: string[];
} & ObjectOptions;
export declare type TUnion<T extends TSchema[]> = {
export declare type TUnion<T> = Infer<T> & {
kind: typeof UnionKind;
anyOf: [...T];
anyOf: TSchema[];
} & CustomOptions;
export declare type TIntersect<T extends TSchema[]> = {
export declare type TIntersect<T> = Infer<T> & {
kind: typeof IntersectKind;
type: 'object';
allOf: [...T];
allOf: TSchema[];
} & IntersectOptions;
export declare type TKeyOf<T extends TKey[]> = {
export declare type TKeyOf<T> = Infer<T> & {
kind: typeof KeyOfKind;
type: 'string';
enum: [...T];
enum: string[];
} & CustomOptions;
export declare type TRecord<K extends TRecordKey, T extends TSchema> = {
export declare type TRecord<T> = Infer<T> & {
kind: typeof RecordKind;
type: 'object';
patternProperties: {
[pattern: string]: T;
[pattern: string]: TSchema;
};
} & ObjectOptions;
export declare type TArray<T extends TSchema> = {
export declare type TArray<T> = Infer<T> & {
kind: typeof ArrayKind;
type: 'array';
items: T;
items: any;
} & ArrayOptions;
export declare type TLiteral<T extends TValue> = {
export declare type TLiteral<T> = Infer<T> & {
kind: typeof LiteralKind;
const: T;
const: TSchema;
} & CustomOptions;
export declare type TEnum<T extends TEnumKey[]> = {
export declare type TEnum<T> = Infer<T> & {
kind: typeof EnumKind;
anyOf: T;
anyOf: TSchema;
} & CustomOptions;
export declare type TString = {
export declare type TString = Infer<string> & {
kind: typeof StringKind;
type: 'string';
} & StringOptions<string>;
export declare type TNumber = {
export declare type TNumber = Infer<number> & {
kind: typeof NumberKind;
type: 'number';
} & NumberOptions;
export declare type TInteger = {
export declare type TInteger = Infer<number> & {
kind: typeof IntegerKind;
type: 'integer';
} & NumberOptions;
export declare type TBoolean = {
export declare type TBoolean = Infer<boolean> & {
kind: typeof BooleanKind;
type: 'boolean';
} & CustomOptions;
export declare type TNull = {
export declare type TNull = Infer<null> & {
kind: typeof NullKind;
type: 'null';
} & CustomOptions;
export declare type TUnknown = {
export declare type TUnknown = Infer<unknown> & {
kind: typeof UnknownKind;
} & CustomOptions;
export declare type TAny = {
export declare type TAny = Infer<any> & {
kind: typeof AnyKind;

@@ -166,37 +169,29 @@ } & CustomOptions;

export declare const VoidKind: unique symbol;
export declare type TConstructor<T extends TSchema[], U extends TSchema> = {
export declare type TConstructor<T> = Infer<T> & {
kind: typeof ConstructorKind;
type: 'constructor';
arguments: readonly [...T];
returns: U;
arguments: TSchema[];
returns: TSchema;
} & CustomOptions;
export declare type TFunction<T extends TSchema[], U extends TSchema> = {
export declare type TFunction<T> = Infer<T> & {
kind: typeof FunctionKind;
type: 'function';
arguments: readonly [...T];
returns: U;
arguments: TSchema[];
returns: TSchema;
} & CustomOptions;
export declare type TPromise<T extends TSchema> = {
export declare type TPromise<T> = Infer<T> & {
kind: typeof PromiseKind;
type: 'promise';
item: T;
item: TSchema;
} & CustomOptions;
export declare type TUndefined = {
export declare type TUndefined = Infer<undefined> & {
kind: typeof UndefinedKind;
type: 'undefined';
} & CustomOptions;
export declare type TVoid = {
export declare type TVoid = Infer<void> & {
kind: typeof VoidKind;
type: 'void';
} & CustomOptions;
export declare type TSchema = TIntersect<any> | TUnion<any> | TTuple<any> | TObject<any> | TKeyOf<any> | TRecord<any, any> | TArray<any> | TEnum<any> | TLiteral<any> | TString | TNumber | TInteger | TBoolean | TNull | TUnknown | TAny | TConstructor<any[], any> | TFunction<any[], any> | TPromise<any> | TUndefined | TVoid;
export declare type TRequired<T extends TProperties> = {
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
};
export declare type TPartial<T extends TProperties> = {
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
};
export declare type TSchema = TIntersect<any> | TUnion<any> | TTuple<any> | TObject<any> | TKeyOf<any> | TRecord<any> | TArray<any> | TEnum<any> | TLiteral<any> | TString | TNumber | TInteger | TBoolean | TNull | TUnknown | TAny | TConstructor<any> | TFunction<any> | TPromise<any> | TUndefined | TVoid;
export declare type UnionToIntersect<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
export declare type ObjectPropertyKeys<T> = T extends TObject<infer U> ? PropertyKeys<U> : never;
export declare type PropertyKeys<T extends TProperties> = keyof T;
export declare type ReadonlyOptionalPropertyKeys<T extends TProperties> = {

@@ -212,11 +207,8 @@ [K in keyof T]: T[K] extends TReadonlyOptional<TSchema> ? K : never;

export declare type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
export declare type ReduceModifiers<T extends object> = {
[K in keyof T]: T[K];
};
export declare type StaticModifiers<T extends TProperties> = {
readonly [K in ReadonlyOptionalPropertyKeys<T>]?: T[K] extends TReadonlyOptional<infer U> ? Static<U> : never;
export declare type StaticProperties<T extends TProperties> = {
readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K]>;
} & {
readonly [K in ReadonlyPropertyKeys<T>]: T[K] extends TReadonly<infer U> ? Static<U> : never;
readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K]>;
} & {
[K in OptionalPropertyKeys<T>]?: T[K] extends TOptional<infer U> ? Static<U> : never;
[K in OptionalPropertyKeys<T>]?: Static<T[K]>;
} & {

@@ -234,87 +226,87 @@ [K in RequiredPropertyKeys<T>]: Static<T[K]>;

};
export declare type StaticObject<T extends TProperties> = ReduceModifiers<StaticModifiers<T>>;
export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ? {
[key: string]: Static<T>;
} : K extends TNumber ? {
[key: number]: Static<T>;
} : K extends TUnion<infer L> ? L extends TLiteral<any>[] ? {
[K in StaticUnion<L>]: Static<T>;
} : never : never;
export declare type StaticObject<T extends TProperties> = StaticProperties<StaticProperties<T>>;
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;
export declare type StaticArray<T extends TSchema> = Array<Static<T>>;
export declare type StaticLiteral<T extends TValue> = T;
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 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 StaticPromise<T extends TSchema> = Promise<Static<T>>;
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;
export declare type Static<T> = T extends TKeyOf<infer I> ? I : T extends TIntersect<infer I> ? I : T extends TUnion<infer I> ? I : T extends TTuple<infer I> ? I : T extends TObject<infer I> ? {
[K in keyof I]: I[K];
} : T extends TRecord<infer I> ? I : T extends TArray<infer I> ? I : T extends TEnum<infer I> ? I : T extends TLiteral<infer I> ? I : T extends TString ? T['_infer'] : T extends TNumber ? T['_infer'] : T extends TInteger ? T['_infer'] : T extends TBoolean ? T['_infer'] : T extends TNull ? T['_infer'] : T extends TUnknown ? T['_infer'] : T extends TAny ? T['_infer'] : T extends TConstructor<infer I> ? I : T extends TFunction<infer I> ? I : T extends TPromise<infer I> ? I : T extends TUndefined ? T['_infer'] : T extends TVoid ? T['_infer'] : never;
export declare class TypeBuilder {
/** `STANDARD` Modifies a schema object property to be `readonly` and `optional`. */
/** `standard` Modifies an object property to be both readonly and optional */
ReadonlyOptional<T extends TSchema>(item: T): TReadonlyOptional<T>;
/** `STANDARD` Modifies a schema object property to be `readonly`. */
/** `standard` Modifies an object property to be readonly */
Readonly<T extends TSchema>(item: T): TReadonly<T>;
/** `STANDARD` Modifies a schema object property to be `optional`. */
/** `standard` Modifies an object property to be optional */
Optional<T extends TSchema>(item: T): TOptional<T>;
/** `STANDARD` Creates a Tuple schema. */
Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<T>;
/** `STANDARD` Creates a `object` schema with the given properties. */
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
/** `STANDARD` Creates an intersection schema. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties`. */
Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<T>;
/** `STANDARD` Creates a Union schema. */
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T>;
/** `STANDARD` Creates an `Array<T>` schema. */
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
/** `STANDARD` Creates an `Enum<T>` schema from a TypeScript `enum` definition. */
Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<TEnumKey<T[keyof T]>[]>;
/** `STANDARD` Creates a literal schema. Supports `string | number | boolean` values. */
Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<T>;
/** `STANDARD` Creates a `string` schema. */
/** `standard` Creates a type type */
Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<StaticTuple<T>>;
/** `standard` Creates an object type with the given properties */
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<StaticProperties<T>>;
/** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<StaticIntersect<T>>;
/** `standard` Creates a union type */
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<StaticUnion<T>>;
/** `standard` Creates an array type */
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<StaticArray<T>>;
/** `standard` Creates an enum type from a TypeScript enum */
Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<StaticEnum<TEnumKey<T[keyof T]>[]>>;
/** `standard` Creates a literal type. Supports string, number and boolean values only */
Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<StaticLiteral<T>>;
/** `standard` Creates a string type */
String<TCustomFormatOption extends string>(options?: StringOptions<StringFormatOption | TCustomFormatOption>): TString;
/** `STANDARD` Creates a `string` schema from a regular expression. */
/** `standard` Creates a string type from a regular expression */
RegEx(regex: RegExp, options?: CustomOptions): TString;
/** `STANDARD` Creates a `number` schema. */
/** `standard` Creates a number type */
Number(options?: NumberOptions): TNumber;
/** `STANDARD` Creates a `integer` schema. */
/** `standard` Creates an integer type */
Integer(options?: NumberOptions): TInteger;
/** `STANDARD` Creates a `boolean` schema. */
/** `standard` Creates a boolean type */
Boolean(options?: CustomOptions): TBoolean;
/** `STANDARD` Creates a `null` schema. */
/** `standard` Creates a null type */
Null(options?: CustomOptions): TNull;
/** `STANDARD` Creates an `unknown` schema. */
/** `standard` Creates an unknown type */
Unknown(options?: CustomOptions): TUnknown;
/** `STANDARD` Creates an `any` schema. */
/** `standard` Creates an any type */
Any(options?: CustomOptions): TAny;
/** `STANDARD` Creates a `keyof` schema. */
KeyOf<T extends TObject<TProperties>>(schema: T, options?: CustomOptions): TKeyOf<ObjectPropertyKeys<T>[]>;
/** `STANDARD` Creates a `Record<Keys, Value>` schema. */
Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<K, T>;
/** `STANDARD` Make all properties in schema object required. */
Required<T extends TObject<TProperties>>(schema: T, options?: ObjectOptions): TObject<TRequired<T['properties']>>;
/** `STANDARD` Make all properties in schema object optional. */
Partial<T extends TObject<TProperties>>(schema: T, options?: ObjectOptions): TObject<TPartial<T['properties']>>;
/** `STANDARD` Picks property keys from the given object schema. */
Pick<T extends TObject<TProperties>, K extends PropertyKeys<T['properties']>[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Pick<T['properties'], K[number]>>;
/** `STANDARD` Omits property keys from the given object schema. */
Omit<T extends TObject<TProperties>, K extends PropertyKeys<T['properties']>[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Omit<T['properties'], K[number]>>;
/** `STANDARD` Omits the `kind` and `modifier` properties from the given schema. */
/** `standard` Creates a keyof type from the given object */
KeyOf<T extends TObject<any>>(schema: T, options?: CustomOptions): TKeyOf<keyof T['_infer']>;
/** `standard` Creates a record type */
Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<StaticRecord<K, T>>;
/** `standard` Makes all properties in the given object type required */
Required<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Required<T['_infer']>>;
/** `standard` Makes all properties in the given object type optional */
Partial<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Partial<T['_infer']>>;
/** `standard` Picks property keys from the given object type */
Pick<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Pick<T['_infer'], K[number]>>;
/** `standard` Omits property keys from the given object type */
Omit<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Omit<T['_infer'], K[number]>>;
/** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
Strict<T extends TSchema>(schema: T, options?: CustomOptions): T;
/** `EXTENDED` Creates a `constructor` schema. */
Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<T, U>;
/** `EXTENDED` Creates a `function` schema. */
Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<T, U>;
/** `EXTENDED` Creates a `Promise<T>` schema. */
Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<T>;
/** `EXTENDED` Creates a `undefined` schema. */
/** `extended` Creates a constructor type */
Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<StaticConstructor<T, U>>;
/** `extended` Creates a function type */
Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<StaticFunction<T, U>>;
/** `extended` Creates a promise type */
Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<StaticPromise<T>>;
/** `extended` Creates a undefined type */
Undefined(options?: CustomOptions): TUndefined;
/** `EXTENDED` Creates a `void` schema. */
/** `extended` Creates a void type */
Void(options?: CustomOptions): TVoid;
/** `EXPERIMENTAL` Creates a recursive type. */
/** `experimental` Creates a recursive type */
Rec<T extends TSchema>(callback: (self: TAny) => T, options?: CustomOptions): T;
/** `EXPERIMENTAL` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
/** `EXPERIMENTAL` Creates a container for schema definitions. */
Box<T extends TDefinitions>(definitions: T, options?: CustomOptions): TBox<T>;
/** `EXPERIMENTAL` References a schema inside a box. The referenced box must specify an `$id`. */
Ref<T extends TBox<TDefinitions>, K extends keyof T['definitions']>(box: T, key: K): T['definitions'][K];
/** `EXPERIMENTAL` References a schema. The referenced schema must specify an `$id`. */
/** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
/** `experimental` Creates a namespace for a set of related types */
Namespace<T extends TDefinitions>(definitions: T, options?: CustomOptions): TNamespace<T>;
/** `experimental` References a type within a namespace. The referenced namespace must specify an `$id` */
Ref<T extends TNamespace<TDefinitions>, K extends keyof T['definitions']>(box: T, key: K): T['definitions'][K];
/** `experimental` References type. The referenced type must specify an `$id` */
Ref<T extends TSchema>(schema: T): T;
}
export declare const Type: TypeBuilder;

@@ -85,15 +85,15 @@ "use strict";

class TypeBuilder {
/** `STANDARD` Modifies a schema object property to be `readonly` and `optional`. */
/** `standard` Modifies an object property to be both readonly and optional */
ReadonlyOptional(item) {
return { ...item, modifier: exports.ReadonlyOptionalModifier };
}
/** `STANDARD` Modifies a schema object property to be `readonly`. */
/** `standard` Modifies an object property to be readonly */
Readonly(item) {
return { ...item, modifier: exports.ReadonlyModifier };
}
/** `STANDARD` Modifies a schema object property to be `optional`. */
/** `standard` Modifies an object property to be optional */
Optional(item) {
return { ...item, modifier: exports.OptionalModifier };
}
/** `STANDARD` Creates a Tuple schema. */
/** `standard` Creates a type type */
Tuple(items, options = {}) {

@@ -107,3 +107,3 @@ const additionalItems = false;

}
/** `STANDARD` Creates a `object` schema with the given properties. */
/** `standard` Creates an object type with the given properties */
Object(properties, options = {}) {

@@ -123,15 +123,15 @@ const property_names = Object.keys(properties);

}
/** `STANDARD` Creates an intersection schema. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties`. */
/** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
Intersect(items, options = {}) {
return { ...options, kind: exports.IntersectKind, type: 'object', allOf: items };
}
/** `STANDARD` Creates a Union schema. */
/** `standard` Creates a union type */
Union(items, options = {}) {
return { ...options, kind: exports.UnionKind, anyOf: items };
}
/** `STANDARD` Creates an `Array<T>` schema. */
/** `standard` Creates an array type */
Array(items, options = {}) {
return { ...options, kind: exports.ArrayKind, type: 'array', items };
}
/** `STANDARD` Creates an `Enum<T>` schema from a TypeScript `enum` definition. */
/** `standard` Creates an enum type from a TypeScript enum */
Enum(item, options = {}) {

@@ -142,39 +142,39 @@ const values = Object.keys(item).filter(key => isNaN(key)).map(key => item[key]);

}
/** `STANDARD` Creates a literal schema. Supports `string | number | boolean` values. */
/** `standard` Creates a literal type. Supports string, number and boolean values only */
Literal(value, options = {}) {
return { ...options, kind: exports.LiteralKind, const: value, type: typeof value };
}
/** `STANDARD` Creates a `string` schema. */
/** `standard` Creates a string type */
String(options = {}) {
return { ...options, kind: exports.StringKind, type: 'string' };
}
/** `STANDARD` Creates a `string` schema from a regular expression. */
/** `standard` Creates a string type from a regular expression */
RegEx(regex, options = {}) {
return this.String({ ...options, pattern: regex.source });
}
/** `STANDARD` Creates a `number` schema. */
/** `standard` Creates a number type */
Number(options = {}) {
return { ...options, kind: exports.NumberKind, type: 'number' };
}
/** `STANDARD` Creates a `integer` schema. */
/** `standard` Creates an integer type */
Integer(options = {}) {
return { ...options, kind: exports.IntegerKind, type: 'integer' };
}
/** `STANDARD` Creates a `boolean` schema. */
/** `standard` Creates a boolean type */
Boolean(options = {}) {
return { ...options, kind: exports.BooleanKind, type: 'boolean' };
}
/** `STANDARD` Creates a `null` schema. */
/** `standard` Creates a null type */
Null(options = {}) {
return { ...options, kind: exports.NullKind, type: 'null' };
}
/** `STANDARD` Creates an `unknown` schema. */
/** `standard` Creates an unknown type */
Unknown(options = {}) {
return { ...options, kind: exports.UnknownKind };
}
/** `STANDARD` Creates an `any` schema. */
/** `standard` Creates an any type */
Any(options = {}) {
return { ...options, kind: exports.AnyKind };
}
/** `STANDARD` Creates a `keyof` schema. */
/** `standard` Creates a keyof type from the given object */
KeyOf(schema, options = {}) {

@@ -184,5 +184,5 @@ const keys = Object.keys(schema.properties);

}
/** `STANDARD` Creates a `Record<Keys, Value>` schema. */
/** `standard` Creates a record type */
Record(key, value, options = {}) {
const pattern = key.kind === exports.UnionKind ? `^${key.anyOf.map((literal) => literal.const).join('|')}$` :
const pattern = key.kind === exports.KeyOfKind ? `^${key.enum.join('|')}$` :
key.kind === exports.NumberKind ? '^(0|[1-9][0-9]*)$' :

@@ -192,3 +192,3 @@ key.pattern ? key.pattern : '^.*$';

}
/** `STANDARD` Make all properties in schema object required. */
/** `standard` Makes all properties in the given object type required */
Required(schema, options = {}) {

@@ -216,3 +216,3 @@ const next = { ...clone(schema), ...options };

}
/** `STANDARD` Make all properties in schema object optional. */
/** `standard` Makes all properties in the given object type optional */
Partial(schema, options = {}) {

@@ -240,3 +240,3 @@ const next = { ...clone(schema), ...options };

}
/** `STANDARD` Picks property keys from the given object schema. */
/** `standard` Picks property keys from the given object type */
Pick(schema, keys, options = {}) {

@@ -251,3 +251,3 @@ const next = { ...clone(schema), ...options };

}
/** `STANDARD` Omits property keys from the given object schema. */
/** `standard` Omits property keys from the given object type */
Omit(schema, keys, options = {}) {

@@ -262,27 +262,27 @@ const next = { ...clone(schema), ...options };

}
/** `STANDARD` Omits the `kind` and `modifier` properties from the given schema. */
/** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
Strict(schema, options = {}) {
return JSON.parse(JSON.stringify({ ...options, ...schema }));
}
/** `EXTENDED` Creates a `constructor` schema. */
/** `extended` Creates a constructor type */
Constructor(args, returns, options = {}) {
return { ...options, kind: exports.ConstructorKind, type: 'constructor', arguments: args, returns };
}
/** `EXTENDED` Creates a `function` schema. */
/** `extended` Creates a function type */
Function(args, returns, options = {}) {
return { ...options, kind: exports.FunctionKind, type: 'function', arguments: args, returns };
}
/** `EXTENDED` Creates a `Promise<T>` schema. */
/** `extended` Creates a promise type */
Promise(item, options = {}) {
return { ...options, type: 'promise', kind: exports.PromiseKind, item };
}
/** `EXTENDED` Creates a `undefined` schema. */
/** `extended` Creates a undefined type */
Undefined(options = {}) {
return { ...options, type: 'undefined', kind: exports.UndefinedKind };
}
/** `EXTENDED` Creates a `void` schema. */
/** `extended` Creates a void type */
Void(options = {}) {
return { ...options, type: 'void', kind: exports.VoidKind };
}
/** `EXPERIMENTAL` Creates a recursive type. */
/** `experimental` Creates a recursive type */
Rec(callback, options = {}) {

@@ -293,3 +293,3 @@ const $id = options.$id || '';

}
/** `EXPERIMENTAL` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
/** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
// public Rec<T extends TProperties>($id: string, callback: (self: TAny) => T, options: ObjectOptions = {}): TObject<T> {

@@ -299,7 +299,6 @@ // const properties = callback({ $recursiveRef: `${$id}` } as any)

// }
/** `EXPERIMENTAL` Creates a container for schema definitions. */
Box(definitions, options = {}) {
/** `experimental` Creates a namespace for a set of related types */
Namespace(definitions, options = {}) {
return { ...options, kind: exports.BoxKind, definitions };
}
/** `EXPERIMENTAL` References a schema. */
Ref(...args) {

@@ -306,0 +305,0 @@ const $id = args[0]['$id'] || '';

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc