@leancodepl/validation
Version:
CQRS validation helpers for command and query validation
76 lines (75 loc) • 1.43 kB
JavaScript
function i(e, n, o = []) {
return {
handle: (r, f) => {
let t;
for (const l of Array.isArray(r) ? r : [r]) {
const s = e.find(
(c) => c.ErrorCode === n[l]
);
if (s) {
t = f(l, s);
break;
}
}
let u = o;
return t !== void 0 && (u = [...u, t]), i(
e,
n,
u
);
},
handleAll: (r, f) => {
let t;
const l = (Array.isArray(r) ? r : [r]).reduce(
(c, d) => {
const h = e.filter(
(E) => E.ErrorCode === n[d]
);
return h.length === 0 ? c : [
...c,
{
errorName: d,
errors: h
}
];
},
[]
);
l.length > 0 && (t = f(l));
let s = o;
return t !== void 0 && (s = [...s, t]), i(
e,
n,
s
);
},
check: ((r) => {
if (r)
return o.reduce(r.reducer, r.initialValue);
})
};
}
function A(e, n) {
const o = {
...n,
success: -1,
failure: -2
}, a = e.isSuccess ? e.result.WasSuccessful ? [
{
ErrorMessage: "",
PropertyName: "",
ErrorCode: -1
}
] : e.result.ValidationErrors : [
{
ErrorMessage: "",
PropertyName: "",
ErrorCode: -2
}
];
return i(a, o);
}
export {
A as handleResponse,
i as handleValidationErrors
};