From 19edeb18ba3d68fd93f96af4f16059f81cb97621 Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Tue, 1 Jul 2014 17:25:24 +0200 Subject: [PATCH] Implemented simple validation in FoxxModel.set --- js/server/modules/org/arangodb/foxx/model.js | 46 +++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/js/server/modules/org/arangodb/foxx/model.js b/js/server/modules/org/arangodb/foxx/model.js index a38872b4e5..04c16971b8 100644 --- a/js/server/modules/org/arangodb/foxx/model.js +++ b/js/server/modules/org/arangodb/foxx/model.js @@ -186,6 +186,11 @@ _.extend(Model.prototype, { get: function (attributeName) { 'use strict'; + var attrs = this.constructor.attributes; + if (attrs && Object.keys(attrs).indexOf(attributeName) === -1) { + throw new Error("Unknown attribute: " + attributeName); + } + return this.attributes[attributeName]; }, @@ -213,12 +218,43 @@ _.extend(Model.prototype, { set: function (attributeName, value) { 'use strict'; - if (is.object(attributeName)) { - _.extend(this.attributes, attributeName); - } else { - this.attributes[attributeName] = value; + var constructorAttributes = this.constructor.attributes; + var attributes = attributeName; + + if (!is.object(attributeName)) { + attributes = {}; + attributes[attributeName] = value; } - this.whitelistedAttributes = whitelistProperties(this.attributes, this.constructor.attributes); + + if (constructorAttributes) { + Object.keys(attributes).forEach(function(key) { + if (!constructorAttributes.hasOwnProperty(key)) { + throw new Error("Unknown attribute: " + key); + } + var value = attributes[key]; + if (value === undefined || value === null) { + return; + } + var actualType = typeof value; + var expectedType = constructorAttributes[key].type; + if (Array.isArray(value)) { + actualType = "array"; + } + if (expectedType === "integer" && actualType === "number") { + actualType = (Math.floor(value) === value ? "integer" : "number"); + } + if (actualType !== expectedType) { + throw new Error( + "Invalid value for \"" + + key + "\": Expected " + expectedType + + " instead of " + actualType + "!" + ); + } + }); + } + + _.extend(this.attributes, attributes); + this.whitelistedAttributes = whitelistProperties(this.attributes, constructorAttributes); }, ////////////////////////////////////////////////////////////////////////////////