@adaptivestone/framework 3.0.13 → 3.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 3.0.14
2
+
3
+ [NEW] now possible to show all errors during validation (default one) by parameter controllerValidationAbortEarly
4
+ [UPDATE] update deps
5
+
1
6
  ### 3.0.13
2
7
 
3
8
  [UPDATE] bug fix with "mergeParams"
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ controllerValidationAbortEarly: true, // abort validation on first error and return first error
3
+ };
@@ -188,7 +188,8 @@ class AbstractController extends Base {
188
188
  new MiddlewareFunction(this.app, params).getMiddleware(),
189
189
  );
190
190
  }
191
-
191
+ const { controllerValidationAbortEarly } =
192
+ this.app.getConfig('validate');
192
193
  this.router[verb](
193
194
  path,
194
195
  additionalMiddlewares || [],
@@ -203,7 +204,9 @@ class AbstractController extends Base {
203
204
  const bodyAndQuery = merge(req.query, req.body);
204
205
 
205
206
  try {
206
- await routeObject.request.validate(bodyAndQuery);
207
+ await routeObject.request.validate(bodyAndQuery, {
208
+ abortEarly: controllerValidationAbortEarly,
209
+ });
207
210
  } catch (e) {
208
211
  let { errors } = e;
209
212
  // translate it
@@ -214,10 +217,22 @@ class AbstractController extends Base {
214
217
  `Request validation failed with message: ${e.message}. errors: ${errors}`,
215
218
  );
216
219
 
220
+ const errorAnswer = {};
221
+ if (!e.inner.length) {
222
+ errorAnswer[e.path] = errors;
223
+ } else {
224
+ e.inner.forEach((err) => {
225
+ errorAnswer[err.path] = err.errors;
226
+ if (req.i18n && err.errors) {
227
+ errorAnswer[err.path] = err.errors.map((err1) =>
228
+ req.i18n.t(err1),
229
+ );
230
+ }
231
+ });
232
+ }
233
+
217
234
  return res.status(400).json({
218
- errors: {
219
- [e.path]: errors,
220
- },
235
+ errors: errorAnswer,
221
236
  });
222
237
  }
223
238
  req.appInfo.request = routeObject.request.cast(bodyAndQuery, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaptivestone/framework",
3
- "version": "3.0.13",
3
+ "version": "3.0.14",
4
4
  "description": "Adaptive stone node js framework",
5
5
  "main": "index.js",
6
6
  "repository": {