tguard / Exports
- TAny
- TAnyObject
- TBigInt
- TBoolean
- TFunction
- TInteger
- TIntegerAsString
- TNull
- TNumber
- TNumberAsString
- TString
- TStringEmail
- TStringISODate
- TStringJSON
- TStringJWT
- TStringMIMEType
- TStringPhoneNumber
- TStringSemVer
- TStringURL
- TStringUUID
- TUndefined
- TAnd
- TArray
- TConstant
- TNot
- TObject
- TObjectOfShape
- TOr
- TStringBase64
- TStringMatch
- TStringWithLength
- TValidate
Ƭ GuardedType<C
>: C
extends Guard
<infer T> ? T
: unknown
Infers the type, that the given Guard
guards.
Name | Type |
---|---|
C |
extends Guard <unknown > |
Ƭ ObjectSchema: Object
▪ [fieldName: string
]: ObjectSchema
| Guard
<unknown
>
src/guards/TObject/types.ts:16
• Const
TAny: Guard
<any
>
Guard that accepts any value.
guard.name: "any"
• Const
TAnyObject: Guard
<Object
>
Primitive guard that accepts all objects. Does not accept null.
guard.name: "object"
src/guards/TAnyObject/index.ts:9
• Const
TBigInt: Guard
<BigInt
>
Primitive guard that only accepts bigint
values.
guard.name: "bigint"
• Const
TBoolean: Guard
<boolean
>
Primitive guard that only accepts boolean
values.
guard.name: "boolean"
src/guards/TBoolean/index.ts:8
• Const
TFunction: Guard
<Function
>
Primitive guard that only accepts function
values.
guard.name: "function"
src/guards/TFunction/index.ts:8
• 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
src/guards/TInteger/index.ts:15
• 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
src/guards/TIntegerAsString/index.ts:16
• Const
TNull: Guard
<null
>
Guard that only accepts null
.
guard.name: "null"
• Const
TNumber: Guard
<number
>
Primitive guard that only accepts numbers.
Not accepts NaN
.
guard.name: "number"
• 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
src/guards/TNumberAsString/index.ts:15
• Const
TString: Guard
<string
>
Primitive guard that only accepts string
values.
guard.name: "string"
• 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
src/guards/TStringEmail/index.ts:16
• 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
src/guards/TStringISODate/index.ts:16
• 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
src/guards/TStringJSON/index.ts:16
• 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
src/guards/TStringJWT/index.ts:16
• 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
src/guards/TStringMIMEType/index.ts:16
• 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
src/guards/TStringPhoneNumber/index.ts:17
• 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
src/guards/TStringSemVer/index.ts:16
• 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
src/guards/TStringURL/index.ts:16
• 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
src/guards/TStringUUID/index.ts:19
• Const
TUndefined: Guard
<undefined
>
Primitive guard that only accepts undefined
values.
guard.name: "undefined"
src/guards/TUndefined/index.ts:8
▸ 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>"
Name | Type |
---|---|
A |
A |
B |
B |
T |
extends Guard <unknown >[] |
Name | Type |
---|---|
guardA |
Guard <A > |
guardB |
Guard <B > |
...others |
T |
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.
▸ 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
Name |
---|
T |
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. |
Guard
<T
[]>
A Guard
that checks if the given value is an array of the given type.
▸ 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
Name | Type |
---|---|
T |
extends string | number | boolean | BigInt |
Name | Type | Description |
---|---|---|
constant |
T |
The literal to compare values against. Only can be a string , number , boolean or bigint . |
Guard
<T
>
A Guard
which checks if the given value is equals to the constant
literal.
guard.name: "constant(<constant.name>)"
src/guards/TConstant/index.ts:26
▸ 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
Name |
---|
T |
Name | Type | Description |
---|---|---|
guard |
Guard <T > |
The guard, which will be negated. |
Guard
<Exclude
<any
, T
>>
A Guard
that accepts a value when it was not accepted by the given guard.
guard.name: "!<type>"
▸ 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
Name | Type |
---|---|
T |
extends ObjectSchema |
Name | Type | Description |
---|---|---|
schema |
T |
Is a tree of guards. (Just a normal JS object, but with Guard values) |
Guard
<SchemaType
<T
>>
A Guard
.
src/guards/TObject/index.ts:31
▸ 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
Name |
---|
T |
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 > |
- |
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> }"
src/guards/TObjectOfShape/index.ts:39
▸ 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
Name | Type |
---|---|
A |
A |
B |
B |
T |
extends Guard <unknown >[] |
Name | Type |
---|---|
guardA |
Guard <A > |
guardB |
Guard <B > |
...others |
T |
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>"
▸ 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
Name | Type | Description |
---|---|---|
options |
Object |
- |
options.urlSafe |
boolean |
If set to true, it will check if the string is bas64URL encoded |
Guard
<string
>
A Guard
that accepts only strings that are base64 encoded.
guard.name: "string(base64<?URL>)"
src/guards/TStringBase64/index.ts:21
▸ 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
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. |
Guard
<string
>
A Guard
that accepts only strings that matches the given regexp
.
guard.name: "string(<regexpName>)"
src/guards/TStringMatch/index.ts:23
▸ 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
Name | Type |
---|---|
options |
Object |
options.maxLength? |
number |
options.minLength? |
number |
Guard
<string
>
A Guard
that accepts only strings, that is in the given length range.
guard.name: "string(minLength=<minLength>,maxLength=<maxLength>)"
src/guards/TStringWithLength/index.ts:22
▸ 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
);
Name | Type | Description |
---|---|---|
T |
never |
The type the created guard will guard. > T type parameter! |
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. |
Guard
<T
>