validateListOfType
Description
Resolves the list if all elements in it are of type specified in the "type" parameter. The type parameter should be a string available in the PrimitiveType enum:
If using PrimitiveTypes enum, make sure to import: import { PrimitiveTypes } from "puddy-m/lib/enums". Note that this function will not resolve if the type parameter is any string that is not pre-defined in the PrimitiveTypes menu. In this case, it will reject ObjectWithExistingKeyValidationError.
Parameter List | Returns | Rejection Errors |
---|---|---|
| Promise<Array<*> || Array> | |
Related Functions | Function Signature | |
function validateListOfType(type, list[, tuple]): Promise<Array<*> || Array> |
Examples
1
2
3
4
5
import { validateListOfType } from "puddy-m/lib/validators/listValidators";
validateListOfType("boolean", [true, false]).then((booleanList) =>
console.log(booleanList)
); // print [ true, false ]
1
2
3
4
5
6
import { validateListOfType } from "puddy-m/lib/validators/listValidators";
import { validateString } from "puddy-m/lib/validators";
validateListOfType("string", ["foo", "bar"], [])
.then((tuple) => validateString("string", tuple))
.then((tuple) => console.log(tuple)); // print [ 'foo', 'bar'], 'string' ]
1
2
3
4
5
6
7
8
9
10
11
12
13
import { validateListOfType } from "puddy-m/lib/validators/listValidators";
const run = async () => {
try {
const validatedListOfObjects = await validateListOfType("object", [{}, {}]);
console.log(validatedListOfObjects); // [ {}, {} ]
} catch (e) {
console.log(e);
}
};
run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { validateListOfType } from "puddy-m/lib/validators/listValidators";
const run = async () => {
try {
const validatedListOfObjects = await validateListOfType("object", ["", {}]);
console.log(validatedListOfObjects);
} catch (e) {
// Throws ListOfTypeValidationError
console.log(e);
}
};
run();