Skip to content

Commit

Permalink
Allow all return values in custom validators
Browse files Browse the repository at this point in the history
Fixes #899.
  • Loading branch information
Marsup committed Jun 25, 2016
1 parent 9ed1e43 commit a646761
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 68 deletions.
6 changes: 3 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const customJoi = Joi.extend({
return Math.round(value); // Change the value
}

return null; // Keep the value as it was
return value; // Keep the value as it was
},
rules: [
{
Expand All @@ -327,7 +327,7 @@ const customJoi = Joi.extend({
return this.createError('number.round', { v: value }, state, options);
}

return null; // Everything is OK
return value; // Everything is OK
}
},
{
Expand All @@ -342,7 +342,7 @@ const customJoi = Joi.extend({
return this.createError('number.dividable', { v: value, q: params.q }, state, options);
}

return null; // Everything is OK
return value; // Everything is OK
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions lib/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ internals.Alternatives = class extends Any {
const obj = this.clone();
let is = Cast.schema(options.is);

if (options.is === null || !options.is.isJoi) {
if (options.is === null || !(Ref.isRef(options.is) || options.is instanceof Any)) {

// Only apply required if this wasn't already a schema, we'll suppose people know what they're doing
// Only apply required if this wasn't already a schema or a ref, we'll suppose people know what they're doing
is = is.required();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module.exports = internals.Any = class {

concat(schema) {

Hoek.assert(schema && schema.isJoi, 'Invalid schema object');
Hoek.assert(schema instanceof internals.Any, 'Invalid schema object');
Hoek.assert(this._type === 'any' || schema._type === 'any' || schema._type === this._type, 'Cannot merge type', this._type, 'with another type:', schema._type);

let obj = this.clone();
Expand Down Expand Up @@ -566,13 +566,13 @@ module.exports = internals.Any = class {
for (let i = 0; i < this._tests.length; ++i) {
const test = this._tests[i];
const ret = test.func.call(this, value, state, options);
if (ret && ret.isJoi) {
if (ret instanceof Errors.Err) {
errors.push(ret);
if (options.abortEarly) {
return finish();
}
}
else if (ret !== null) {
else {
value = ret;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ internals.Array = class extends Any {
return this._test('min', limit, function (value, state, options) {

if (value.length >= limit) {
return null;
return value;
}

return this.createError('array.min', { limit, value }, state, options);
Expand All @@ -409,7 +409,7 @@ internals.Array = class extends Any {
return this._test('max', limit, function (value, state, options) {

if (value.length <= limit) {
return null;
return value;
}

return this.createError('array.max', { limit, value }, state, options);
Expand All @@ -423,7 +423,7 @@ internals.Array = class extends Any {
return this._test('length', limit, function (value, state, options) {

if (value.length === limit) {
return null;
return value;
}

return this.createError('array.length', { limit, value }, state, options);
Expand Down Expand Up @@ -475,7 +475,7 @@ internals.Array = class extends Any {
}
}

return null;
return value;
});
}

Expand Down
6 changes: 3 additions & 3 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internals.Binary = class extends Any {
return this._test('min', limit, function (value, state, options) {

if (value.length >= limit) {
return null;
return value;
}

return this.createError('binary.min', { limit, value }, state, options);
Expand All @@ -69,7 +69,7 @@ internals.Binary = class extends Any {
return this._test('max', limit, function (value, state, options) {

if (value.length <= limit) {
return null;
return value;
}

return this.createError('binary.max', { limit, value }, state, options);
Expand All @@ -83,7 +83,7 @@ internals.Binary = class extends Any {
return this._test('length', limit, function (value, state, options) {

if (value.length === limit) {
return null;
return value;
}

return this.createError('binary.length', { limit, value }, state, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ internals.compare = function (type, compare) {
}

if (compare(value.getTime(), compareTo)) {
return null;
return value;
}

return this.createError('date.' + type, { limit: new Date(compareTo) }, state, options);
Expand Down
6 changes: 3 additions & 3 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internals.stringify = function (value, wrapArrays) {
return value;
}

if (value instanceof internals.Err || type === 'function') {
if (value instanceof exports.Err || type === 'function') {
return value.toString();
}

Expand All @@ -43,7 +43,7 @@ internals.stringify = function (value, wrapArrays) {
return JSON.stringify(value);
};

internals.Err = class {
exports.Err = class {

constructor(type, context, state, options, flags) {

Expand Down Expand Up @@ -97,7 +97,7 @@ internals.Err = class {

exports.create = function (type, context, state, options, flags) {

return new internals.Err(type, context, state, options, flags);
return new exports.Err(type, context, state, options, flags);
};


Expand Down
17 changes: 8 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const Hoek = require('hoek');
const Any = require('./any');
const Cast = require('./cast');
const Errors = require('./errors');
const Lazy = require('./lazy');
const Ref = require('./ref');

Expand Down Expand Up @@ -151,7 +152,7 @@ internals.root = function () {

root.reach = function (schema, path) {

Hoek.assert(schema && schema.isJoi, 'you must provide a joi schema');
Hoek.assert(schema && schema instanceof Any, 'you must provide a joi schema');
Hoek.assert(typeof path === 'string', 'path must be a string');

if (path === '') {
Expand Down Expand Up @@ -218,12 +219,11 @@ internals.root = function () {
let ret;
if (extension.coerce) {
ret = extension.coerce.call(this, value, state, options);
if (ret && ret.isJoi) {
if (ret instanceof Errors.Err) {
return { value, errors: ret };
}
else if (ret !== null) {
value = ret;
}

value = ret;
}

if (ctor.prototype._base) {
Expand All @@ -238,12 +238,11 @@ internals.root = function () {

if (extension.pre) {
ret = extension.pre.call(this, value, state, options);
if (ret && ret.isJoi) {
if (ret instanceof Errors.Err) {
return { value, errors: ret };
}
else if (ret !== null) {
return { value: ret };
}

return { value: ret };
}

return { value };
Expand Down
12 changes: 6 additions & 6 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internals.Number = class extends Any {
}

if (value % divisor === 0) {
return null;
return value;
}

return this.createError('number.multiple', { multiple: base, value }, state, options);
Expand All @@ -80,7 +80,7 @@ internals.Number = class extends Any {

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

return Hoek.isInteger(value) ? null : this.createError('number.integer', { value }, state, options);
return Hoek.isInteger(value) ? value : this.createError('number.integer', { value }, state, options);
});
}

Expand All @@ -89,7 +89,7 @@ internals.Number = class extends Any {
return this._test('negative', undefined, function (value, state, options) {

if (value < 0) {
return null;
return value;
}

return this.createError('number.negative', { value }, state, options);
Expand All @@ -101,7 +101,7 @@ internals.Number = class extends Any {
return this._test('positive', undefined, function (value, state, options) {

if (value > 0) {
return null;
return value;
}

return this.createError('number.positive', { value }, state, options);
Expand All @@ -118,7 +118,7 @@ internals.Number = class extends Any {
const places = value.toString().match(internals.precisionRx);
const decimals = Math.max((places[1] ? places[1].length : 0) - (places[2] ? parseInt(places[2], 10) : 0), 0);
if (decimals <= limit) {
return null;
return value;
}

return this.createError('number.precision', { limit, value }, state, options);
Expand Down Expand Up @@ -155,7 +155,7 @@ internals.compare = function (type, compare) {
}

if (compare(value, compareTo)) {
return null;
return value;
}

return this.createError('number.' + type, { limit: compareTo, value }, state, options);
Expand Down
Loading

0 comments on commit a646761

Please sign in to comment.