validateType
Description
It makes a validation upon primitive built-in javascript types, excluding complex/custom types (eg: classes and arrays).
Primitive examples: "undefined", "boolean", "number", "string", "bigint", "symbol".
Parameter List | Returns | Rejection Errors |
---|---|---|
| Promise<*> | |
Related Functions | Function Signature | |
function validateType(type, arg[, tuple]): Promise<*> |
Examples
1
2
3
import { validateType } from "puddy-m/lib/validators";
validateType("boolean", true).then(console.log); // prints true
1
2
3
4
5
import { validateType, validateString } from "puddy-m/lib/validators";
validateType("string", "false", [])
.then((tuple) => validateString("foo", tuple))
.then(console.log); // prints ['false', 'foo']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { validateType } from "puddy-m/lib/validators";
const run = async () => {
try {
const validTypeVar = await validateType("boolean", false);
console.log(validTypeVar); // prints false
} catch (e) {
console.log(e);
}
};
run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { validateType } from "puddy-m/lib/validators";
const run = async () => {
try {
const validTypeVar = await validateType("boolean", 80);
console.log(validTypeVar); // since arg is not a bollean, function will throw
} catch (e) {
// Throws TypeValidationError
console.log(e);
}
};
run();