arrayGuardMemo()
function arrayGuardMemo<T>(itemGuard): Guard<T[]>
Validate arrays
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
itemGuard | Guard <T > | validates every item in the array |
Returns
Guard
<T
[]>
a guard function that validates arrays
dictionaryGuardMemo()
function dictionaryGuardMemo<Key, Value>(keyGard, valueGuard): Guard<Partial<Record<Key, Value>>>
Dictionaries are objects where all properties are optional and have the same type. In TypeScript, this can be represented by Partial<Record<string, ?>>
.
Type Parameters
Type Parameter |
---|
Key extends string |
Value |
Parameters
Parameter | Type | Description |
---|---|---|
keyGard | Guard <Key > | validates every key |
valueGuard | Guard <Value > | validates every value |
Returns
Guard
<Partial
<Record
<Key
, Value
>>>
a guard for a dictionary, of type Partial<Record<K, V>>
Examples
Validate a word dictionary:
const isDictionary = dictionaryGuard(isString, isString)
isDictionary({ hello: 'world' }) // -> true
You can limit the set of keys; for example, to only allow lowercase strings:
const isLowerCase = (data: unknown): data is Lowercase<string> =>
typeof data === 'string' && data === data.toLowerCase()
const isDictionary = dictionaryGuard(isLowerCase, isString)
isDictionary({ hello: 'world' }) // -> true
isDictionary({ Hello: 'world' }) // -> false
nonEmptyArrayGuardMemo()
function nonEmptyArrayGuardMemo<T>(itemGuard): Guard<[T, ...T[]]>
Validate non-empty arrays
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
itemGuard | Guard <T > | validates every item in the array |
Returns
Guard
<[T
, ...T[]
]>
a guard function that validates non-empty arrays
objectGuardCompiledMemo()
function objectGuardCompiledMemo<T>(schema): Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>
Same as objectGuard, but performs just-in-time (JIT) compilation with the Function
constructor, which greatly increases the execution speed of the validation. However, the JIT compilation is slow and gets executed at the time when the validation function is constructed. When using this function at the module level, it is recommended to wrap it in lazy to defer the JIT compilation to when the validation function is called for the first time. This function will be blocked in environments where the Function
constructor is blocked; for example, when the Content-Security-Policy policy is set without the 'unsafe-eval
' directive.
Example
Defer the JIT compilation to when the validation function is called for the first time.
const isUser = lazy(() => objectGuardCompiled({
id: isNumber,
name: isString,
})
See
objectGuard for a non-just-in-time compiled version of this function.
Type Parameters
Type Parameter |
---|
T extends Record <string , unknown > |
Parameters
Parameter | Type | Description |
---|---|---|
schema | { [K in string | number | symbol]-?: Object extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]> } | maps keys to validation functions. |
Returns
Guard
<OptionalKeys
<T
> extends undefined
? T
: WithOptionalFields
<T
>>
objectGuardMemo()
function objectGuardMemo<T>(schema): Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>
Objects have a fixed set of properties of different types.
See
objectGuardCompiled for just-in-time compiled version of this function.
Type Parameters
Type Parameter |
---|
T extends Record <string , unknown > |
Parameters
Parameter | Type | Description |
---|---|---|
schema | { [K in string | number | symbol]-?: Object extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]> } | maps keys to validation functions. |
Returns
Guard
<OptionalKeys
<T
> extends undefined
? T
: WithOptionalFields
<T
>>
Examples
Object with both required and optional properties:
const isUser = objectGuard({
id: isNumber,
uid: isString,
active: isBoolean,
email: optional(isString),
})
Note that optional properties will be inferred as required properties that can be assigned undefined
. See Infer > limitations for in-depth information.
Annotate explicitly:
type User = {
id: number
name: string
}
const isUser = object<User>({
id: isNumber,
name: isString,
})
tupleGuardMemo()
function tupleGuardMemo<T>(guards): Guard<T>
Type Parameters
Type Parameter |
---|
T extends readonly unknown [] |
Parameters
Parameter | Type | Description |
---|---|---|
guards | [...{ [K in string | number | symbol]: Guard<T[K<K>]> }[]] | an array of guards. Each guard validates the corresponding element in the data tuple. |
Returns
Guard
<T
>
unionGuardMemo()
function unionGuardMemo<T>(...guards): (data) => data is T[number]
Executes guards
in order and returns true if any guard matches. The result type is a union.
Type Parameters
Type Parameter |
---|
T extends readonly unknown [] |
Parameters
Parameter | Type | Description |
---|---|---|
...guards | { [K in string | number | symbol]: Guard<T[K<K>]> } | any of these guard functions must match the data. |
Returns
Function
a guard function that validates unions
Parameters
Parameter | Type | Description |
---|---|---|
data | unknown | data to be validated |
Returns
data is T[number]
true
if the data is in the specified union
Examples
Commonly used in discriminated unions:
const isResult = oneOfGuard([
objectGuard({
tag: equalsGuard('success')
}),
objectGuard({
tag: equalsGuard('error')
}),
])
When explicitly annotating oneOfGuard
, provide a tuple of the union members as type argument:
const isId = oneOfGuard<[string, number]>(isString, isNumber)
Due to a limitation of TypeScript, it is not possible to write unionGuard<string | number>()
or equalsGuard<'red' | 'green' | 'blue'>()
. Therefore, it is generally recommended to omit the type arguments for union types and let TypeScript infer them.