Repository
Current version released
3 years ago
Deno is_x module
This module provides some basic utilities for creating or utilizing type-guarded validation.
Requirement
- Deno v1.14.0+
Usage
The validation in this module is specifically the following Vld
type function.
Types
Vld
- A functional type that takestgt
and returns a boolean. Iftgt
is valid, return true.TgVld
- A functional type that takestgt
and returns a boolean. Iftgt
is valid, return true to perform typeguard.TgtType
- The type oftgt
inVld
.OkType
- The type of type guard fortgt
inTgVld
, or the type oftgt
inVld
.
Functions
isAny()
- AVld
that always returns true.isNever()
- ATgVld
that always returns false.isString()
- ATgVld
that returns true if thetgt
is a string.isNumber()
- ATgVld
that returns true if thetgt
is a number.isInt()
- ATgVld
that returns true if thetgt
is an integer.isNonnegInt()
- ATgVld
that returns true if thetgt
is a non-negative integer.isBigInt()
- ATgVld
that returns true if thetgt
is a bigint.isBoolean()
- ATgVld
that returns true if thetgt
is a boolean.isSymbol()
- ATgVld
that returns true if thetgt
is a symbol.isNull()
- ATgVld
that returns true if thetgt
is a null.isUndefined()
- ATgVld
that returns true if thetgt
is a undefined.nodupElems()
- AVld
that returns true if there are no duplicate elements in thetgt
.eq()
- Takesbase
and returnsTgVld
. ThatTgVld
returns true iftgt
is equal tobase
.gt()
- Takesbase
and returnsVld
. ThatVld
returns true iftgt
is greater thanbase
.lt()
- Takesbase
and returnsVld
. ThatVld
returns true iftgt
is less thanbase
.ge()
- Takesbase
and returnsVld
. ThatVld
returns true iftgt
is greater than or equal tobase
.le()
- Takesbase
and returnsVld
. ThatVld
returns true iftgt
is less than or equal tobase
.len()
- Takesvld
and returnsTgVld
. ThatTgVld
returns true if the value of thelength
property oftgt
is valid forvld
.array()
- TakeselemVld
and the optionallenVld
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain array, all elements are valid forelemVld
, and the value of thelength
property is valid forlenVld
.tuple()
- TakeselemVlds
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain array and each element is valid for each element ofelemVlds
.assoc()
- TakeselemVld
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain object and the values of all the properties it owns are valid forelemVld
.dict()
- TakeselemVld
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain object, the keys of all the properties it owns are strings, and the values of those properties are valid forelemVld
.album()
- TakeselemVld
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain object, the keys of all the properties it owns are symbols, and the values of those properties are valid forelemVld
.interf()
- TakesvldSchema
and the optionaloptionalKeys
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain object, and each property it owns is valid for each property owned byvldSchema
. Also, for a property that has a key inoptionalKeys
, it is valid even if the key does not exist or the value is undefined.struct()
- TakesvldSchema
and the optionaloptionalKeys
and returnsTgVld
. ThatTgVld
returns true iftgt
is a plain object, and each property it owns is valid for each property owned byvldSchema
, and it owns only properties owned byvldSchema
. Also, for a property that has a key inoptionalKeys
, it is valid even if the key does not exist or the value is undefined.extend()
- Takesvlds
and returnsTgVld
. ThatTgVld
returns true iftgt
is valid for all elements ofvlds
.union()
- Takesvlds
and returnsTgVld
. ThatTgVld
returns true iftgt
is valid for any element ofvlds
.lazy()
- TakesvldGet
andargs
and returnsTgVld
. ThatTgVld
returns true iftgt
is valid for the return value of passing all the elements ofargs
tovldGet
.
Examples
Basic:
import { isString, eq, tuple, struct, union }
from 'https://deno.land/x/is_x/mod.ts'
const isResult = union(
struct({
kind: eq('ok'),
data: tuple(isString, isString)
}),
struct({
kind: eq('ng'),
message: isString
}, ['message'])
)
const tgt = JSON.parse('{ "kind": "ok", "data": ["a", "b"] }')
if (isResult(tgt)) {
// Guard the type of tgt
}
Extend:
import { isString, extend }
from 'https://deno.land/x/is_x/mod.ts'
function isURLFmt(tgt: string) {
// ...
return true
}
const isURL = extend(isString, isURLFmt)
const tgt = JSON.parse('"https://deno.land/x/is_x"')
if (isURL(tgt)) {
// Guard the type of tgt
}
Recursive:
import { TgVld, struct, lazy }
from 'https://deno.land/x/is_x/mod.ts'
type LoopNest = { a?: LoopNest }
function loopNest(): TgVld<unknown, LoopNest> {
return struct({
a: lazy(loopNest)
}, ['a'])
}
const isLoopNest = loopNest()
const tgt = JSON.parse('{ "a": { "a": {} } }')
if (isLoopNest(tgt)) {
// Guard the type of tgt
}