2.0.2 • Published 7 months ago

@konfirm/guard v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

tests release

Guard

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.

Installation

npm install --save @konfirm/guard

Or use your favorite package manager to install @konfirm/guard

API

Exports

nametype
allfunction
anyfunction
assertfunction
assertionfunction
isfunction
isArrayfunction
isArrayOfSizefunction
isArrayOfTypefunction
isBigIntfunction
isBooleanfunction
isFloatfunction
isFunctionfunction
isGreaterfunction
isGreaterOrEqualfunction
isInstanceOffunction
isIntegerfunction
isKeyfunction
isKeyOfTypefunction
isLessfunction
isLessOrEqualfunction
isNULLfunction
isNegativefunction
isNumberfunction
isObjectfunction
isOptionalKeyOfTypefunction
isPositivefunction
isStrictStructurefunction
isStringfunction
isStringWithPatternfunction
isStructurefunction
isSymbolfunction
isUndefinedfunction
notfunction
FloatType alias
GuardType alias
IntegerType alias
NegativeType alias
PositiveType alias
StringWithPatternType alias
ValidatorType alias

all

all<T>(...checks): Guard<T>

Create a guard verifying the given value matches all of the validators

Parameters

NameType
...checks[Validator, ...<Validator>]

Returns

Guard<T>

any

any<T>(...checks): Guard<T>

Create a guard verifying the given value matches any of the validators

Parameters

NameType
...checks[Validator, ...<Validator>]

Returns

Guard<T>

assert

assert<T>(value, message, ...rules): value is T

Guard asserting the given value to match the conditions or throw an AssertionError otherwise

Parameters

NameType
valueany
messagestring
...rules[Validator, ...<Validator>]

Returns

value is T

assertion

assertion<T>(message, ...rules): Guard<T>

Create an assertion guard, throwing an AssertionError with the provided message if any validator fails

Parameters

NameType
messagestring
...rules[Validator, ...<Validator>]

Returns

Guard<T>

is

is<T>(type): Guard<T>

Create a guard verifying the given value matches the specified type

Parameters

NameType
type"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"

Returns

Guard<T>

isArray

Const isArray(value): value is unknown[]

Guard verifying the value is a array

Parameters

NameType
valueany

Returns

value is unknown[]

isArrayOfSize

isArrayOfSize<T>(min, max?): Guard<T>

Guard verifying the value to be an array with a length between the given boundaries

Parameters

NameTypeDefault value
minnumberundefined
maxnumberInfinity

Returns

Guard<T>

isArrayOfType

isArrayOfType<T>(...validators): Guard<T>

Guard verifying the value to be an array containing only elements matching the validators

Parameters

NameType
...validators[Validator, ...<Validator>]

Returns

Guard<T>

isBigInt

Const isBigInt(value): value is BigInt

Guard verifying the value is a bigint

Parameters

NameType
valueany

Returns

value is BigInt

isBoolean

Const isBoolean(value): value is boolean

Guard verifying the value is a boolean

Parameters

NameType
valueany

Returns

value is boolean

isFloat

isFloat<T>(value): value is T

Guard verifying the value to be a float number

Type parameters

NameType
Tnumber

Parameters

NameType
valueany

Returns

value is T

isFunction

Const isFunction(value): value is Function

Guard verifying the value is a function

Parameters

NameType
valueany

Returns

value is Function

isGreater

isGreater<T>(than): Guard<T>

Create a guard matching numbers greater than the provided value

Type parameters

NameType
Tnumber

Parameters

NameType
thannumber

Returns

Guard<T>

isGreaterOrEqual

isGreaterOrEqual<T>(than): Guard<T>

Create a guard matching numbers greater than or equal to the provided value

Type parameters

NameType
Tnumber

Parameters

NameType
thannumber

Returns

Guard<T>

isInstanceOf

isInstanceOf(type): Guard<typeof type>

Creates a guard verifying the value is an object and an instance of the given class

Parameters

NameType
type(...args: any[]) => unknown

Returns

Guard<typeof type>

isInteger

isInteger<T>(value): value is T

Guard verifying the value to be an integer number

Type parameters

NameType
Tnumber

Parameters

NameType
valueany

Returns

value is T

isKey

isKey<T>(key): Guard<T>

Creates a guard verifying the value is an object and has the given key

Parameters

NameType
keyKey

Returns

Guard<T>

isKeyOfType

isKeyOfType<T>(key, ...validators): Guard<T>

Creates a guard verifying the value is an object and has the given key matching the validators

Parameters

NameType
keyKey
...validatorsValidator[]

Returns

Guard<T>

isLess

isLess<T>(than): Guard<T>

Create a guard matching numbers less than the provided value

Type parameters

NameType
Tnumber

Parameters

NameType
thannumber

Returns

Guard<T>

isLessOrEqual

isLessOrEqual<T>(than): Guard<T>

Create a guard matching numbers less than or equal to the provided value

Type parameters

NameType
Tnumber

Parameters

NameType
thannumber

Returns

Guard<T>

isNULL

Const isNULL(value): value is null

Guard verifying the value is a null

Parameters

NameType
valueany

Returns

value is null

isNegative

isNegative<T>(value): value is T

Guard verifying the value to be a negative number

Type parameters

NameType
Tnumber

Parameters

NameType
valueany

Returns

value is T

isNumber

Const isNumber(value): value is number

Guard verifying the value is a number

Parameters

NameType
valueany

Returns

value is number

isObject

Const isObject(value): value is unknown

Guard verifying the value is a object

Parameters

NameType
valueany

Returns

value is unknown

isOptionalKeyOfType

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

Parameters

NameType
keyKey
...validatorsValidator[]

Returns

Guard<T>

isPositive

isPositive<T>(value): value is T

Guard verifying the value to be a positive number

Type parameters

NameType
Tnumber

Parameters

NameType
valueany

Returns

value is T

isStrictStructure

isStrictStructure<T>(struct, ...options): Guard<T>

Creates a guard verifying the value is an object matching exactly the structure

Parameters

NameType
structObject
...options(Key | Key[])[]

Returns

Guard<T>

isString

Const isString(value): value is string

Guard verifying the value is a string

Parameters

NameType
valueany

Returns

value is string

isStringWithPattern

isStringWithPattern<T>(pattern): Guard<T>

Guard verifying the value to be a string which matches the given pattern

Type parameters

NameType
Tstring

Parameters

NameType
patternRegExp

Returns

Guard<T>

isStructure

isStructure<T>(struct, ...options): Guard<T>

Creates a guard verifying the value is an object matching at least the structure

Parameters

NameType
structStruct<Validator>
...options(Key | Key[])[]

Returns

Guard<T>

isSymbol

Const isSymbol(value): value is Symbol

Guard verifying the value is a symbol

Parameters

NameType
valueany

Returns

value is Symbol

isUndefined

Const isUndefined(value): value is undefined

Guard verifying the value is a undefined

Parameters

NameType
valueany

Returns

value is undefined

not

not<T>(...checks): Guard<T>

Create a guard verifying the given value matches none of the validators

Parameters

NameType
...checks[Validator, ...<Validator>]

Returns

Guard<T>

Type aliases

Float

Ƭ Float: number

Guard

Ƭ Guard<T>: (value: any) => value is T

Type declaration

▸ (value): value is T

Parameters
NameType
valueany
Returns

value is T

Integer

Ƭ Integer: number

Negative

Ƭ Negative: number

Positive

Ƭ Positive: number

StringWithPattern

Ƭ StringWithPattern: string

Validator

Ƭ Validator: (value: any) => boolean

Type declaration

▸ (value): boolean

Parameters
NameType
valueany
Returns

boolean

License

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.

2.0.2

7 months ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.4-2

2 years ago

1.0.4-0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago