From 5abbdf600396144fbbe32b97e83beaabf6ed5c39 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 21:40:38 -0400 Subject: Support autoincrement and custom ID field in OKSchema --- app/node_modules/okschema/index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'app/node_modules/okschema/index.js') diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 4b215d1..1528eab 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -69,8 +69,9 @@ function OKSchema(spec) { if (!spec) throw new Error('No spec provided to OKSchema'); spec = cloneDeep(spec); + var specKeys = Object.keys(spec); // Cache the mschema version of our spec - this._mschemaSpec = Object.keys(spec).reduce(function(cache, prop) { + this._mschemaSpec = specKeys.reduce(function(cache, prop) { // If custom type, return its parent spec var type = spec[prop].type; if (types[type]) { @@ -82,12 +83,45 @@ function OKSchema(spec) { return cache; }, {}); + // Find ID field + var idField; + specKeys.every(function(prop) { + if (prop === 'id' || spec[prop].id) { + idField = prop; + return false; + } else { + return true; + } + }); + + // Register autoincrement fields + // NOTE Does not work for nested fields + var autoIncrementFields = specKeys.reduce(function(arr, prop) { + if (spec[prop] === 'autoincrement') { + arr.push(prop); + } + return arr; + }, []); + Object.defineProperty(this, 'spec', { get: function() { return cloneDeep(spec); }, enumerable: true }); + + Object.defineProperty(this, 'idField', { + value: idField, + writable: true, + enumerable: true + }); + + Object.defineProperty(this, 'autoIncrementFields',{ + get: function() { + return cloneDeep(autoIncrementFields); + }, + enumerable: true + }); } OKSchema.prototype.assertValid = function(data) { -- cgit v1.2.3-70-g09d2