summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-03-28 14:39:44 -0400
committerJules Laplace <jules@okfoc.us>2016-03-28 14:53:12 -0400
commitea9a94f3e1f980153588da1aee3fc7e456e292b5 (patch)
tree607e647f646e500b134573a091a1499af522b00c /app
parent8898bbe48285c7f9f11760ef420cca43d683d5e0 (diff)
okschema: replace array-type data with empty list if unspecified (due to constraint of querystring body-parser)v0.1.19
Diffstat (limited to 'app')
-rw-r--r--app/node_modules/okschema/index.js74
1 files changed, 51 insertions, 23 deletions
diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js
index 82aa13f..cf041fb 100644
--- a/app/node_modules/okschema/index.js
+++ b/app/node_modules/okschema/index.js
@@ -41,43 +41,25 @@ var types = {
}
},
'captioned-image-list': {
+ isArray: true,
parent: [{
uri: { type: 'string' }, // TODO Implement URI type
caption: { type: 'string' }
}],
- assertValid: function(spec, value) {
- var message;
- var actual;
- if (!value || !value.length) {
- throw [{
- message: 'Not an array',
- expected: JSON.stringify(this.parent),
- actual: value
- }];
- }
- }
+ assertValid: function(spec, value) {}
},
// Special type for resource meta information
'meta': {
parent: 'string',
assertValid: function(spec, value) {}
},
- 'tag-list': {
+ 'link-list': {
+ isArray: true,
parent: [{
uri: { type: 'string' },
text: { type: 'string' }
}],
- assertValid: function(spec, value) {
- var message;
- var actual;
- if (!value || !value.length) {
- throw [{
- message: 'Not an array',
- expected: JSON.stringify(this.parent),
- actual: value
- }];
- }
- }
+ assertValid: function(spec, value) {}
},
'date': {
parent: 'string',
@@ -90,8 +72,38 @@ var types = {
'foreign-key': {
parent: 'enum',
assertValid: function(spec, value) {}
+ },
+ 'media-list': {
+ isArray: true,
+ parent: [],
+ assertValid: function(spec, value) {}
+ },
+ 'double-captioned-image-list': {
+ isArray: true,
+ parent: [],
+ assertValid: function(spec, value) {}
+ },
+ 'triple-captioned-image-list': {
+ isArray: true,
+ parent: [],
+ assertValid: function(spec, value) {}
+ },
+}
+
+/*
+function checkArrayLength (spec, value) {
+ var message;
+ var actual;
+ if (!value || !value.length) {
+ throw [{
+ message: 'Not an array',
+ expected: JSON.stringify(this.parent),
+ actual: value
+ }];
}
}
+*/
+
/**
* OKSchema!
@@ -162,9 +174,25 @@ function OKSchema(spec) {
});
}
+OKSchema.prototype.checkDataForMissingArrays = function(data) {
+ data = data || {};
+ var spec = this.spec;
+
+ // The qs body-parser module does not have a way to represent
+ // empty lists. If you delete all elements from a list,
+ // check against the spec so we know to replace with an empty list.
+ Object.keys(spec).forEach(function(prop){
+ var type = spec[prop].type;
+ if (types[type] && types[type].isArray && ! data[prop]) {
+ data[prop] = []
+ }
+ })
+}
+
OKSchema.prototype.assertValid = function(data) {
data = data || {};
var spec = this.spec;
+ this.checkDataForMissingArrays(data)
// Run through custom validators, they'll throw if invalid
Object.keys(data).forEach(function(prop) {
var type = spec[prop].type;