improvement(middleware-joi): use joi's detail message
MODIFY
packages/middleware-joi/src/index.ts
packages/middleware-joi/src/index.ts
@@ -12,6 +12,12 @@ import {
map
} from 'rxjs/operators';
+const isJoiValidationError = (
+ err: Joi.ValidationError | Error
+): err is Joi.ValidationError => {
+ return (<Joi.ValidationError>err).isJoi === true;
+};
+
const validateSource = (req: HttpRequest, rules: Map<string, any>, options) =>
from(rules.keys()).pipe(
mergeMap(rule =>
@@ -19,9 +25,12 @@ const validateSource = (req: HttpRequest, rules: Map<string, any>, options) =>
flatMap(item =>
from(Joi.validate(item || {}, rules.get(rule), options))
),
- catchError((err: Error) =>
- throwError(new HttpError(err.message, HttpStatus.BAD_REQUEST))
- ),
+ catchError((err: Joi.ValidationError | Error) => {
+ const message = isJoiValidationError(err)
+ ? err.details[0].message
+ : err.message;
+ return throwError(new HttpError(message, HttpStatus.BAD_REQUEST));
+ }),
map(result => (req[rule] = result))
)
),

improvement(middleware-joi): use joi's detail message