Skip to content

Commit

Permalink
[added] validation params to ValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 25, 2016
1 parent 9ff67a3 commit 816e607
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ inherits(ObjectSchema, MixedSchema, {
}),
props = this._nodes.concat(extra);

var innerOptions = _extends({}, opts, { parent: {} });
var innerOptions = _extends({}, opts, { parent: {}, __validating: false });

value = transform(props, function (obj, prop) {
var field = fields[prop];
Expand All @@ -105,9 +105,11 @@ inherits(ObjectSchema, MixedSchema, {
if (field) {
var fieldValue = void 0;

var strict = field._options && field._options.strict;

if (field._strip === true) return;

fieldValue = field.cast(value[prop], innerOptions);
fieldValue = !opts.__validating || !strict ? field.cast(value[prop], innerOptions) : value[prop];

if (fieldValue !== undefined) obj[prop] = fieldValue;
} else if (exists && !strip) obj[prop] = value[prop];
Expand All @@ -127,6 +129,8 @@ inherits(ObjectSchema, MixedSchema, {
endEarly = this._option('abortEarly', opts);
recursive = this._option('recursive', opts);

opts = _extends({}, opts, { __validating: true });

return MixedSchema.prototype._validate.call(this, _value, opts).catch(endEarly ? null : function (err) {
errors.push(err);
return err.value;
Expand Down
3 changes: 2 additions & 1 deletion src/util/createValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ function createErrorFactory({ value, label, resolve, ...opts}) {
return function createError({ path = opts.path, message = opts.message, type = opts.name, params } = {}) {
params = resolveParams(opts.params, params, resolve)

return new ValidationError(
return Object.assign(new ValidationError(
formatError(message, { path, value, label, ...params })
, value
, path
, type)
, { params })
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Object types', function(){
err.message.should.match(/must be a `string` type/)
})

it ('should respect child schema with strict()', async () => {
it('should respect child schema with strict()', async () => {
inst = object({
field: number().strict()
})
Expand All @@ -132,6 +132,14 @@ describe('Object types', function(){
err.message.should.match(/must be a `number` type/)

inst.cast({ field: '5' }).should.eql({ field: 5 })

err = await object({
port: number()
.strict()
.integer()
})
.validate({ port: 'asdad' })
.should.be.rejected
})

it('should handle custom validation', async function(){
Expand Down

0 comments on commit 816e607

Please sign in to comment.