Browse files

couchapp ulitity library added

  • Loading branch information...
1 parent aa7c95d commit 2f95a152190460ed5f91eca68a2cd2b1c8238f6d @yssk22 committed May 14, 2011
Showing with 79 additions and 3 deletions.
  1. +5 −2 apps/posts/app.js
  2. +20 −0 apps/posts/validate_doc_update.js
  3. +52 −0 lib/couchapp.js
  4. +2 −1 lib/sunrise.js
View
7 apps/posts/app.js
@@ -1,5 +1,5 @@
var path = require('path');
-var couchapp = require('couchapp');
+var couchapp = require('sunrise').couchapp;
var ddoc = {
_id: "_design/posts" ,
@@ -22,7 +22,10 @@ var ddoc = {
};
module.exports = ddoc;
-
+ddoc.lib = {
+ validator: require('sunrise').couchapp.validator
+};
+ddoc.validate_doc_update = require('./validate_doc_update');
ddoc.middleware = require('./middleware');
ddoc.views = require('./views');
View
20 apps/posts/validate_doc_update.js
@@ -0,0 +1,20 @@
+module.exports = function(newDoc, oldDoc, userCtx, secObj){
+ var v = (eval(this.lib.validator))(newDoc, oldDoc, userCtx, secObj);
+
+ if( newDoc._deleted ){
+ // on deleted
+ }else{
+ // on inserd or update
+ v.required('type',
+ 'created_at', 'updated_at',
+ 'created_by', 'updated_by');
+ v.unchanged('type',
+ 'created_at',
+ 'created_by');
+ v.dateFormat('created_at', 'updated_at');
+ }
+ if( newDoc.type == 'post' ){
+ v.required('title', 'content');
+ v.unchanged('created_by');
+ }
+};
View
52 lib/couchapp.js
@@ -0,0 +1,52 @@
+module.exports = require('couchapp');
+module.exports.validator = function(newDoc, oldDoc, userCtx, secObj){
+ var v = {};
+
+ v.forbidden = function(message){
+ throw({forbidden: message});
+ };
+
+ v.required = function(){
+ for (var i=0; i < arguments.length; i++) {
+ var field = arguments[i];
+ message = "The '"+field+"' field is required.";
+ if (typeof newDoc[field] == "undefined"){
+ v.forbidden(message);
+ }
+ log(field)
+ if(newDoc[field] == ""){
+ v.forbidden(message);
+ }
+ };
+ };
+
+ v.matches = function(field, regex, message) {
+ if (!newDoc[field].match(regex)) {
+ message = message || "Format of '"+field+"' field is invalid.";
+ v.forbidden(message);
+ }
+ };
+
+ v.unchanged = function() {
+ for (var i=0; i < arguments.length; i++) {
+ var field = arguments[i];
+ if (oldDoc){
+ var _old = JSON.stringify(oldDoc[field]);
+ var _new = JSON.stringify(newDoc[field]);
+ if( _old != _new ){
+ v.forbidden("You may not change the '"+field+"' field.");
+ }
+ }
+ }
+ };
+
+ v.dateFormat = function(field){
+ for (var i=0; i < arguments.length; i++) {
+ var val = JSON.stringify(newDoc[field]);
+ var message = "Sorry, '"+field+"'(value = " + val + ") is not a valid date format. Try: 2010-02-24T17:00:03.432Z";
+ v.matches(field, /\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}(\.\d*)?Z/, message);
+ }
+ };
+
+ return v;
+};
View
3 lib/sunrise.js
@@ -13,7 +13,8 @@ module.exports = {
appPaths: require('./app').paths,
raiseError: function raiseError(statusCode){
throw new HTTPError(statusCode);
- }
+ },
+ couchapp: require('./couchapp')
}
fs.readdirSync(__dirname + '/middleware').forEach(function(filename){

0 comments on commit 2f95a15

Please sign in to comment.