summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-08 21:50:59 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-08 21:50:59 -0400
commitee5a0ec0c43f673ee79a2fdcf16ea368ea385532 (patch)
tree8d808d90188e8a487681648059e904a5e8be3274
parent06ef2f2337dd349af160e2c50dbef76f68f406d5 (diff)
Add support for textarea data types
-rw-r--r--app/node_modules/okadminview/index.js2
-rw-r--r--app/node_modules/okschema/index.js51
-rw-r--r--app/node_modules/okschema/package.json4
-rw-r--r--examples/index.js2
-rw-r--r--themes/okadmin/templates/partials/inputs.liquid8
5 files changed, 60 insertions, 7 deletions
diff --git a/app/node_modules/okadminview/index.js b/app/node_modules/okadminview/index.js
index 42e7f36..1dcc0d6 100644
--- a/app/node_modules/okadminview/index.js
+++ b/app/node_modules/okadminview/index.js
@@ -186,7 +186,7 @@ function OKAdminView(options) {
if (!resource) {
errorHandler(req, res)(new Error('No such resource ' + type));
} else {
- // TODO Maybe should make metadata synchronous...
+ // TODO Prob should make metadata synchronous...
meta.get().then(function(metadata) {
var templateData = {
meta: metadata,
diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js
index d63f5db..51783a1 100644
--- a/app/node_modules/okschema/index.js
+++ b/app/node_modules/okschema/index.js
@@ -1,4 +1,27 @@
+var assign = require('object-assign');
var mschema = require('mschema');
+var v = require('validator');
+
+/**
+ * Custom types.
+ * To add a custom type, add a key with the type name and a value
+ * in the form {parent: {...}, assertValid: function(d) {...}}
+ * where parent is the base mschema spec and validate is a function
+ * accepting one data point and throwing an array of errors if invalid.
+ *
+ * Error array format is derived from mschema and is in the form
+ * [{property: x, constraint: x, actual: x, expected: x, message: x} ... ]
+ */
+var types = {
+ /**
+ * Larger text inputs. Currently just proxies to string type.
+ */
+ 'text': {
+ parent: {type: 'string'},
+ // Let parent handle validation
+ assertValid: function(s) {}
+ }
+}
/**
* OKSchema!
@@ -9,6 +32,20 @@ function OKSchema(spec) {
if (!(this instanceof OKSchema)) return new OKSchema(spec);
if (!spec)
throw new Error('No spec provided to OKSchema');
+ spec = assign({}, spec);
+ // Cache the mschema version of our spec
+ this._mschemaSpec = Object.keys(spec).reduce(function(cache, prop) {
+ // If custom type, return its parent spec
+ var type = spec[prop].type;
+ if (types[type]) {
+ cache[prop] = types[type].parent;
+ // Otherwise, it's already in mschema format
+ } else {
+ cache[prop] = spec[prop];
+ }
+ return cache;
+ }, {});
+
Object.defineProperty(this, 'spec', {
value: spec,
writable: false
@@ -16,17 +53,25 @@ function OKSchema(spec) {
}
OKSchema.prototype.assertValid = function(data) {
+ data = data || {};
+ var spec = this.spec;
+ // Run through custom validators, they'll throw if invalid
+ Object.keys(data).forEach(function(prop) {
+ var type = spec[prop].type;
+ if (types[type]) {
+ types[type].assertValid(data[prop]);
+ }
+ });
var result = mschema.validate(data, this.toMschema());
if (!result.valid)
throw result.errors;
};
/**
- * Return schema as an mschema object.
- * Currently no difference between the two.
+ * Return our custom spec as an mschema spec
*/
OKSchema.prototype.toMschema = function() {
- return this.spec;
+ return this._mschemaSpec;
};
module.exports = OKSchema;
diff --git a/app/node_modules/okschema/package.json b/app/node_modules/okschema/package.json
index 795f057..21214fa 100644
--- a/app/node_modules/okschema/package.json
+++ b/app/node_modules/okschema/package.json
@@ -9,6 +9,8 @@
"author": "OKFocus",
"license": "None",
"dependencies": {
- "mschema": "^0.5.5"
+ "mschema": "^0.5.5",
+ "object-assign": "^2.0.0",
+ "validator": "^3.37.0"
}
}
diff --git a/examples/index.js b/examples/index.js
index e55b717..6a07c4e 100644
--- a/examples/index.js
+++ b/examples/index.js
@@ -8,7 +8,7 @@ var app = okcms.createApp({
page: {
id: {type: 'string'},
title: {type: 'string'},
- body: {type: 'string'}
+ body: {type: 'text'}
},
bread: {
type: {type: 'string', id: true},
diff --git a/themes/okadmin/templates/partials/inputs.liquid b/themes/okadmin/templates/partials/inputs.liquid
index 4dd600d..2726473 100644
--- a/themes/okadmin/templates/partials/inputs.liquid
+++ b/themes/okadmin/templates/partials/inputs.liquid
@@ -4,13 +4,19 @@
{% assign type = spec.type %}
<div class="property">
+ <label for="{{name}}">{{name | capitalize}}</label>
{% if type == 'string' %}
- <label for="{{name}}">{{name | capitalize}}</label>
<input
{% if spec.disabled %}
disabled="true"
{% endif %}
name="{{name}}" type="text" value="{{spec.value}}">
+ {% else if type == 'text' %}
+ <textarea
+ {% if spec.disabled %}
+ disabled="true"
+ {% endif %}
+ name="{{name}}" rows="40" cols="80">{{spec.value}}</textarea>
{% else %}
<p><pre style="color: red">Admin template doesn't support '{{type}}' properties!</pre></p>
{% endif %}