This repository was archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_doc_update.js
More file actions
45 lines (36 loc) · 1.34 KB
/
validate_doc_update.js
File metadata and controls
45 lines (36 loc) · 1.34 KB
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
44
45
function(newDoc, oldDoc, userCtx) {
// Don't validate documents being deleted
if (newDoc._deleted) {
return;
}
// We require a schema to do validation
if (!newDoc.schema) {
throw({forbidden : 'The document schema is missing.'});
}
// ...and that schema must exist in the couchapp
if (!this.schema.hasOwnProperty(newDoc.schema)) {
throw({forbidden : 'There is no schema for: ' + newDoc.schema});
}
// Comment this out if you are throwing an admin party.
// This check simply enforces that the author is who they claim to be
//if (userCtx.name !== newDoc.author) {
// throw({forbidden: "Author must be the current user"});
//}
var
JSV = require("lib/jsv").JSV,
env = JSV.createEnvironment('json-schema-draft-03'),
report = env.validate(newDoc, this.schema[newDoc.schema]),
property;
if (report.errors.length) {
// Can only throw one error at a time (without stringifying)
// See issue: COUCHDB-1635
// https://issues.apache.org/jira/browse/COUCHDB-1635
property = report.errors[0].uri.split('/')[1];
if (property) {
error = report.errors[0].message + " : " + property;
} else {
error = report.errors[0].message;
}
throw({forbidden: error});
}
}