Skip to content

Commit

Permalink
More i18n for model datatypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Mar 13, 2012
1 parent d65508c commit 0d2a4b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
67 changes: 34 additions & 33 deletions lib/model/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*/

var i18n = require('../i18n');

/*
* Datatype verification -- may modify the value by casting
*/
Expand All @@ -31,35 +33,35 @@ var datatypes = new (function () {

this.string = function (name, val, locale) {
return {
err: null,
val: String(val)
err: null
, val: String(val)
};
};

this.number = function (name, val, locale) {
if (isNaN(val)) {
return {
err: 'Field "' + name + '" must be a Number.',
val: null
err: i18n.getText('model.validatesNumber', {name: name}, locale)
, val: null
};
};
return {
err: null,
val: Number(val)
err: null
, val: Number(val)
};
};

this.int = function (name, val, locale) {
// Allow decimal values like 10.0 and 3.0
if (Math.round(val) != val) {
return {
err: 'Field "' + name + '" must be an integer.',
val: null
err: i18n.getText('model.validatesInteger', {name: name}, locale)
, val: null
};
};
return {
err: null,
val: parseInt(val, 10)
err: null
, val: parseInt(val, 10)
};
};

Expand Down Expand Up @@ -91,30 +93,29 @@ var datatypes = new (function () {

if (typeof validated != 'boolean') {
return {
err: 'Field "' + name + '" must be a Boolean.',
val: null
err: i18n.getText('model.validatesBoolean', {name: name}, locale)
, val: null
};
};
return {
err: null,
val: validated
err: null
, val: validated
};
};

this.object = function (name, val, locale) {
// Sure, Arrays are technically Objects, but we're treating Array as a
// separate datatype. Remember, instanceof Array fails across window
// boundaries, so let's also make sure the Object doesn't have a 'length'
// property.
// boundaries, so let's also make sure the Object isn't Array-ish
if (typeof val != 'object' || _isArray(val)) {
return {
err: 'Field "' + name + '" must be an Object.',
val: null
err: i18n.getText('model.validatesObject', {name: name}, locale)
, val: null
};
};
return {
err: null,
val: val
err: null
, val: val
};
};

Expand All @@ -123,28 +124,28 @@ var datatypes = new (function () {
// to make sure there's a length property
if (!_isArray(val)) {
return {
err: 'Field "' + name + '" must be an Array.',
val: null
err: i18n.getText('model.validatesArray', {name: name}, locale)
, val: null
};
};
return {
err: null,
val: val
err: null
, val: val
};
};

this.date = function (name, val, locale) {
var dt = geddy.date.parse(val);
if (dt) {
return {
err: null,
val: dt
err: null
, val: dt
};
}
else {
return {
err: 'Field "' + name + '" must be in a valid date format.',
val: null
err: i18n.getText('model.validatesDate', {name: name}, locale)
, val: null
};
}
};
Expand All @@ -153,14 +154,14 @@ var datatypes = new (function () {
var dt = geddy.date.parse(val);
if (dt) {
return {
err: null,
val: dt
err: null
, val: dt
};
}
else {
return {
err: 'Field "' + name + '" must be in a valid datetime format.',
val: null
err: i18n.getText('model.validatesDatetime', {name: name}, locale)
, val: null
};
}
};
Expand All @@ -177,7 +178,7 @@ var datatypes = new (function () {
}
else {
return {
err: 'Field "' + name + '" must be in a valid time format.',
err: i18n.getText('model.validatesTime', {name: name}, locale)
val: null
};
}
Expand Down
7 changes: 7 additions & 0 deletions templates/locales/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
, "model.validatesMinLength": "\"{name}\" must be at least {min} characters long."
, "model.validatesMaxLength": "\"{name}\" may not be more than {max} characters long."
, "model.validatesWithFunction": "\"{name}\" is not valid."
, "model.validatesNumber": "\"{name}\" must be a number."
, "model.validatesInteger": "\"{name}\" must be an integer."
, "model.validatesObject": "\"{name}\" must be an object."
, "model.validatesArray": "\"{name}\" must be an array."
, "model.validatesDate": "\"{name}\" must be in a valid date format."
, "model.validatesDatetime": "\"{name}\" must be in a valid datetime format."
, "model.validatesTime": "\"{name}\" must be in a valid time format."
}

0 comments on commit 0d2a4b4

Please sign in to comment.