diff options
Diffstat (limited to 'app/node_modules/okschema/index.js')
| -rw-r--r-- | app/node_modules/okschema/index.js | 36 |
1 files changed, 35 insertions, 1 deletions
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) { |
