Skip to content

Commit

Permalink
Implemented validation and started writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Surdu Nicolae committed Apr 25, 2015
1 parent 79a3d84 commit 6edb20e
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 22 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab
30 changes: 30 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),

jasmine_nodejs: {
options: {
specNameSuffix: '-spec.js',
},

dist: {
specs:[
"specs/**-spec.js"
]
}
},

watch: {
files: ["src/**/*.js", "specs/**/*.js", "*.js"],
tasks: "test",
options: {
debounceDelay: 1000,
}
}
});

grunt.loadNpmTasks('grunt-jasmine-nodejs');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask("test", ["jasmine_nodejs"]);
};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Formless

Documentation comming soon
42 changes: 38 additions & 4 deletions fields/baseField.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function nameToLabel(name) {
return parts.join(" ")
}


function requiredValidator(callback) {
if (this.required && !this.value) {
callback("This field is required");
}
else {
callback(true);
}
}

function BaseField() {
}

Expand All @@ -28,13 +38,37 @@ BaseField.prototype = {
this.name = fieldInfo.name;
this.required = fieldInfo.required;
this.placeholder = fieldInfo.placeholder;

this.validators = [requiredValidator];
},

validate: function () {
if (this.required) {
return !!this.value || "This field is required";
addValidator: function (validator) {
this.validators.push(validator);
return this;
},

validate: function (callback) {
if (typeof(callback) !== "function") {
throw Error("'" + callback + "' is not a function");
}

var remainingValidations = this.validators.length;
var errors = [];

for (var f = 0; f < this.validators.length; f++) {
var validate = this.validators[f];

validate.call(this, function (error) {
remainingValidations--;
if (error !== true) {
errors.push(error);
}

if (remainingValidations === 0) {
callback(errors);
}
})
}
return true;
},

render: function () {
Expand Down
34 changes: 23 additions & 11 deletions formless.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var typeMap = {

function Formless(model) {
this.fields = {};
this.validators = [];


if (modelParser) {
model = modelParser.parse(model);
Expand Down Expand Up @@ -55,22 +57,32 @@ Formless.prototype = {
}
},

isValid: function () {
var isValid = true;
validate: function (callback) {
if (typeof(callback) !== "function") {
throw Error("'" + callback + "' is not a function");
}

var errors = {};
var remainingValidations = Object.keys(this.fields).length;
remainingValidations += this.validators.length;

for (var fieldName in this.fields) {
var field = this.fields[fieldName];
var result = field.validate();

if (result !== true) {
if (typeof result === "string") {
field.error = result;
}
isValid = false;
}
(function (field) {
field.validate(function (fieldErrors) {
remainingValidations--;
if (fieldErrors) {
field.errors = fieldErrors;
errors[field.name] = fieldErrors;
}

if (remainingValidations === 0) {
callback.call(this, errors);
}
});
})(field)
}

return isValid;
},

render: function () {
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
{
"name": "Formless",
"version": "0.0.0",
"description": "",
"description": "Form handling solution for node.js",
"main": "formless.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "grunt watch",
"test": "grunt test"
},
"author": "",
"repository": {
"type": "git",
"url": "https://github.com/Formless-js/formless"
},
"author": "Surdu Nicolae",
"license": "MIT",
"dependencies": {
"node.extend": "^1.1.3"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-watch": "^0.6.1",
"grunt-jasmine-nodejs": "^1.3.0"
}
}
19 changes: 19 additions & 0 deletions specs/assets/basicForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = [
{
name: "firstName",
type: "text",
required: true
},

{
name: "lastName",
type: "text",
required: true
},

{
name: "nickname",
type: "text",
required: false
},
]
27 changes: 27 additions & 0 deletions specs/form-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var Form = require("../formless");

var basicForm = require("./assets/basicForm");

describe("form", function () {
var form;

beforeEach(function () {
form = new Form(basicForm)
});

it("should initialize with basic JSON", function () {
expect(Object.keys(form.fields).length).toEqual(3);
});

it("should validate required fields", function (done) {
form.fill({
firstName: "Surdu",
nickname: "nick"
});

form.validate(function (err) {
expect(err.lastName).not.toBeUndefined();
done();
});
})
})
8 changes: 4 additions & 4 deletions templates/bootstrap/textField.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="form-group{% if error %} has-error{% endif %}">
<div class="form-group{% if errors.length > 0 %} has-error{% endif %}">
<label class="control-label" for="{{name}}">{{label}}</label>
<input type="text"
class="form-control"
id="{{name}}"
name="{{name}}"
value="{{value}}"
{% if placeholder %}placeholder="{{placeholder}}"{% endif %}
{% if required %}required{% endif %}>
{% if error %}
> <!-- {% if required %}required{% endif %} -->
{% for error in errors %}
<span class="help-block">{{error}}</span>
{% endif%}
{% endfor%}
</div>

0 comments on commit 6edb20e

Please sign in to comment.