@adaptivestone/framework 3.0.11 → 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,17 @@
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
+
6
+ ### 3.0.13
7
+
8
+ [UPDATE] bug fix with "mergeParams"
9
+
10
+ ### 3.0.12
11
+
12
+ [NEW] ability to pass "mergeParams" options to express router
13
+ [UPDATE] update deps
14
+
1
15
  ### 3.0.11
2
16
 
3
17
  [UPDATE] more verbose email error
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ controllerValidationAbortEarly: true, // abort validation on first error and return first error
3
+ };
@@ -16,11 +16,13 @@ const Auth = require('../services/http/middleware/Auth');
16
16
  * In most cases you will want to have a 'home' route that not include controller name. For this case please check 'getExpressPath'
17
17
  */
18
18
  class AbstractController extends Base {
19
- constructor(app, prefix) {
19
+ constructor(app, prefix, isExpressMergeParams = false) {
20
20
  const time = Date.now();
21
21
  super(app);
22
22
  this.prefix = prefix;
23
- this.router = express.Router();
23
+ this.router = express.Router({
24
+ mergeParams: isExpressMergeParams,
25
+ });
24
26
  const { routes } = this;
25
27
  const expressPath = this.getExpressPath();
26
28
 
@@ -186,7 +188,8 @@ class AbstractController extends Base {
186
188
  new MiddlewareFunction(this.app, params).getMiddleware(),
187
189
  );
188
190
  }
189
-
191
+ const { controllerValidationAbortEarly } =
192
+ this.app.getConfig('validate');
190
193
  this.router[verb](
191
194
  path,
192
195
  additionalMiddlewares || [],
@@ -201,7 +204,9 @@ class AbstractController extends Base {
201
204
  const bodyAndQuery = merge(req.query, req.body);
202
205
 
203
206
  try {
204
- await routeObject.request.validate(bodyAndQuery);
207
+ await routeObject.request.validate(bodyAndQuery, {
208
+ abortEarly: controllerValidationAbortEarly,
209
+ });
205
210
  } catch (e) {
206
211
  let { errors } = e;
207
212
  // translate it
@@ -212,10 +217,22 @@ class AbstractController extends Base {
212
217
  `Request validation failed with message: ${e.message}. errors: ${errors}`,
213
218
  );
214
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
+
215
234
  return res.status(400).json({
216
- errors: {
217
- [e.path]: errors,
218
- },
235
+ errors: errorAnswer,
219
236
  });
220
237
  }
221
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.11",
3
+ "version": "3.0.14",
4
4
  "description": "Adaptive stone node js framework",
5
5
  "main": "index.js",
6
6
  "repository": {