validateInstance
Description
Resolves if the argument passed is an instance of a class reference. For example, if you have a class User and you want to check if the variable user is an instance of User, you can use this function.
Parameter List | Returns | Rejection Errors |
---|---|---|
| Promise<* || Array> | |
Related Functions | Function Signature | |
function validateNotInstance(supposedInstance[, tuple]): Promise<* || Array> |
Examples
1
2
3
4
5
6
import { validateInstance } from "puddy-m/lib/validators";
class User {}
const user = new User();
// Will resolve since user is not instance of an Array
validateInstance(User, user).then(console.log); // Prints user{}
1
2
3
4
5
6
7
import { validateInstance, validateString } from "puddy-m/lib/validators";
class User {}
const user = new User();
validateInstance(User, user, [])
.then((tuple) => validateString("user is a User", tuple))
.then(console.log); // prints [user{}, 'user is a User']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { validateInstance } from "puddy-m/lib/validators";
const run = async () => {
try {
class User {}
const user = new User();
const validatedUser = await validateInstance(User, user);
console.log(validatedUser);
} catch (e) {
console.log(e);
}
};
run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { validateInstance } from "puddy-m/lib/validators";
const run = async () => {
try {
class User {}
const user = new User();
const notArray = await validateInstance(Array, user); // As user is instance of User, this function will throw
console.log(notArray); // Since user is not an Array, this function will throw
} catch (e) {
// Throws InstanceValidationError
console.log(e);
}
};
run();