summaryrefslogtreecommitdiff
path: root/app/node_modules/okschema/index.js
diff options
context:
space:
mode:
authorjulie lala <jules@okfoc.us>2015-04-11 11:42:27 -0400
committerjulie lala <jules@okfoc.us>2015-04-11 11:42:27 -0400
commit65c1c6541f98eb863f9a19533f4bdb4bd9e38514 (patch)
tree84cbe0902cc355b9e1556c346f217d1a4cafd552 /app/node_modules/okschema/index.js
parent0b6afc1d5aa8a2f33b1c21e04a10c3eaa43870c1 (diff)
parent2b95bcf414f02551a384ef2020958e90431814dd (diff)
merge
Diffstat (limited to 'app/node_modules/okschema/index.js')
-rw-r--r--app/node_modules/okschema/index.js42
1 files changed, 41 insertions, 1 deletions
diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js
index 4b215d1..d53ed7b 100644
--- a/app/node_modules/okschema/index.js
+++ b/app/node_modules/okschema/index.js
@@ -56,6 +56,11 @@ var types = {
}];
}
}
+ },
+ // Special type for resource meta information
+ 'meta': {
+ parent: 'string',
+ assertValid: function(spec, value) {}
}
}
@@ -69,8 +74,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 +88,46 @@ 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) {
+ var specProp = spec[prop];
+ if (specProp.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) {