expressa 2.0.10 → 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.
- package/package.json +2 -2
- package/util.js +18 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expressa",
|
|
3
|
-
"version": "2.0.
|
|
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.
|
|
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]
|
|
@@ -203,11 +219,11 @@ exports.mongoToPostgres = mongoToPostgres
|
|
|
203
219
|
// paging requires orderby to include a field that is known to be unique and constant
|
|
204
220
|
// to ensure consistent results from database with offset and limit. Chosen field should
|
|
205
221
|
// also represent a preferred ordering when doing final sort
|
|
206
|
-
exports.orderByForPagedRequests = function(orderby, finalSortField = 'meta.created') {
|
|
222
|
+
exports.orderByForPagedRequests = function(orderby, finalSortField = 'meta.created', finalSortDirection = 1) {
|
|
207
223
|
orderby = exports.normalizeOrderBy(orderby || {})
|
|
208
224
|
const allFields = orderby.map(([field]) => field)
|
|
209
225
|
if (!allFields.includes(finalSortField)) {
|
|
210
|
-
orderby.push([finalSortField,
|
|
226
|
+
orderby.push([finalSortField, finalSortDirection])
|
|
211
227
|
}
|
|
212
228
|
return orderby
|
|
213
229
|
}
|