Skip to content

Commit

Permalink
add some validation
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftb committed Aug 28, 2010
1 parent 00a07ac commit 2f6b96d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
24 changes: 16 additions & 8 deletions couch.js
Expand Up @@ -6,6 +6,8 @@ sys.puts('Got c => ' + sys.inspect(c));
sys.puts('Databases => '+c.databases());
var db = c.database('maprejuice');
sys.puts('Got db: '+db.name);

/*
db.exists(function (err, res) {
sys.puts('db.exists => err = '+sys.inspect(err));
sys.puts('db.exists => res = '+sys.inspect(res));
Expand All @@ -22,18 +24,13 @@ db.exists(function (err, res) {
// sys.p(doc);
//});

///*
/*
db.get('db8b3af81b102c450a8440fe870006c8', function (err, doc) {
sys.puts("db.get => err = "+err);
sys.puts("db.get => doc = "+doc);
});
sys.puts('Got p');
db.view('problems/all', function (err, res) {
res.forEach(function (row) {
sys.puts(row.name + " is a problem");
});
});
/*
db.insert({
name: 'First Problem',
Expand All @@ -51,8 +48,19 @@ db.insert({
sys.puts('Set p');
//*/

db.get('problem1', function (err, doc) {
sys.puts('db.get(problem1) => doc = '+doc);
db.insert('_design/problems', {
find_nulls: {
map: function (doc) {
if (!doc.name) { emit(null, doc); }
}
}
});

db.get('problems/find_nulls', function (err, rowSet) {
for (var i in rowSet) {
sys.puts(sys.inspect(rowSet[i]));
}
});

sys.puts('Got p');
//*/
18 changes: 18 additions & 0 deletions public/css/styles.css
Expand Up @@ -75,6 +75,21 @@ body {
font-size: 12px;
padding: 20px;
}
.message {
background: #eee;
border: 1px solid blue;
font-weight: bold;
margin-bottom: 20px;
padding: 20px;
}
.message ul {}
.message ul li {
padding: 5px 0;
}
.message.errors {
background: pink;
border: 1px solid red;
}

/** =FORM */
form { }
Expand All @@ -83,6 +98,9 @@ form { }
padding: 5px 0 20px;
position: relative;
}
form .form-field.error {
background: pink;
}
form .form-field .step {
background: #999;
color: #eee;
Expand Down
47 changes: 40 additions & 7 deletions server.js
Expand Up @@ -2,7 +2,8 @@ var express = require('express'),
sys = require('sys'),
fs = require('fs'),
listenPort = 3000,
app = express.createServer();
app = express.createServer(),
_ = require('./lib/underscore')._;

var cradle = require('cradle'),
c = new(cradle.Connection)('maprejuice.couchone.com'),
Expand Down Expand Up @@ -68,6 +69,25 @@ var Problem = function(opts) {
this.reduce_function = opts.reduce_function;
this.data = opts.data || {};
this.type = 'problem';
this.errors = [];
};
Problem.prototype.validate = function() {
if (!this.name || this.name.trim() == '') {
this.errors.push({field: 'name', message: "Problem Name can't be blank"})
}
if (!this.map_function || this.map_function.trim() == '') {
this.errors.push({field: 'map_function', message: "Map Function can't be blank"})
}
if (!this.reduce_function || this.reduce_function.trim() == '') {
this.errors.push({field: 'reduce_function', message: "Reduce Function can't be blank"})
}
return !this.has_errors();
};
Problem.prototype.has_errors = function() {
return this.errors.length != 0;
};
Problem.prototype.has_error = function(field_name) {
return _.any(this.errors, function (item) { return item.field == field_name; }, field_name);
};

/* Redirect to correct URL on every request */
Expand Down Expand Up @@ -102,7 +122,11 @@ app.get('/', function(req, res) {
/* Form to create problem */
app.get('/problem', function(req, res) {
// TODO create object here
res.render('problem/new');
res.render('problem/new', {
locals: {
problem: new Problem({})
}
});
});

/* Get a specific problem */
Expand All @@ -119,12 +143,21 @@ app.get('/problem/:id', function(req, resp) {
});

/* This is where the problem is actually created */
app.post('/problem', function(req, res) {
app.post('/problem', function(req, resp) {
// TODO sanitize the shit out of this. Make sure it's valid js etc
var p = new Problem(req.body); // hack for now, we'll sanitize and split up data later
db.insert(p, function (err, result) {
res.redirect('/problem/' + result.id);
});
var p = new Problem(req.body);

if (p.validate()) {
db.insert(p, function (err, result) {
resp.redirect('/problem/' + result.id);
});
} else {
resp.render('problem/new', {
locals: {
problem: p
}
});
}
});


Expand Down
15 changes: 11 additions & 4 deletions views/problem/new.jade
@@ -1,23 +1,30 @@
- if (problem && problem.has_errors())
.message.errors
ul
- for (var i in problem.errors)
- error = problem.errors[i]
li #{error.message}

form(action="/problem", method="post")
.form-field
.form-field(class=(problem && problem.has_error('name') ? "error" : ""))
.step 1
label(for="problem_name") Problem Name
input(type="text", name="name", id="problem_name")
.instructions This is just for your reference later

.form-field
.form-field(class=(problem && problem.has_error('map_function') ? " error" : ""))
.step 2
label(for="problem_map_algorithm") Map Algorithm
textarea(cols="3", type="text", name="map_function", id="problem_algorithm_map_function")
.instructions Should look like function map(key, value) { ... }

.form-field
.form-field(class=(problem && problem.has_error('reduce_function') ? " error" : ""))
.step 3
label(for="problem_reduce_algorithm") Reduce Algorithm
textarea(cols="3", type="text", name="reduce_function", id="problem_algorithm_reduce_function")
.instructions Should look like function reduce(key, values) { ... }

.form-field
.form-field(class=(problem && problem.has_error('data') ? " error" : ""))
.step 4
label(for="problem_inputs") Inputs
textarea(cols="3", type="text", name="data", id="problem_inputs", disabled="disabled")
Expand Down

0 comments on commit 2f6b96d

Please sign in to comment.