Skip to content

Commit

Permalink
CastError.path; ValidatorError.value; fixed validators
Browse files Browse the repository at this point in the history
  • Loading branch information
ManInTheBox committed Sep 17, 2012
1 parent 3ae744b commit 6eb1cf4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 36 deletions.
19 changes: 0 additions & 19 deletions lib/errormessages.json

This file was deleted.

1 change: 1 addition & 0 deletions lib/errors/cast.js
Expand Up @@ -23,6 +23,7 @@ function CastError (type, value, message, path) {
Error.captureStackTrace(this, arguments.callee);
this.name = 'CastError';
this.type = type;
this.path = path;
this.value = value;
};

Expand Down
6 changes: 4 additions & 2 deletions lib/errors/validator.js
Expand Up @@ -13,11 +13,12 @@ var MongooseError = require('../error')
* @param {String} path
* @param {String} type
* @param {String} message
* @param {Any} value
* @inherits MongooseError
* @api private
*/

function ValidatorError (path, type, message) {
function ValidatorError (path, type, message, value) {
message = message || errorMessages[type];

if ('undefined' === typeof message) { // if still don't have message
Expand All @@ -27,8 +28,9 @@ function ValidatorError (path, type, message) {
MongooseError.call(this, utils.normalizeMessage(path, message));
Error.captureStackTrace(this, arguments.callee);
this.name = 'ValidatorError';
this.path = path;
this.type = type;
this.path = path;
this.value = value;
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/date.js
Expand Up @@ -5,7 +5,7 @@

var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, errorMessages = require('../errormessages');
, errorMessages = require('../errors/messages');

/**
* Date SchemaType constructor.
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/number.js
Expand Up @@ -4,7 +4,7 @@

var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, errorMessages = require('../errormessages');
, errorMessages = require('../errors/messages');

/**
* Number SchemaType constructor.
Expand Down
15 changes: 12 additions & 3 deletions lib/schema/string.js
Expand Up @@ -5,7 +5,7 @@

var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, errorMessages = require('../errormessages');
, errorMessages = require('../errors/messages');

/**
* String SchemaType constructor.
Expand Down Expand Up @@ -59,6 +59,14 @@ SchemaString.prototype.enum = function () {
return;
}

var message = null;
// we have custom error message passed
if (arguments[0] instanceof Array) {
len = arguments[0].length;
message = arguments[1];
arguments = arguments[0];
}

for (var i = 0; i < len; i++) {
if (undefined !== arguments[i]) {
this.enumValues.push(this.cast(arguments[i]));
Expand All @@ -70,7 +78,7 @@ SchemaString.prototype.enum = function () {
this.enumValidator = function(v){
return undefined === v || ~values.indexOf(v);
};
var message = errorMessages['string']['enum'];
message = message || errorMessages['string']['enum'];
this.validators.push([this.enumValidator, 'enum', message]);
}
};
Expand Down Expand Up @@ -155,10 +163,11 @@ SchemaString.prototype.trim = function () {
* })
*
* @param {RegExp} regExp regular expression to test against
* @param {String} optional error message
* @api public
*/

SchemaString.prototype.match = function match (regExp) {
SchemaString.prototype.match = function match (regExp, message) {
message = message || errorMessages['string']['match'];
this.validators.push([function(v){
return null != v && '' !== v
Expand Down
2 changes: 1 addition & 1 deletion lib/schematype.js
Expand Up @@ -556,7 +556,7 @@ SchemaType.prototype.doValidate = function (value, fn, scope) {
if (val === undefined || val) {
--count || fn(null);
} else {
fn(err = new ValidatorError(path, type, msg));
fn(err = new ValidatorError(path, type, msg, value));
}
}

Expand Down
16 changes: 7 additions & 9 deletions lib/utils.js
Expand Up @@ -483,6 +483,9 @@ exports.expires = function expires (object) {

/**
* Makes path more user friendly.
*
* If `message` contains special placeholder `{path}`, it will be
* replaced with normalized path.
*
* Example:
*
Expand All @@ -502,13 +505,13 @@ exports.expires = function expires (object) {
* type: Date
* } // My Birth Date
*
* If you don't like this format, supply `{path|Your own label}` in your own error message
* Provide your own error message instead of built-in one:
*
* name: {
* first: {
* type: String,
* required: [true, '{path|Your dear name} can\'t be blank.']
* }
* required: [true, 'Please fill out "{path}" field.']
* } // Please fill out "Name First" field.
* }
*
* @param {String} path
Expand All @@ -531,12 +534,7 @@ exports.normalizeMessage = function normalizeMessage(path, message) {
}
}

// TODO: fix this without {path|Label}
if (~message.indexOf('{path}')) {
return message.replace('{path}', normalizedPath.trim())
} else {
return message.replace(/\{path\|(.*)\}/i, '$1');
}
return message.replace('{path}', normalizedPath.trim())
};

exports.readPref = function readPref (pref, tags) {
Expand Down

0 comments on commit 6eb1cf4

Please sign in to comment.