Guards - or input validation - is a concept in Typescript which allows you to easily ensure specific types. Besides being a great assistance when authoring your code in Typescript, they have the added benefit of being preserved during the transpilation to Javascript, which means your validations will become part of the 'compiled' Javascript. This solves an important issue with Typescript based projects at runtime, as the integrity of the data will still be guaranteed by your Guards.
For pure Javascript developers this is good news, as these Guards easily let you validate your variables too.
npm install --save @konfirm/guard
Or use your favorite package manager to install @konfirm/guard
| name | type |
|---|---|
| all | function |
| any | function |
| assert | function |
| assertion | function |
| is | function |
| isArray | function |
| isArrayOfSize | function |
| isArrayOfType | function |
| isBigInt | function |
| isBoolean | function |
| isFloat | function |
| isFunction | function |
| isGreater | function |
| isGreaterOrEqual | function |
| isInstanceOf | function |
| isInteger | function |
| isKey | function |
| isKeyOfType | function |
| isLess | function |
| isLessOrEqual | function |
| isNULL | function |
| isNegative | function |
| isNumber | function |
| isObject | function |
| isOptionalKeyOfType | function |
| isPositive | function |
| isStrictStructure | function |
| isString | function |
| isStringWithPattern | function |
| isStructure | function |
| isSymbol | function |
| isUndefined | function |
| not | function |
| Float | Type alias |
| Guard | Type alias |
| Integer | Type alias |
| Negative | Type alias |
| Positive | Type alias |
| StringWithPattern | Type alias |
| Validator | Type alias |
▸ all<T>(...checks): Guard<T>
Create a guard verifying the given value matches all of the validators
| Name | Type |
|---|---|
...checks |
[Validator, ...<Validator>] |
Guard<T>
▸ any<T>(...checks): Guard<T>
Create a guard verifying the given value matches any of the validators
| Name | Type |
|---|---|
...checks |
[Validator, ...<Validator>] |
Guard<T>
▸ assert<T>(value, message, ...rules): value is T
Guard asserting the given value to match the conditions or throw an AssertionError otherwise
| Name | Type |
|---|---|
value |
any |
message |
string |
...rules |
[Validator, ...<Validator>] |
value is T
▸ assertion<T>(message, ...rules): Guard<T>
Create an assertion guard, throwing an AssertionError with the provided message if any validator fails
| Name | Type |
|---|---|
message |
string |
...rules |
[Validator, ...<Validator>] |
Guard<T>
▸ is<T>(type): Guard<T>
Create a guard verifying the given value matches the specified type
| Name | Type |
|---|---|
type |
"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" |
Guard<T>
▸ Const isArray(value): value is unknown[]
Guard verifying the value is a array
| Name | Type |
|---|---|
value |
any |
value is unknown[]
▸ isArrayOfSize<T>(min, max?): Guard<T>
Guard verifying the value to be an array with a length between the given boundaries
| Name | Type | Default value |
|---|---|---|
min |
number |
undefined |
max |
number |
Infinity |
Guard<T>
▸ isArrayOfType<T>(...validators): Guard<T>
Guard verifying the value to be an array containing only elements matching the validators
| Name | Type |
|---|---|
...validators |
[Validator, ...<Validator>] |
Guard<T>
▸ Const isBigInt(value): value is BigInt
Guard verifying the value is a bigint
| Name | Type |
|---|---|
value |
any |
value is BigInt
▸ Const isBoolean(value): value is boolean
Guard verifying the value is a boolean
| Name | Type |
|---|---|
value |
any |
value is boolean
▸ isFloat<T>(value): value is T
Guard verifying the value to be a float number
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
value |
any |
value is T
▸ Const isFunction(value): value is Function
Guard verifying the value is a function
| Name | Type |
|---|---|
value |
any |
value is Function
▸ isGreater<T>(than): Guard<T>
Create a guard matching numbers greater than the provided value
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
than |
number |
Guard<T>
▸ isGreaterOrEqual<T>(than): Guard<T>
Create a guard matching numbers greater than or equal to the provided value
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
than |
number |
Guard<T>
▸ isInstanceOf(type): Guard<typeof type>
Creates a guard verifying the value is an object and an instance of the given class
| Name | Type |
|---|---|
type |
(...args: any[]) => unknown |
Guard<typeof type>
▸ isInteger<T>(value): value is T
Guard verifying the value to be an integer number
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
value |
any |
value is T
▸ isKey<T>(key): Guard<T>
Creates a guard verifying the value is an object and has the given key
| Name | Type |
|---|---|
key |
Key |
Guard<T>
▸ isKeyOfType<T>(key, ...validators): Guard<T>
Creates a guard verifying the value is an object and has the given key matching the validators
| Name | Type |
|---|---|
key |
Key |
...validators |
Validator[] |
Guard<T>
▸ isLess<T>(than): Guard<T>
Create a guard matching numbers less than the provided value
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
than |
number |
Guard<T>
▸ isLessOrEqual<T>(than): Guard<T>
Create a guard matching numbers less than or equal to the provided value
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
than |
number |
Guard<T>
▸ Const isNULL(value): value is null
Guard verifying the value is a null
| Name | Type |
|---|---|
value |
any |
value is null
▸ isNegative<T>(value): value is T
Guard verifying the value to be a negative number
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
value |
any |
value is T
▸ Const isNumber(value): value is number
Guard verifying the value is a number
| Name | Type |
|---|---|
value |
any |
value is number
▸ Const isObject(value): value is unknown
Guard verifying the value is a object
| Name | Type |
|---|---|
value |
any |
value is unknown
▸ isOptionalKeyOfType<T>(key, ...validators): Guard<T>
Creates a guard verifying the value is an object and doesn't have the key or has the given key matching the validators
| Name | Type |
|---|---|
key |
Key |
...validators |
Validator[] |
Guard<T>
▸ isPositive<T>(value): value is T
Guard verifying the value to be a positive number
| Name | Type |
|---|---|
T |
number |
| Name | Type |
|---|---|
value |
any |
value is T
▸ isStrictStructure<T>(struct, ...options): Guard<T>
Creates a guard verifying the value is an object matching exactly the structure
| Name | Type |
|---|---|
struct |
Object |
...options |
(Key | Key[])[] |
Guard<T>
▸ Const isString(value): value is string
Guard verifying the value is a string
| Name | Type |
|---|---|
value |
any |
value is string
▸ isStringWithPattern<T>(pattern): Guard<T>
Guard verifying the value to be a string which matches the given pattern
| Name | Type |
|---|---|
T |
string |
| Name | Type |
|---|---|
pattern |
RegExp |
Guard<T>
▸ isStructure<T>(struct, ...options): Guard<T>
Creates a guard verifying the value is an object matching at least the structure
| Name | Type |
|---|---|
struct |
Struct<Validator> |
...options |
(Key | Key[])[] |
Guard<T>
▸ Const isSymbol(value): value is Symbol
Guard verifying the value is a symbol
| Name | Type |
|---|---|
value |
any |
value is Symbol
▸ Const isUndefined(value): value is undefined
Guard verifying the value is a undefined
| Name | Type |
|---|---|
value |
any |
value is undefined
▸ not<T>(...checks): Guard<T>
Create a guard verifying the given value matches none of the validators
| Name | Type |
|---|---|
...checks |
[Validator, ...<Validator>] |
Guard<T>
Ƭ Float: number
Ƭ Guard<T>: (value: any) => value is T
▸ (value): value is T
| Name | Type |
|---|---|
value |
any |
value is T
Ƭ Integer: number
Ƭ Negative: number
Ƭ Positive: number
Ƭ StringWithPattern: string
Ƭ Validator: (value: any) => boolean
▸ (value): boolean
| Name | Type |
|---|---|
value |
any |
boolean
MIT License Copyright (c) 2021-2023 Rogier Spieker (Konfirm)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.