accurized
Version:
Verify the validity and accuracy of data as it enters your system.
151 lines (133 loc) • 4.17 kB
JavaScript
(function() {
var Field, func, name, ref, ref1, ref2, validator,
slice = [].slice;
validator = require('./validator');
Field = (function() {
Field.transformer = function(name, func) {
return this.prototype[name] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
this.queue.transformers.push({
func: func,
args: args
});
return this;
};
};
Field.validator = function(name, func) {
return this.prototype[name] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
if (!this.queue.validators.length) {
this.required.message = this.msg;
}
this.queue.validators.push({
func: func,
args: args,
message: this.msg
});
return this;
};
};
Field.conversion = function(name, func) {
return (this.conversions != null ? this.conversions : this.conversions = {})[name] = func;
};
function Field(msg) {
this.msg = msg;
this.queue = {
transformers: [],
validators: []
};
this.required();
}
Field.prototype.optional = function() {
this.req = false;
return this;
};
Field.prototype.required = function() {
var ref;
this.req = (ref = this.msg) != null ? ref : true;
return this;
};
Field.prototype.message = function(message) {
if (message != null) {
this.msg = message;
}
return this;
};
Field.prototype.verify = function(custom) {
return this.as('string', custom);
};
Field.prototype.as = function(type, custom) {
var delegate;
if (custom == null) {
custom = function(value, pass, fail) {
return pass(value);
};
}
if (custom.length === 2) {
delegate = custom;
custom = function(value, pass, fail) {
var failed;
failed = false;
delegate(value, function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
failed = true;
return fail.apply(null, args);
});
if (!failed) {
return pass(value);
}
};
}
return (function(_this) {
return function(value, pass, fail) {
var args, func, i, j, len, len1, message, ref, ref1, ref2, ref3, ref4;
if (value === (void 0) || value === null || value === '') {
if (value === null || _this.req) {
fail(_this.req || _this.msg);
} else {
pass();
}
return;
}
ref = _this.queue.transformers;
for (i = 0, len = ref.length; i < len; i++) {
ref1 = ref[i], func = ref1.func, args = ref1.args;
value = func.apply(null, [value].concat(slice.call(args)));
}
ref2 = _this.queue.validators;
for (j = 0, len1 = ref2.length; j < len1; j++) {
ref3 = ref2[j], func = ref3.func, args = ref3.args, message = ref3.message;
if (!func.apply(null, [value].concat(slice.call(args)))) {
return fail(message);
}
}
return custom(((ref4 = _this.constructor.conversions[type]) != null ? ref4 : _this.constructor.conversions.identity)(value), pass, function() {
var args, message;
message = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return fail.apply(null, [message != null ? message : _this.msg].concat(slice.call(args)));
});
};
})(this);
};
return Field;
})();
ref = validator.transformers;
for (name in ref) {
func = ref[name];
Field.transformer(name, func);
}
ref1 = validator.validators;
for (name in ref1) {
func = ref1[name];
Field.validator(name, func);
}
ref2 = validator.conversions;
for (name in ref2) {
func = ref2[name];
Field.conversion(name, func);
}
module.exports = Field;
}).call(this);