Skip to content

Commit

Permalink
language cleanup. Closes #259
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Apr 17, 2014
1 parent 8f80500 commit 978b073
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 90 deletions.
2 changes: 1 addition & 1 deletion lib/any.js
Expand Up @@ -334,7 +334,7 @@ internals.Any.prototype._validate = function (value, state, options, reference)
// Required values did not match

if (this._flags.allowOnly) {
errors.push(Errors.create('any.validate.allowOnly', { value: this._valids.toString(false) }, state, options));
errors.push(Errors.create('any.allowOnly', { value: this._valids.toString(false) }, state, options));
if (options.abortEarly) {
return finish();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/array.js
Expand Up @@ -119,7 +119,7 @@ internals.Array.prototype.min = function (limit) {
return null;
}

return Errors.create('array.min', { value: limit }, state, options);
return Errors.create('array.min', { limit: limit }, state, options);
});
};

Expand All @@ -134,7 +134,7 @@ internals.Array.prototype.max = function (limit) {
return null;
}

return Errors.create('array.max', { value: limit }, state, options);
return Errors.create('array.max', { limit: limit }, state, options);
});
};

Expand All @@ -149,7 +149,7 @@ internals.Array.prototype.length = function (limit) {
return null;
}

return Errors.create('array.length', { value: limit }, state, options);
return Errors.create('array.length', { limit: limit }, state, options);
});
};

Expand Down
6 changes: 3 additions & 3 deletions lib/binary.js
Expand Up @@ -63,7 +63,7 @@ internals.Binary.prototype.min = function (limit) {
return null;
}

return Errors.create('binary.min', { value: limit }, state, options);
return Errors.create('binary.min', { limit: limit }, state, options);
});
};

Expand All @@ -78,7 +78,7 @@ internals.Binary.prototype.max = function (limit) {
return null;
}

return Errors.create('binary.max', { value: limit }, state, options);
return Errors.create('binary.max', { limit: limit }, state, options);
});
};

Expand All @@ -93,7 +93,7 @@ internals.Binary.prototype.length = function (limit) {
return null;
}

return Errors.create('binary.length', { value: limit }, state, options);
return Errors.create('binary.length', { limit: limit }, state, options);
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/date.js
Expand Up @@ -60,7 +60,7 @@ internals.Date.prototype.min = function (date) {
return null;
}

return Errors.create('date.min', { value: date }, state, options);
return Errors.create('date.min', { limit: date }, state, options);
});
};

Expand All @@ -76,7 +76,7 @@ internals.Date.prototype.max = function (date) {
return null;
}

return Errors.create('date.max', { value: date }, state, options);
return Errors.create('date.max', { limit: date }, state, options);
});
};

Expand Down
4 changes: 3 additions & 1 deletion lib/errors.js
Expand Up @@ -13,7 +13,7 @@ internals.Err = function (type, context, state, options) {

this.type = type;
this.context = context || {};
this.context.key = state.key || '<root>';
this.context.key = state.key;
this.path = state.path;
this.options = options;
};
Expand All @@ -24,6 +24,8 @@ internals.Err.prototype.toString = function () {
var self = this;

var localized = this.options.language;
this.context.key = this.context.key || localized.root || Language.errors.root;

var format = Hoek.reach(localized, this.type) || Hoek.reach(Language.errors, this.type);
if (!format) {
return this.context.key;
Expand Down
79 changes: 40 additions & 39 deletions lib/language.js
Expand Up @@ -7,46 +7,45 @@ var internals = {};


exports.errors = {
root: 'value',
any: {
unknown: 'the key {{key}} is not allowed',
invalid: 'the value of {{key}} is not allowed to be {{value}}',
empty: 'the value of {{key}} is not allowed to be empty',
validate: {
allowOnly: 'the value of {{key}} must be one of {{value}}'
}
unknown: '{{key}} is not allowed',
invalid: '{{key}} is not allowed to be {{value}}',
empty: '{{key}} is not allowed to be empty',
allowOnly: '{{key}} must be one of {{value}}'
},
alternatives: {
base: 'no matching alternatives found'
base: '{{key}} not matching any of the allowed alternatives'
},
array: {
base: 'the value of {{key}} must be an array',
base: '{{key}} must be an array',
includes: '{{key}}[{{pos}}] does not match any of the allowed types',
'includes-single': '{{key}}[{{pos}}] fails because {{reason}}',
excludes: '{{key}}[{{pos}}] contains an excluded value',
min: 'the length of {{key}} must include at least {{value}} items',
max: 'the length of {{key}} must include less than (or equal to) {{value}} items',
length: 'the length of {{key}} must include {{value}} items'
min: '{{key}} must contain at least {{limit}} items',
max: '{{key}} must contain less than or equal to {{limit}} items',
length: '{{key}} must contain {{limit}} items'
},
boolean: {
base: 'the value of {{key}} must be a boolean'
base: '{{key}} must be a boolean'
},
binary: {
base: 'the value of {{key}} must be a buffer or a string',
min: 'the length of {{key}} must be at least {{value}}',
max: 'the length of {{key}} must be less than (or equal to) {{value}}',
length: 'the length of {{key}} must be {{value}}'
base: '{{key}} must be a buffer or a string',
min: '{{key}} must be at least {{limit}} bytes',
max: '{{key}} must be less than or equal to {{limit}} bytes',
length: '{{key}} must be {{limit}} bytes'
},
date: {
base: 'the value of {{key}} must be a number of milliseconds or valid date string',
min: 'the value of {{key}} must be larger than or equal to {{value}}',
max: 'the value of {{key}} must be less than or equal to {{value}}'
base: '{{key}} must be a number of milliseconds or valid date string',
min: '{{key}} must be larger than or equal to {{limit}}',
max: '{{key}} must be less than or equal to {{limit}}'
},
function: {
base: 'the value of {{key}} must be a Function'
base: '{{key}} must be a Function'
},
object: {
base: 'the value of {{key}} must be an object',
allowUnknown: 'the key {{key}} is not allowed',
base: '{{key}} must be an object',
allowUnknown: '{{key}} is not allowed',
rename: {
multiple: 'cannot rename {{from}} because multiple renames are disabled and another key was already renamed to {{to}}',
override: 'cannot rename {{from}} because override is disabled and target {{to}} exists'
Expand All @@ -62,23 +61,25 @@ exports.errors = {
assert: '{{ref}} failed assertion {{message}}'
},
number: {
base: 'the value of {{key}} must be a number',
min: 'the value of {{key}} must be larger than or equal to {{value}}',
max: 'the value of {{key}} must be less than or equal to {{value}}',
float: 'the value of {{key}} must be a float or double',
int: 'the value of {{key}} must be an integer'
base: '{{key}} must be a number',
min: '{{key}} must be larger than or equal to {{limit}}',
max: '{{key}} must be less than or equal to {{limit}}',
float: '{{key}} must be a float or double',
integer: '{{key}} must be an integer',
negative: '{{key}} must be a negative number',
positive: '{{key}} must be a positive number'
},
string: {
base: 'the value of {{key}} must be a string',
min: 'the length of {{key}} must be at least {{value}} characters long',
max: 'the length of {{key}} must be less than or equal to {{value}} characters long',
length: 'the length of {{key}} must be {{value}} characters long',
alphanum: 'the value of {{key}} must only contain alpha-numeric characters',
token: 'the value of {{key}} must only contain alpha-numeric and underscore characters',
regex: 'the value of {{key}} must match the regular expression {{value}}',
email: 'The value of {{key}} must be a valid email',
isoDate: 'the value of {{key}} must be a valid ISO 8601 date',
guid: 'the value of {{key}} must be a valid GUID',
hostname: 'the value of {{key}} must be a valid hostname'
}
base: '{{key}} must be a string',
min: '{{key}} length must be at least {{limit}} characters long',
max: '{{key}} length must be less than or equal to {{limit}} characters long',
length: '{{key}} length must be {{limit}} characters long',
alphanum: '{{key}} must only contain alpha-numeric characters',
token: '{{key}} must only contain alpha-numeric and underscore characters',
regex: '{{key}} must match the regular expression {{value}}',
email: '{{key}} must be a valid email',
isoDate: '{{key}} must be a valid ISO 8601 date',
guid: '{{key}} must be a valid GUID',
hostname: '{{key}} must be a valid hostname'
}
};
10 changes: 5 additions & 5 deletions lib/number.js
Expand Up @@ -48,7 +48,7 @@ internals.Number.prototype.min = function (limit) {
return null;
}

return Errors.create('number.min', { value: limit }, state, options);
return Errors.create('number.min', { limit: limit }, state, options);
});
};

Expand All @@ -63,7 +63,7 @@ internals.Number.prototype.max = function (limit) {
return null;
}

return Errors.create('number.max', { value: limit }, state, options);
return Errors.create('number.max', { limit: limit }, state, options);
});
};

Expand All @@ -72,7 +72,7 @@ internals.Number.prototype.integer = function () {

return this._test('integer', undefined, function (value, state, options) {

return Hoek.isInteger(value) ? null : Errors.create('number.int', { value: value }, state, options);
return Hoek.isInteger(value) ? null : Errors.create('number.integer', null, state, options);
});
};

Expand All @@ -85,7 +85,7 @@ internals.Number.prototype.negative = function () {
return null;
}

return Errors.create('number.negative', { value: value }, state, options);
return Errors.create('number.negative', null, state, options);
});
};

Expand All @@ -98,7 +98,7 @@ internals.Number.prototype.positive = function () {
return null;
}

return Errors.create('number.positive', { value: value }, state, options);
return Errors.create('number.positive', null, state, options);
});
};

Expand Down
6 changes: 3 additions & 3 deletions lib/object.js
Expand Up @@ -231,7 +231,7 @@ internals.Object.prototype.length = function (limit) {
return null;
}

return Errors.create('object.length', { value: limit }, state, options);
return Errors.create('object.length', { limit: limit }, state, options);
});
};

Expand All @@ -246,7 +246,7 @@ internals.Object.prototype.min = function (limit) {
return null;
}

return Errors.create('object.min', { value: limit }, state, options);
return Errors.create('object.min', { limit: limit }, state, options);
});
};

Expand All @@ -261,7 +261,7 @@ internals.Object.prototype.max = function (limit) {
return null;
}

return Errors.create('object.max', { value: limit }, state, options);
return Errors.create('object.max', { limit: limit }, state, options);
});
};

Expand Down
6 changes: 3 additions & 3 deletions lib/string.js
Expand Up @@ -49,7 +49,7 @@ internals.String.prototype.min = function (limit, encoding) {
return null;
}

return Errors.create('string.min', { value: limit }, state, options);
return Errors.create('string.min', { limit: limit }, state, options);
});
};

Expand All @@ -65,7 +65,7 @@ internals.String.prototype.max = function (limit, encoding) {
return null;
}

return Errors.create('string.max', { value: limit }, state, options);
return Errors.create('string.max', { limit: limit }, state, options);
});
};

Expand All @@ -81,7 +81,7 @@ internals.String.prototype.length = function (limit, encoding) {
return null;
}

return Errors.create('string.length', { value: limit }, state, options);
return Errors.create('string.length', { limit: limit }, state, options);
});
};

Expand Down
2 changes: 1 addition & 1 deletion test/alternatives.js
Expand Up @@ -26,7 +26,7 @@ describe('alternatives', function () {
Joi.alternatives().validate('a', function (err, value) {

expect(err).to.exist;
expect(err.message).to.equal('no matching alternatives found');
expect(err.message).to.equal('value not matching any of the allowed alternatives');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/any.js
Expand Up @@ -229,7 +229,7 @@ describe('any', function () {
expect(function () {

var schema = Joi.valid(5, 6, 7).example(4);
}).to.throw('Bad example: the value of <root> must be one of 5, 6, 7');
}).to.throw('Bad example: value must be one of 5, 6, 7');
done();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/array.js
Expand Up @@ -36,7 +36,7 @@ describe('array', function () {
Joi.array().validate('{ "something": false }', function (err, value) {

expect(err).to.exist;
expect(err.message).to.equal('the value of <root> must be an array');
expect(err.message).to.equal('value must be an array');
done();
});
});
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('array', function () {

schema.validate(input, function (err, value) {

expect(err.message).to.equal('test[1] fails because the value of foo is not allowed to be undefined');
expect(err.message).to.equal('test[1] fails because foo is not allowed to be undefined');
done();
});
});
Expand Down Expand Up @@ -317,7 +317,7 @@ describe('array', function () {
schema.validate(input, function (err, value) {

expect(err).to.exist;
expect(err.message).to.equal('arr[2] fails because the value of 2 must be an integer');
expect(err.message).to.equal('arr[2] fails because 2 must be an integer');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/binary.js
Expand Up @@ -40,7 +40,7 @@ describe('binary', function () {
Joi.binary().validate(5, function (err, value) {

expect(err).to.exist;
expect(err.message).to.equal('the value of <root> must be a buffer or a string');
expect(err.message).to.equal('value must be a buffer or a string');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/date.js
Expand Up @@ -47,7 +47,7 @@ describe('date', function () {
Joi.date().options({ convert: false }).validate('1-1-2013', function (err, value) {

expect(err).to.exist;
expect(err.message).to.equal('the value of <root> must be a number of milliseconds or valid date string');
expect(err.message).to.equal('value must be a number of milliseconds or valid date string');
done();
});
});
Expand Down
11 changes: 10 additions & 1 deletion test/errors.js
Expand Up @@ -70,6 +70,15 @@ describe('errors', function () {
});
});

it('overrides root key language', function (done) {

Joi.string().options({ language: { root: 'blah' } }).validate(4, function (err, value) {

expect(err.message).to.equal('blah must be a string');
done();
});
});

describe('#annotate', function () {

it('displays alternatives fail as a single line', function (done) {
Expand All @@ -85,7 +94,7 @@ describe('errors', function () {
Joi.validate({ x: true }, schema, function (err, value) {

expect(err).to.exist;
expect(err.annotate()).to.equal('{\n \"x\" \u001b[31m[1, 2, 3]\u001b[0m: true\n}\n\u001b[31m\n[1] the value of x must be a string\n[2] the value of x must be a number\n[3] the value of x must be a number of milliseconds or valid date string\u001b[0m');
expect(err.annotate()).to.equal('{\n \"x\" \u001b[31m[1, 2, 3]\u001b[0m: true\n}\n\u001b[31m\n[1] x must be a string\n[2] x must be a number\n[3] x must be a number of milliseconds or valid date string\u001b[0m');
done();
});
});
Expand Down

0 comments on commit 978b073

Please sign in to comment.