Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1x 1x 2x 1x 2x 2x 4x | import { pipe } from "@benneq/function";
import { flatMap, limit } from "@benneq/iterable";
import { ConstraintViolation, Validator } from "./_types";
/**
*
* @example
* ```ts
* const isMyObjectValidator = validate(objectValidator({
* a: valueValidator(isString, "err1"),
* b: valueValidator(isString, "err2"),
* }));
* const result = isMyObjectValidator({ a: "", b: 42 });
* console.log(result); // [{ path: "$.b", message: "err2", value: 42 }]
* ```
*
* @param def
* @returns
*/
export const objectValidator =
<T extends Record<string, unknown>, R = T, P = unknown>(def: {
[key in keyof T]: Validator<T[key], R, T>;
}): Validator<T, R, P> =>
(obj, context) => {
return pipe(
flatMap<[keyof T, Validator<T[keyof T], R, T>], ConstraintViolation>(
([key, validator]) =>
validator(obj[key], {
...context,
path: context.path + "." + (key as string),
parent: obj,
})
),
limit<ConstraintViolation>(context.shortCircuit ? 1 : -1)
)(Object.entries(def));
};
|