Repository
Current version released
4 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 takestgtand returns a boolean. Iftgtis valid, return true.TgVld- A functional type that takestgtand returns a boolean. Iftgtis valid, return true to perform typeguard.TgtType- The type oftgtinVld.OkType- The type of type guard fortgtinTgVld, or the type oftgtinVld.
Functions
isAny()- AVldthat always returns true.isNever()- ATgVldthat always returns false.isString()- ATgVldthat returns true if thetgtis a string.isNumber()- ATgVldthat returns true if thetgtis a number.isInt()- ATgVldthat returns true if thetgtis an integer.isNonnegInt()- ATgVldthat returns true if thetgtis a non-negative integer.isBigInt()- ATgVldthat returns true if thetgtis a bigint.isBoolean()- ATgVldthat returns true if thetgtis a boolean.isSymbol()- ATgVldthat returns true if thetgtis a symbol.isNull()- ATgVldthat returns true if thetgtis a null.isUndefined()- ATgVldthat returns true if thetgtis a undefined.nodupElems()- AVldthat returns true if there are no duplicate elements in thetgt.eq()- Takesbaseand returnsTgVld. ThatTgVldreturns true iftgtis equal tobase.gt()- Takesbaseand returnsVld. ThatVldreturns true iftgtis greater thanbase.lt()- Takesbaseand returnsVld. ThatVldreturns true iftgtis less thanbase.ge()- Takesbaseand returnsVld. ThatVldreturns true iftgtis greater than or equal tobase.le()- Takesbaseand returnsVld. ThatVldreturns true iftgtis less than or equal tobase.len()- Takesvldand returnsTgVld. ThatTgVldreturns true if the value of thelengthproperty oftgtis valid forvld.array()- TakeselemVldand the optionallenVldand returnsTgVld. ThatTgVldreturns true iftgtis a plain array, all elements are valid forelemVld, and the value of thelengthproperty is valid forlenVld.tuple()- TakeselemVldsand returnsTgVld. ThatTgVldreturns true iftgtis a plain array and each element is valid for each element ofelemVlds.assoc()- TakeselemVldand returnsTgVld. ThatTgVldreturns true iftgtis a plain object and the values of all the properties it owns are valid forelemVld.dict()- TakeselemVldand returnsTgVld. ThatTgVldreturns true iftgtis a plain object, the keys of all the properties it owns are strings, and the values of those properties are valid forelemVld.album()- TakeselemVldand returnsTgVld. ThatTgVldreturns true iftgtis a plain object, the keys of all the properties it owns are symbols, and the values of those properties are valid forelemVld.interf()- TakesvldSchemaand the optionaloptionalKeysand returnsTgVld. ThatTgVldreturns true iftgtis 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()- TakesvldSchemaand the optionaloptionalKeysand returnsTgVld. ThatTgVldreturns true iftgtis 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()- Takesvldsand returnsTgVld. ThatTgVldreturns true iftgtis valid for all elements ofvlds.union()- Takesvldsand returnsTgVld. ThatTgVldreturns true iftgtis valid for any element ofvlds.lazy()- TakesvldGetandargsand returnsTgVld. ThatTgVldreturns true iftgtis valid for the return value of passing all the elements ofargstovldGet.
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
}