-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlisteners_validation.js
43 lines (37 loc) · 1.55 KB
/
listeners_validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const util = require('./util')
function addImplicitFields (schema) {
schema['properties'] ??= {}
schema['properties']['meta'] ??= { type: 'object' }
schema['properties']['meta']['properties'] ??= {}
schema['properties']['meta']['properties']['created'] ??= { type: 'string' }
schema['properties']['meta']['properties']['updated'] ??= { type: 'string' }
schema['properties']['meta']['properties']['owner'] ??= { type: 'string' }
schema['properties']['meta']['properties']['owner_collection'] ??= { type: 'string' }
schema['properties']['_id'] ??= { type: 'string' }
return schema
}
module.exports = async function (api) {
// Load all validators
const collections = await api.db.collection.all()
collections.forEach((collection) => {
const schema = addImplicitFields(collection.schema)
util.addSchema(collection._id, schema)
})
// Load new and update validators as necessary
api.addCollectionListener('changed', 'collection', function updateSchemaValidators (req, collection, data) {
const schema = addImplicitFields(data.schema)
util.addSchema(data._id, schema)
})
api.addCollectionListener('get', ['collection', 'schemas'], function ensureIdAdded (req, collection, data) {
if (data.schema) {
data.schema = addImplicitFields(data.schema)
}
})
api.addListener(['put', 'post'], function matchesSchema (req, collection, data) {
// TODO (switch to check if "installed" after install tests are done.
if (!req.settings.enforce_permissions) {
return
}
util.validateSchema(collection, data)
})
}