validateObjectWithExistingKey
Description
It resolves the object if it contains the expected key.
eg: {name:"Foo"}, the "name" key exists in the object, and thus, it will resolve if you pass the the string "name" in the key parameter. This method is useful when you are expecting to receive an object with an specific key, like handling http requests/responses objects.
Parameter List | Returns | Rejection Errors |
---|---|---|
| Promise<object> | |
Related Functions | Function Signature | |
function validateObjectWithExistingKey(key, object[, tuple]): Promise<object> |
Examples
1
2
3
import { validateObjectWithExistingKey } from "puddy-m/lib/validators/complexValidators";
validateObjectWithExistingKey("foo", { foo: "bar" }).then(console.log); // prints {foo: "bar"}
1
2
3
4
5
6
import { validateString } from "puddy-m/lib/validators";
import { validateObjectWithExistingKey } from "puddy-m/lib/validators/complexValidators";
validateObjectWithExistingKey("someKey", {someKey:"foo"}, [])
.then((tuple) => validateString("foo", tuple))
.then(console.log); // prints [{someKey:"foo"}, 'foo']
1
2
3
4
5
6
7
8
9
10
11
12
13
import { validateObjectWithExistingKey } from "puddy-m/lib/validators/complexValidators";
const run = async () => {
try {
const obj = await validateObjectWithExistingKey("a",{a:"a"});
console.log(obj); // prints {}
} catch (e) {
console.log(e);
}
};
run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { validateObjectWithExistingKey } from "puddy-m/lib/validators/complexValidators";
const run = async () => {
try {
const obj = await validateObjectWithExistingKey("nonExstingKey",{a:"a"});
console.log(obj); // Because Key doesn't exist in object, it will throw
} catch (e) {
// Throw ObjectWithExistingKeyValidationError
console.log(e);
}
};
run();