initialized kettu on gitorious
[kettu:kettu.git] / js / models / validator.js
1 var Validator = function() {};
2
3 Validator.prototype = {
4   has_errors: function() {
5     return this.errors.length > 0;
6   },
7   
8   validate: function(object) {
9     this.errors = [];
10     for(validation in this.schema) {
11       switch(validation) {
12         case 'presence_of':
13           this.validate_presence_of(object, this.schema[validation]);
14           break;
15         case 'numericality_of':
16           this.validate_numericality_of(object, this.schema[validation]);
17           break;
18         case 'inclusion_of':
19           this.validate_inclusion_of(object, this.schema[validation]);
20           break;
21       }
22     }
23   },
24   
25   validate_presence_of: function(object, fields) {
26     var context = this;
27     fields = this.arrayfy_fields(fields);
28
29     $.each(fields, function() {
30       var field = object[this];
31       if(typeof(field) == 'undefined') {
32         context.errors.push({'field': this, 'message': context.error_messages['presence_of']})
33       }      
34     });
35   },
36   
37   validate_numericality_of: function(object, fields) {
38     var context = this;
39     fields = this.arrayfy_fields(fields);
40
41     $.each(fields, function() {
42       if(this == '[object Object]') {
43         if(object[this['field']] == undefined) {
44           context.errors.push({'field': this['field'], 'message': context.error_messages['numericality_of']});
45         } else {
46           var field = object[this['field']].toString();
47           if(!field.match(/^\d+$/) || field > this['max']) {
48             context.errors.push({'field': this['field'], 'message': context.error_messages['numericality_of']});
49           }          
50         }
51       } else {
52         if(object[this] == undefined) {
53           context.errors.push({'field': this, 'message': context.error_messages['numericality_of']});
54         } else {
55           var field = object[this].toString();
56           if(!field.match(/^\d+$/)) {
57             context.errors.push({'field': this, 'message': context.error_messages['numericality_of']});
58           }          
59         }
60       }
61     });
62   },
63   
64   validate_inclusion_of: function(object, field) {
65     var content = object[field['field']];
66     var included = false;
67
68     $.each(field['in'], function() {
69       if(this == content) {
70         included = true;
71       }
72     });
73     if(!included) {
74       this.errors.push({'field': field['field'], 'message': this.error_messages['inclusion_of']});
75     }
76   },
77   
78   arrayfy_fields: function(fields) {
79     if(!$.isArray(fields)) {
80       return [fields];
81     } else {
82       return fields;
83     }
84   },
85   
86   schema: {},
87   errors: [],
88   
89   error_messages: { 
90     presence_of: 'should not be empty', 
91     numericality_of: 'is not a valid number',
92     inclusion_of: 'is not in the list of a valid values'
93   }
94 };