diff --git a/js/common/tests/shell-foxx-model.js b/js/common/tests/shell-foxx-model.js index aa705d9e7a..749f7cef05 100644 --- a/js/common/tests/shell-foxx-model.js +++ b/js/common/tests/shell-foxx-model.js @@ -97,6 +97,71 @@ function ModelSpec () { }; } +function JSONSchema () { + var FoxxModel, TestModel, jsonSchema, instance; + + return { + setUp: function () { + FoxxModel = require('org/arangodb/foxx/model').Model; + }, + + testGetEmptyJSONSchema: function () { + TestModel = FoxxModel.extend({}); + jsonSchema = TestModel.toJSONSchema("myname"); + assertEqual(jsonSchema.id, "myname"); + assertEqual(jsonSchema.required, []); + assertEqual(jsonSchema.properties, {}); + }, + + testAttributesOfAPlainModel: function () { + attributes = {a: 1, b: 2}; + TestModel = FoxxModel.extend({}); + instance = new TestModel(attributes); + assertEqual(instance.attributes, attributes); + }, + + testAddOptionalAttributeToJSONSchemaInLongForm: function () { + TestModel = FoxxModel.extend({}, { + attributes: { + x: { type: "string" } + } + }); + + jsonSchema = TestModel.toJSONSchema("myname"); + assertEqual(jsonSchema.id, "myname"); + assertEqual(jsonSchema.required, []); + assertEqual(jsonSchema.properties.x.type, "string"); + }, + + testAddOptionalAttributeToJSONSchemaInShortForm: function () { + TestModel = FoxxModel.extend({}, { + attributes: { + x: "string" + } + }); + + jsonSchema = TestModel.toJSONSchema("myname"); + assertEqual(jsonSchema.id, "myname"); + assertEqual(jsonSchema.required, []); + assertEqual(jsonSchema.properties.x.type, "string"); + }, + + testAddRequiredAttributeToJSONSchema: function () { + TestModel = FoxxModel.extend({}, { + attributes: { + x: { type: "string", required: true } + } + }); + + jsonSchema = TestModel.toJSONSchema("myname"); + assertEqual(jsonSchema.id, "myname"); + assertEqual(jsonSchema.properties.x.type, "string"); + assertEqual(jsonSchema.required, ["x"]); + } + }; +} + jsunity.run(ModelSpec); +jsunity.run(JSONSchema); return jsunity.done(); diff --git a/js/server/modules/org/arangodb/foxx/model.js b/js/server/modules/org/arangodb/foxx/model.js index e8b01decbb..71f9c27827 100644 --- a/js/server/modules/org/arangodb/foxx/model.js +++ b/js/server/modules/org/arangodb/foxx/model.js @@ -31,7 +31,9 @@ var Model, _ = require("underscore"), is = require("org/arangodb/is"), - backbone_helpers = require("backbone"); + backbone_helpers = require("backbone"), + parseAttributes, + parseRequiredAttributes; // ----------------------------------------------------------------------------- // --SECTION-- Model @@ -65,8 +67,56 @@ Model = function (attributes) { this.attributes = attributes || {}; }; -_.extend(Model.prototype, { +parseAttributes = function (rawAttributes) { + 'use strict'; + var properties = {}; + _.each(rawAttributes, function (value, key) { + if (_.isString(value)) { + properties[key] = { + type: value + }; + } else { + properties[key] = { + type: value.type + }; + } + }); + + return properties; +}; + +parseRequiredAttributes = function (rawAttributes) { + 'use strict'; + var requiredProperties = []; + + _.each(rawAttributes, function (value, key) { + if (_.isObject(value) && value.required) { + requiredProperties.push(key); + } + }); + + return requiredProperties; +}; + +// "Class" Properties +_.extend(Model, { + // TODO: Docs + + toJSONSchema: function (id) { + 'use strict'; + var attributes = this.attributes; + + return { + id: id, + required: parseRequiredAttributes(attributes), + properties: parseAttributes(attributes) + }; + }, +}); + +// Instance Properties +_.extend(Model.prototype, { //////////////////////////////////////////////////////////////////////////////// /// @fn JSF_foxx_model_get /// @brief Get the value of an attribute