Skip to content

Latest commit

 

History

History
925 lines (577 loc) · 21.7 KB

modules.md

File metadata and controls

925 lines (577 loc) · 21.7 KB

tguard / Exports

tguard

Table of contents

Classes

Type aliases

Variables

Functions

Type aliases

GuardedType

Ƭ GuardedType<C>: C extends Guard<infer T> ? T : unknown

Infers the type, that the given Guard guards.

Type parameters

Name Type
C extends Guard<unknown>

Defined in

src/Guard.ts:22


ObjectSchema

Ƭ ObjectSchema: Object

Index signature

▪ [fieldName: string]: ObjectSchema | Guard<unknown>

Defined in

src/guards/TObject/types.ts:16

Variables

TAny

Const TAny: Guard<any>

Guard that accepts any value.

guard.name: "any"

Defined in

src/guards/TAny/index.ts:8


TAnyObject

Const TAnyObject: Guard<Object>

Primitive guard that accepts all objects. Does not accept null.

guard.name: "object"

Defined in

src/guards/TAnyObject/index.ts:9


TBigInt

Const TBigInt: Guard<BigInt>

Primitive guard that only accepts bigint values.

guard.name: "bigint"

Defined in

src/guards/TBigInt/index.ts:8


TBoolean

Const TBoolean: Guard<boolean>

Primitive guard that only accepts boolean values.

guard.name: "boolean"

Defined in

src/guards/TBoolean/index.ts:8


TFunction

Const TFunction: Guard<Function>

Primitive guard that only accepts function values.

guard.name: "function"

Defined in

src/guards/TFunction/index.ts:8


TInteger

Const TInteger: Guard<number>

Guard that accepts whole numbers.

guard.name: "integer"

example

TInteger.isValid("15"); // false
TInteger.isValid(15.223); // false
TInteger.isValid(15); // true

Defined in

src/guards/TInteger/index.ts:15


TIntegerAsString

Const TIntegerAsString: Guard<string>

Guard that accepts strings, which can be parsed as a valid integer.

guard.name: "integer(as a string)"

example

TIntegerAsString.isValid("15.223"); // false
TIntegerAsString.isValid(15); // false
TIntegerAsString.isValid("15"); // true

Defined in

src/guards/TIntegerAsString/index.ts:16


TNull

Const TNull: Guard<null>

Guard that only accepts null.

guard.name: "null"

Defined in

src/guards/TNull/index.ts:8


TNumber

Const TNumber: Guard<number>

Primitive guard that only accepts numbers. Not accepts NaN.

guard.name: "number"

Defined in

src/guards/TNumber/index.ts:9


TNumberAsString

Const TNumberAsString: Guard<string>

Guard that accepts strings, which represents a valid number.

guard.name: "number(as a string)"

example

TNumberAsString.isValid("abcd"); // false
TNumberAsString.isValid(15.223); // false
TNumberAsString.isValid("15.223"); // true

Defined in

src/guards/TNumberAsString/index.ts:15


TString

Const TString: Guard<string>

Primitive guard that only accepts string values.

guard.name: "string"

Defined in

src/guards/TString/index.ts:8


TStringEmail

Const TStringEmail: Guard<string>

A Guard which validates if a string is a valid email.

guard.name: "string(email)"

example

TStringEmail.isValid("1234"); // false
TStringEmail.isValid("foo@bar.com"); // true
TStringEmail.name === "string(email)"; // true

Defined in

src/guards/TStringEmail/index.ts:16


TStringISODate

Const TStringISODate: Guard<string>

A Guard which validates if a string is a valid ISO date string.

guard.name: "string(date)"

example

TStringISODate.isValid("1234"); // false
TStringISODate.isValid("2022-03-06T22:01:41.160Z"); // true
TStringISODate.name === "string(date)"; // true

Defined in

src/guards/TStringISODate/index.ts:16


TStringJSON

Const TStringJSON: Guard<string>

A Guard which validates if a string is a valid JSON.

guard.name: "string(JSON)"

example

TStringJSON.isValid("1234"); // false
TStringJSON.isValid("{\"foo\": 2}"); // true
TStringJSON.name === "string(JSON)"; // true

Defined in

src/guards/TStringJSON/index.ts:16


TStringJWT

Const TStringJWT: Guard<string>

A Guard which validates if a string is a valid JSON Web Token.

guard.name: "string(JWT)"

example

TStringJWT.isValid("1234"); // false
TStringJWT.isValid("something.fooo.bar"); // true
TStringJWT.name === "string(JSON)"; // true

Defined in

src/guards/TStringJWT/index.ts:16


TStringMIMEType

Const TStringMIMEType: Guard<string>

A Guard which validates if a string is a valid MIME type.

guard.name: "string(MIME type)"

example

TStringMIMEType.isValid("foobar"); // false
TStringMIMEType.isValid("application/json"); // true
TStringMIMEType.name === "string(MIME type)"; // true

Defined in

src/guards/TStringMIMEType/index.ts:16


TStringPhoneNumber

Const TStringPhoneNumber: Guard<string>

A Guard which validates if a string is a valid phone number. (all locale formats are accepted)

guard.name: "string(phone number)"

example

TStringPhoneNumber.isValid("foobar"); // false
TStringPhoneNumber.isValid("061555555"); // true
TStringPhoneNumber.name === "string(phone number)"; // true

Defined in

src/guards/TStringPhoneNumber/index.ts:17


TStringSemVer

Const TStringSemVer: Guard<string>

A Guard which checks if the string follows the Semantic Versioning Specification (SemVer).

guard.name: "string(SemVer)"

example

TStringSemVer.isValid("foobar"); // false
TStringSemVer.isValid("1.0.4"); // true
TStringSemVer.name === "string(SemVer)"; // true

Defined in

src/guards/TStringSemVer/index.ts:16


TStringURL

Const TStringURL: Guard<string>

A Guard which checks if the string is a valid URL.

guard.name: "string(URL)"

example

TStringURL.isValid("foobar"); // false
TStringURL.isValid("foobar.com"); // true
TStringURL.name === "string(URL)"; // true

Defined in

src/guards/TStringURL/index.ts:16


TStringUUID

Const TStringUUID: Guard<string>

Checks if the string is a valid UUID v4.

returns A Guard which checks if the string is a valid v4 UUID.

guard.name: "string(UUID)"

example

TStringUUID.isValid("foobar"); // false
TStringUUID.isValid("936a0dd4-cf7f-497d-a0cd-7c891416c719"); // true
TStringUUID.name === "string(UUID)"; // true

Defined in

src/guards/TStringUUID/index.ts:19


TUndefined

Const TUndefined: Guard<undefined>

Primitive guard that only accepts undefined values.

guard.name: "undefined"

Defined in

src/guards/TUndefined/index.ts:8

Functions

TAnd

TAnd<A, B, T>(guardA, guardB, ...others): Guard<A & B & UnionToIntersection<GuardedType<ArrayType<T>>>>

Validates if criterias of two (or more) types are all met.

guard.name: "<typeA.name> & <typeB.name>"

Type parameters

Name Type
A A
B B
T extends Guard<unknown>[]

Parameters

Name Type
guardA Guard<A>
guardB Guard<B>
...others T

Returns

Guard<A & B & UnionToIntersection<GuardedType<ArrayType<T>>>>

A Guard that is similar in concept as the & operator in TypeScript. Accepts a value when it was accepted by all guardA and guardB, and others.

Defined in

src/guards/TAnd/index.ts:14


TArray

TArray<T>(guard, options?): Guard<T[]>

Validates an array of elements.

guard.name: "<type>[](minLength:<minLength>,maxLength:<maxLength>)"

example

const TNumbers = TArray(TNumber);
TNumbers.isValid([1, 2, 3]); // true
TNumbers.isValid([1, 2, "3"]); // false
TNumbers.name === "number[]"; // true

Type parameters

Name
T

Parameters

Name Type Description
guard Guard<T> The guard, which validates the elements of the array.
options? Object -
options.maxLength? number The array can't be longer than this.
options.minLength? number The array must be at least this long.

Returns

Guard<T[]>

A Guard that checks if the given value is an array of the given type.

Defined in

src/guards/TArray/index.ts:24


TConstant

TConstant<T>(constant): Guard<T>

Validates equality to a literal value.

example

TConstant("foo").isValid("foobar"); // false
TConstant("foo").isValid("bar"); // false
TConstant("foo").isValid("foo"); // true

TConstant("foo").name === 'constant("foo")'; // true
TConstant(2).name === 'constant(2)'; // true

Type parameters

Name Type
T extends string | number | boolean | BigInt

Parameters

Name Type Description
constant T The literal to compare values against. Only can be a string, number, boolean or bigint.

Returns

Guard<T>

A Guard which checks if the given value is equals to the constant literal.

guard.name: "constant(<constant.name>)"

Defined in

src/guards/TConstant/index.ts:26


TNot

TNot<T>(guard): Guard<Exclude<any, T>>

Negates a Guard.

example

TNot(TNumber).isValid(1); // false
TNot(TNumber).isValid("foo"); // true
TNot(TNumber).name === "!number"; // true

Type parameters

Name
T

Parameters

Name Type Description
guard Guard<T> The guard, which will be negated.

Returns

Guard<Exclude<any, T>>

A Guard that accepts a value when it was not accepted by the given guard.

guard.name: "!<type>"

Defined in

src/guards/TNot/index.ts:21


TObject

TObject<T>(schema): Guard<SchemaType<T>>

It will validate that the given value is matching the object schema.

guard.name: JSON representation of the object

example

const TUser = TObject({
  id: TInteger,
  name: TString,
  cart: {
    mangos: TInteger,
    avocados: TInteger,
  },
});

TUser.isValid({id: 1, name: "John" cart: {apples: 1}}) // false
TUser.isValid({id: 1, name: "John" cart: {mangos: 1, avocados: 2}}) // true
TUser.name === '{"id": "string(UUID)", "name": "string" cart: {"mangos": "integer", "avocados": "integeer"}}' // true

Type parameters

Name Type
T extends ObjectSchema

Parameters

Name Type Description
schema T Is a tree of guards. (Just a normal JS object, but with Guard values)

Returns

Guard<SchemaType<T>>

A Guard.

Defined in

src/guards/TObject/index.ts:31


TObjectOfShape

TObjectOfShape<T>(shape): Guard<{ [key: string]: T; }>

Validates the shape of an object.

example

const guard = TObjectShape({
 keys: TString,
 values: TNumber,
});

guard.isValid({
 avocado: 2,
 orange: 5,
}); // true

guard.isValid({
 avocado: "green",
 orange: 5,
}); // false

guard.name === "{ [string]: number }"; // true

Type parameters

Name
T

Parameters

Name Type Description
shape Object The guards, which will validate the keys and values of the given object.
shape.keys Guard<string> -
shape.values Guard<T> -

Returns

Guard<{ [key: string]: T; }>

A Guard that checks if the given value matches the provided object shape.

Accpets not-null objects, where all keys and values are accepted by the given shape guards. Similar in concept as TypeScript's {[keys: string]: number} type annotations.

guard.name: "{ [<keyType>]: <valueType> }"

Defined in

src/guards/TObjectOfShape/index.ts:39


TOr

TOr<A, B, T>(guardA, guardB, ...others): Guard<A | B | GuardedType<ArrayType<T>>>

Validates if at least one type criteria is met.

example

const guard = TOr(TNumber, TString);
guard.isValid(1); // true
guard.isValid("foo"); // true
guard.isValid(true); // false
guard.name === "number | string"; // true

Type parameters

Name Type
A A
B B
T extends Guard<unknown>[]

Parameters

Name Type
guardA Guard<A>
guardB Guard<B>
...others T

Returns

Guard<A | B | GuardedType<ArrayType<T>>>

A Guard that is similar in concept as the | operator in TypeScript. Accepts a value when it was accepted by at least one of the guards.

guard.name: "<typeA> | <typeB>"

Defined in

src/guards/TOr/index.ts:22


TStringBase64

TStringBase64(options): Guard<string>

Validates if a string is a base64 encoded data.

example

TStringBase64({ urlSafe: true }).isValid("foobar"); // false
TStringBase64({ urlSafe: true }).isValid("c29tZXRoaW5n"); // true
TStringBase64({ urlSafe: true }).name === "string(base64URL)"; // true

Parameters

Name Type Description
options Object -
options.urlSafe boolean If set to true, it will check if the string is bas64URL encoded

Returns

Guard<string>

A Guard that accepts only strings that are base64 encoded.

guard.name: "string(base64<?URL>)"

Defined in

src/guards/TStringBase64/index.ts:21


TStringMatch

TStringMatch(patternName, regexp): Guard<string>

Validates if a string matches a regexp.

example

const TStringUpperCase = TStringMatch("string(upper-case)",/^[A-Z]/);
TStringUpperCase.isValid("Foo"); // true
TStringUpperCase.isValid("foo"); // false
TStringUpperCase.name === "string(upper-case)"; // true

Parameters

Name Type Description
patternName string Describes the regular expression in a user-readable form.
regexp RegExp The regexp to use for validation of incoming values.

Returns

Guard<string>

A Guard that accepts only strings that matches the given regexp.

guard.name: "string(<regexpName>)"

Defined in

src/guards/TStringMatch/index.ts:23


TStringWithLength

TStringWithLength(options): Guard<string>

Validates if a string is in the given length range.

example

TStringOfLength({ minLength: 5 }).isValid("1234"); // false
TStringOfLength({ minLength: 5 }).isValid("123456789"); // true
TStringOfLength({ minLength: 5 }).name === "string(minLength=5)"; // true

Parameters

Name Type
options Object
options.maxLength? number
options.minLength? number

Returns

Guard<string>

A Guard that accepts only strings, that is in the given length range.

guard.name: "string(minLength=<minLength>,maxLength=<maxLength>)"

Defined in

src/guards/TStringWithLength/index.ts:22


TValidate

TValidate<T>(name, isValid): Guard<T>

Creates a custom Guard from the given params.

example Defining a guard that validates if a number is bigger than 10:

const TBiggerThan10 = TValidate<number>(
  "number(bigger than 10)",
  (value) => typeof value === "number" && value > 10
);

Type parameters

Name Type Description
T never The type the created guard will guard. > ⚠️ Don't forget to provide T type parameter!

Parameters

Name Type Description
name string The name of the type this guard will guard.
isValid (value: any) => boolean A function, which decides if a given value is valid or not.

Returns

Guard<T>

Defined in

src/guards/TValidate/index.ts:22