diff options
| -rw-r--r-- | app/node_modules/okschema/index.js | 74 | ||||
| -rw-r--r-- | examples/db.json | 23 |
2 files changed, 68 insertions, 29 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; diff --git a/examples/db.json b/examples/db.json index 4319237..5e10d60 100644 --- a/examples/db.json +++ b/examples/db.json @@ -92,6 +92,22 @@ }, "__index": 3, "id": "croissant" + }, + { + "type": "Test", + "title": "Testing", + "description": "", + "color": "red", + "video": { + "url": "", + "type": "", + "token": "", + "title": "", + "thumb": "" + }, + "__index": "5", + "dateCreated": "Mon, 28 Mar 2016 18:49:32 GMT", + "images": [] } ], "page": [ @@ -100,12 +116,7 @@ "body": "Just a small bakery", "id": "about", "__index": "1", - "links": [ - { - "text": "asdf2", - "uri": "asdf" - } - ], + "links": [], "dateCreated": "" }, { |
