expressa 2.0.11 → 2.0.12

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/util.js +16 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expressa",
3
- "version": "2.0.11",
3
+ "version": "2.0.12",
4
4
  "description": "API framework using JSON schemas",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/thomas4019/expressa",
@@ -17,7 +17,7 @@
17
17
  "jfs": "^0.3.0",
18
18
  "jsonwebtoken": "^9.0.0",
19
19
  "mongo-query": "^0.5.7",
20
- "mongo-query-to-postgres-jsonb": "^0.2.9",
20
+ "mongo-query-to-postgres-jsonb": "^0.2.16",
21
21
  "mongo-querystring": "4.1.1",
22
22
  "mongodb": "^3.5.7",
23
23
  "on-finished": "^2.3.0",
package/util.js CHANGED
@@ -37,8 +37,11 @@ const schemas = {}
37
37
  const schemaValidators = {}
38
38
 
39
39
  exports.addSchema = function(collection, schema) {
40
+ schema.$id = collection
41
+ ajv.removeSchema(collection)
40
42
  schemas[collection] = schema
41
43
  schemaValidators[collection] = ajv.compile(schema)
44
+ delete schema.$id
42
45
  }
43
46
 
44
47
  exports.validateSchema = function(collection, doc) {
@@ -53,6 +56,19 @@ exports.validateSchema = function(collection, doc) {
53
56
  return true
54
57
  }
55
58
 
59
+ exports.validateSchemaProperty = function(collection, path, value) {
60
+ if (!schemaValidators[collection]) {
61
+ console.error(`missing schema validator for collection ${collection}`)
62
+ return true
63
+ }
64
+ const $ref = `${collection}#/properties/${path.split('.').join('/properties/')}`
65
+ const isValid = ajv.validate({ $ref }, value)
66
+ if (!isValid) {
67
+ throw new ApiError(400, ajv.errors.map((err) => `${err.instancePath} ${err.message}: ${JSON.stringify(err.params)}`).join(', '))
68
+ }
69
+ return true
70
+ }
71
+
56
72
  exports.getSchemaProperty = function(collection, path) {
57
73
  const parts = path.split('.')
58
74
  let obj = schemas[collection]