Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
optional refinement with custom error message not passing locals.error
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Oct 13, 2015
1 parent cb10878 commit 6d821a6
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ var Component = (function (_React$Component) {
var error = this.props.options.error || this.typeInfo.getValidationErrorMessage;
if (_tcombValidation2['default'].Func.is(error)) {
var validationOptions = this.getValidationOptions();
return error(this.state.value, validationOptions.path, validationOptions.context);
var value = this.getTransformer().parse(this.state.value);
return error(value, validationOptions.path, validationOptions.context);
}
return error;
};
Expand Down
18 changes: 12 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ function getTypeInfo(type) {
var isMaybe = false;
var isSubtype = false;
var kind = undefined;
var hasGetValidationErrorMessage = false;
var innerGetValidationErrorMessage = undefined;

while (innerType) {
kind = innerType.meta.kind;
hasGetValidationErrorMessage = hasGetValidationErrorMessage || _tcombValidation2['default'].Func.is(innerType.getValidationErrorMessage);
if (_tcombValidation2['default'].Func.is(innerType.getValidationErrorMessage)) {
innerGetValidationErrorMessage = innerType.getValidationErrorMessage;
}
if (kind === 'maybe') {
isMaybe = true;
innerType = innerType.meta.type;
Expand All @@ -49,12 +51,16 @@ function getTypeInfo(type) {
break;
}

var getValidationErrorMessage = hasGetValidationErrorMessage ? function (value, path, context) {
var getValidationErrorMessage = innerGetValidationErrorMessage ? function (value, path, context) {
var result = _tcombValidation2['default'].validate(value, type, { path: path, context: context });
if (result.isValid() || !_tcombValidation2['default'].Func.is(result.errors[0].expected.getValidationErrorMessage)) {
return null;
if (!result.isValid()) {
for (var i = 0, len = result.errors.length; i < len; i++) {
if (_tcombValidation2['default'].Func.is(result.errors[i].expected.getValidationErrorMessage)) {
return result.errors[i].message;
}
}
return innerGetValidationErrorMessage(value, path, context);
}
return result.errors[0].message;
} : undefined;

return {
Expand Down
3 changes: 2 additions & 1 deletion src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ export class Component extends React.Component {
const error = this.props.options.error || this.typeInfo.getValidationErrorMessage;
if (t.Func.is(error)) {
const validationOptions = this.getValidationOptions();
return error(this.state.value, validationOptions.path, validationOptions.context);
const value = this.getTransformer().parse(this.state.value);
return error(value, validationOptions.path, validationOptions.context);
}
return error;
}
Expand Down
18 changes: 12 additions & 6 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export function getTypeInfo(type) {
let isMaybe = false;
let isSubtype = false;
let kind;
let hasGetValidationErrorMessage = false;
let innerGetValidationErrorMessage;

while (innerType) {
kind = innerType.meta.kind;
hasGetValidationErrorMessage = hasGetValidationErrorMessage || t.Func.is(innerType.getValidationErrorMessage);
if (t.Func.is(innerType.getValidationErrorMessage)) {
innerGetValidationErrorMessage = innerType.getValidationErrorMessage;
}
if (kind === 'maybe') {
isMaybe = true;
innerType = innerType.meta.type;
Expand All @@ -36,12 +38,16 @@ export function getTypeInfo(type) {
break;
}

const getValidationErrorMessage = hasGetValidationErrorMessage ? function (value, path, context) {
const getValidationErrorMessage = innerGetValidationErrorMessage ? function (value, path, context) {
var result = t.validate(value, type, {path, context});
if (result.isValid() || !t.Func.is(result.errors[0].expected.getValidationErrorMessage)) {
return null;
if (!result.isValid()) {
for (let i = 0, len = result.errors.length; i < len; i++ ) {
if (t.Func.is(result.errors[i].expected.getValidationErrorMessage)) {
return result.errors[i].message;
}
}
return innerGetValidationErrorMessage(value, path, context);
}
return result.errors[0].message;
} : undefined;

return {
Expand Down
153 changes: 153 additions & 0 deletions test/components/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,159 @@ var ctx = util.ctx;

tape('Component', function (tape) {

tape.test('getValidationErrorMessage', function (tape) {
tape.plan(15);

var component = new Component({
type: t.String,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#1');

component = new Component({
type: t.maybe(t.String),
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#2');

var Password = t.refinement(t.String, function (s) { return s.length > 6; });

component = new Component({
type: Password,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#3');

Password.getValidationErrorMessage = function () {
return 'bad password';
};

component = new Component({
type: Password,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), 'bad password', '#4');

component = new Component({
type: t.maybe(Password),
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#5');

component = new Component({
type: t.maybe(Password),
options: {},
ctx: ctx,
value: 'a'
});

tape.strictEqual(component.getError(), 'bad password', '#6');

t.String.getValidationErrorMessage = function () {
return 'bad string';
};

component = new Component({
type: Password,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), 'bad string', '#7');

delete t.String.getValidationErrorMessage;

var Age = t.refinement(t.Number, function (n) { return n > 6; });

component = new Component({
type: Age,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#8');

component = new Component({
type: t.maybe(Age),
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#9');

Age.getValidationErrorMessage = function () {
return 'bad age';
};

component = new Component({
type: Age,
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), 'bad age', '#10');

component = new Component({
type: t.maybe(Age),
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#11');

component = new Component({
type: t.maybe(Age),
options: {},
ctx: ctx,
value: 'a'
});

tape.strictEqual(component.getError(), 'bad age', '#12');

t.Number.getValidationErrorMessage = function () {
return 'bad number';
};

component = new Component({
type: Age,
options: {},
ctx: ctx,
value: 'a'
});

tape.strictEqual(component.getError(), 'bad number', '#13');

component = new Component({
type: Age,
options: {},
ctx: ctx,
value: 1
});

tape.strictEqual(component.getError(), 'bad age', '#14');

component = new Component({
type: t.maybe(Age),
options: {},
ctx: ctx
});

tape.strictEqual(component.getError(), undefined, '#15');

delete t.Number.getValidationErrorMessage;

});

tape.test('typeInfo', function (tape) {
tape.plan(3);

Expand Down
2 changes: 1 addition & 1 deletion test/components/Datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tape('Datetime', function (tape) {
ctx: ctx,
value: new Date(1973, 10, 30)
}).getLocals().error,
'error: 1973,10,30',
'error: Fri Nov 30 1973 00:00:00 GMT+0100 (CET)',
'should handle error option as a function');
});

Expand Down

0 comments on commit 6d821a6

Please sign in to comment.