1 /*
  2  * Geddy JavaScript Web development framework
  3  * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the "License");
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  *         http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an "AS IS" BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  *
 17 */
 18 
 19 /*
 20  * Basic validators -- name is the field name, params is the entire params
 21  * collection (needed for stuff like password confirmation so it's possible
 22  * to compare with other field values, and the rule is the data for this
 23  * particular validation
 24  * Rules can look like this:
 25  * present: {qualifier: true, {message: 'Gotta be here'}}
 26  * length: {qualifier: {min: 2, max: 12}}
 27  * withFunction: {qualifier: function (s) { return true },
 28  *    message: 'Something is wrong'}
 29  */
 30 var validators = {
 31   present: function (name, val, params, rule) {
 32     if (!val) {
 33       return rule.message || 'Field "' + name + '" is required.';
 34     }
 35   },
 36 
 37   absent: function (name, val, params, rule) {
 38     if (val) {
 39       return rule.message || 'Field "' + name + '" must not be filled in.';
 40     }
 41   },
 42 
 43   confirmed: function (name, val, params, rule) {
 44     var qual = rule.qualifier;
 45     if (val != params[qual]) {
 46       return rule.message || 'Field "' + name + '" and field "' + qual +
 47           '" must match.';
 48     }
 49   },
 50 
 51   format: function (name, val, params, rule) {
 52     if (!rule.qualifier.test(val)) {
 53       return rule.message || 'Field "' + name + '" is not correctly formatted.';
 54     }
 55   },
 56 
 57   length: function (name, val, params, rule) {
 58     var qual = rule.qualifier;
 59     var err;
 60     if (!val) {
 61       return rule.message || 'Field "' + name + '" is required.';
 62     }
 63     if (typeof qual == 'number') {
 64       if (val.length != qual) {
 65         return rule.message || 'Field "' + name + '" must be ' + qual +
 66             ' characters long.';
 67       }
 68     }
 69     else {
 70       if (typeof qual.min == 'number' && val.length < qual.min) {
 71         return rule.message || 'Field "' + name + '" must be at least ' +
 72             qual.min + ' characters long.';
 73       }
 74       if (typeof qual.max == 'number' && val.length > qual.max) {
 75         return rule.message || 'Field "' + name + '" may not be more than ' +
 76             qual.max + ' characters long.';
 77  78       }
 79     }
 80   },
 81 
 82   withFunction: function (name, val, params, rule) {
 83     var func = rule.qualifier;
 84     if (typeof func != 'function') {
 85       throw new Error('withFunction validator for field "' + name +
 86           '" must be a function.');
 87     }
 88     if (!func(val, params)) {
 89       return rule.message || 'Field "' + name + '" is not valid.';
 90     }
 91   }
 92 };
 93 
 94 module.exports = validators;
 95