codehooks-js 1.2.11 → 1.2.13

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.
@@ -206,14 +206,14 @@ async function updateFunc(req, res) {
206
206
  }
207
207
  const document = req.body;
208
208
  delete document._id; // avoid schema validation error
209
- console.log(req.headers['x-real-ip'], req.method, req.originalUrl, _schema[collection]);
209
+ console.log(req.headers['x-real-ip'], req.method, req.originalUrl);
210
210
  if (_schema[collection] != null) {
211
211
  _validate(_schema[collection], document, _opt)
212
212
  .then(async function (value) {
213
213
  document._id = ID; // put back id that was removed for validation
214
214
  const conn = await Datastore.open();
215
215
  await _eventHooks.fireBefore(collection, 'PUT', document);
216
- const result = await conn.replaceOne(collection, ID, document, {});
216
+ const result = await conn.replaceOne(collection, ID, document, {});
217
217
  await _eventHooks.fireAfter(collection, 'PUT', result);
218
218
  res.json(result);
219
219
  })
@@ -225,15 +225,20 @@ async function updateFunc(req, res) {
225
225
  document._id = ID; // put back id that was removed for validation
226
226
  const conn = await Datastore.open();
227
227
  await _eventHooks.fireBefore(collection, 'PUT', document);
228
- const result = await conn.replaceOne(collection, ID, document, {});
228
+ const result = await conn.replaceOne(collection, ID, document, {});
229
229
  await _eventHooks.fireAfter(collection, 'PUT', result);
230
230
  res.json(result);
231
231
  }
232
232
  } catch (e) {
233
- console.error(e.message)
234
- res
233
+ if (e.schemaError) {
234
+ res
235
+ .status(400) // validator
236
+ .end(e);
237
+ } else {
238
+ res
235
239
  .status(404) // not found
236
240
  .end(e.message);
241
+ }
237
242
  }
238
243
  }
239
244
 
@@ -251,11 +256,16 @@ async function patchFunc(req, res) {
251
256
  const result = await conn.updateOne(collection, ID, document, {});
252
257
  await _eventHooks.fireAfter(collection, 'PATCH', document);
253
258
  res.json(result);
254
- } catch (e) {
255
-
256
- res
259
+ } catch (e) {
260
+ if (e.schemaError) {
261
+ res
262
+ .status(400) // validator
263
+ .end(e);
264
+ } else {
265
+ res
257
266
  .status(404) // not found
258
267
  .end(e.message);
268
+ }
259
269
  }
260
270
  }
261
271
 
@@ -277,9 +287,15 @@ async function patchManyFunc(req, res) {
277
287
  await _eventHooks.fireAfter(collection, 'PATCH', result);
278
288
  res.json(result);
279
289
  } catch (e) {
280
- res
290
+ if (e.schemaError) {
291
+ res
292
+ .status(400) // validator
293
+ .end(e);
294
+ } else {
295
+ res
281
296
  .status(404) // not found
282
297
  .end(e.message);
298
+ }
283
299
  }
284
300
  }
285
301
 
@@ -2,8 +2,21 @@ const debug = console.debug;
2
2
 
3
3
  export const validate = (schema, document) => {
4
4
  debug('Validate Yup', document, schema)
5
- if (schema == null) return true;
6
- return schema.validate(document)
5
+ return new Promise(async (resolve, reject) => {
6
+ if (schema == null) {
7
+ debug('Null validator')
8
+ resolve(true)
9
+ }
10
+ const valid = await schema.validate(document);
11
+
12
+ if (valid) {
13
+ debug('Validate ok', valid)
14
+ resolve(valid)
15
+ } else {
16
+ debug('Validate error', valid)
17
+ resolve(valid)
18
+ }
19
+ });
7
20
  }
8
21
 
9
22
  export const cast = (schema, document) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.2.11",
3
+ "version": "1.2.13",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",