forked from 1602/jugglingdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
418 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
exports.Schema = require('./lib/schema'); | ||
exports.AbstractClass = require('./lib/abstract-class'); | ||
exports.Validatable = require('./lib/validatable'); | ||
exports.Schema = require('./lib/schema').Schema; | ||
exports.AbstractClass = require('./lib/abstract-class').AbstractClass; | ||
exports.Validatable = require('./lib/validatable').Validatable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
exports.inherits = function (newClass, baseClass) { | ||
Object.keys(baseClass).forEach(function (classMethod) { | ||
newClass[classMethod] = baseClass[classMethod]; | ||
}); | ||
Object.keys(baseClass.prototype).forEach(function (instanceMethod) { | ||
newClass.prototype[instanceMethod] = baseClass.prototype[instanceMethod]; | ||
}); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
exports.Validatable = Validatable; | ||
|
||
function Validatable() { | ||
// validatable class | ||
}; | ||
|
||
Validatable.validatesPresenceOf = getConfigurator('presence'); | ||
Validatable.validatesLengthOf = getConfigurator('length'); | ||
Validatable.validatesNumericalityOf = getConfigurator('numericality'); | ||
Validatable.validatesInclusionOf = getConfigurator('inclusion'); | ||
Validatable.validatesExclusionOf = getConfigurator('exclusion'); | ||
|
||
function getConfigurator(name) { | ||
return function () { | ||
configure(this, name, arguments); | ||
}; | ||
} | ||
|
||
Validatable.prototype.isValid = function () { | ||
var valid = true, inst = this; | ||
if (!this.constructor._validations) { | ||
Object.defineProperty(this, 'errors', { | ||
enumerable: false, | ||
configurable: true, | ||
value: false | ||
}); | ||
return valid; | ||
} | ||
Object.defineProperty(this, 'errors', { | ||
enumerable: false, | ||
configurable: true, | ||
value: new Errors | ||
}); | ||
this.constructor._validations.forEach(function (v) { | ||
if (validationFailed(inst, v)) { | ||
valid = false; | ||
} | ||
}); | ||
if (valid) { | ||
Object.defineProperty(this, 'errors', { | ||
enumerable: false, | ||
configurable: true, | ||
value: false | ||
}); | ||
} | ||
return valid; | ||
}; | ||
|
||
function validationFailed(inst, v) { | ||
var attr = v[0]; | ||
var conf = v[1]; | ||
// here we should check skip validation conditions (if, unless) | ||
// that can be specified in conf | ||
var fail = false; | ||
validators[conf.validation].call(inst, attr, conf, function onerror(kind) { | ||
var message; | ||
if (conf.message) { | ||
message = conf.message; | ||
} | ||
if (!message && defaultMessages[conf.validation]) { | ||
message = defaultMessages[conf.validation]; | ||
} | ||
if (!message) { | ||
message = 'is invalid'; | ||
} | ||
if (kind) { | ||
if (message[kind]) { | ||
// get deeper | ||
message = message[kind]; | ||
} else if (defaultMessages.common[kind]) { | ||
message = defaultMessages.common[kind]; | ||
} | ||
} | ||
inst.errors.add(attr, message); | ||
fail = true; | ||
}); | ||
return fail; | ||
} | ||
|
||
var defaultMessages = { | ||
presence: 'can\'t be blank', | ||
length: { | ||
min: 'too short', | ||
max: 'too long', | ||
is: 'length is wrong' | ||
}, | ||
common: { | ||
blank: 'is blank', | ||
'null': 'is null' | ||
}, | ||
numericality: { | ||
'int': 'is not an integer', | ||
'number': 'is not a number' | ||
}, | ||
inclusion: 'is not included in the list', | ||
exclusion: 'is reserved' | ||
}; | ||
|
||
var validators = { | ||
presence: function (attr, conf, err) { | ||
if (blank(this[attr])) { | ||
err(); | ||
} | ||
}, | ||
length: function (attr, conf, err) { | ||
var isNull = this[attr] === null || !(attr in this); | ||
if (isNull) { | ||
if (conf.allowNull) { | ||
return true; | ||
} else { | ||
return err('null'); | ||
} | ||
} else { | ||
if (blank(this[attr])) { | ||
if (conf.allowBlank) { | ||
return true; | ||
} else { | ||
return err('blank'); | ||
} | ||
} | ||
} | ||
var len = this[attr].length; | ||
if (conf.min && len < conf.min) { | ||
err('min'); | ||
} | ||
if (conf.max && len > conf.max) { | ||
err('max'); | ||
} | ||
if (conf.is && len !== conf.is) { | ||
err('is'); | ||
} | ||
}, | ||
numericality: function (attr, conf, err) { | ||
if (typeof this[attr] !== 'number') { | ||
return err('number'); | ||
} | ||
if (conf.int && this[attr] !== Math.round(this[attr])) { | ||
return err('int'); | ||
} | ||
}, | ||
inclusion: function (attr, conf, err) { | ||
if (!~conf.in.indexOf(this[attr])) { | ||
err() | ||
} | ||
}, | ||
exclusion: function (attr, conf, err) { | ||
if (~conf.in.indexOf(this[attr])) { | ||
err() | ||
} | ||
} | ||
}; | ||
|
||
function blank(v) { | ||
if (typeof v === 'undefined') return true; | ||
if (v instanceof Array && v.length === 0) return true; | ||
if (v === null) return true; | ||
if (typeof v == 'string' && v === '') return true; | ||
return false; | ||
} | ||
|
||
function configure(class, validation, args) { | ||
if (!class._validations) { | ||
Object.defineProperty(class, '_validations', { | ||
writable: true, | ||
configurable: true, | ||
enumerable: false, | ||
value: [] | ||
}); | ||
} | ||
args = [].slice.call(args); | ||
var conf; | ||
if (typeof args[args.length - 1] === 'object') { | ||
conf = args.pop(); | ||
} else { | ||
conf = {}; | ||
} | ||
conf.validation = validation; | ||
args.forEach(function (attr) { | ||
class._validations.push([attr, conf]); | ||
}); | ||
} | ||
|
||
function Errors() { | ||
} | ||
|
||
Errors.prototype.add = function (field, message) { | ||
if (!this[field]) { | ||
this[field] = [message]; | ||
} else { | ||
this[field].push(message); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.