Skip to content

Commit

Permalink
Fixed bug in issue surveyjs#462. Added corresponding tests (surveyjs#464
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dmitrykobets authored and andrewtelnov committed Jun 22, 2017
1 parent 099f2c6 commit ab26b4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ export class NumericValidator extends SurveyValidator {
}
public getType(): string { return "numericvalidator"; }
public validate(value: any, name: string = null): ValidatorResult {
if (!value || !this.isNumber(value)) {
if (!this.isNumber(value)) {
return new ValidatorResult(null, new RequreNumericError());
}
var result = new ValidatorResult(parseFloat(value));
if (this.minValue && this.minValue > result.value) {
if (this.minValue !== null && this.minValue > result.value) {
result.error = new CustomError(this.getErrorText(name));
return result;
}
if (this.maxValue && this.maxValue < result.value) {
if (this.maxValue !== null && this.maxValue < result.value) {
result.error = new CustomError(this.getErrorText(name));
return result;
}
return (typeof value === 'number') ? null : result;
}
protected getDefaultErrorText(name: string) {
var vName = name ? name : "value";
if (this.minValue && this.maxValue) {
if (this.minValue !== null && this.maxValue !== null) {
return surveyLocalization.getString("numericMinMax")["format"](vName, this.minValue, this.maxValue);
} else {
if (this.minValue) {
if (this.minValue !== null) {
return surveyLocalization.getString("numericMin")["format"](vName, this.minValue);
}
return surveyLocalization.getString("numericMax")["format"](vName, this.maxValue);
Expand Down
34 changes: 26 additions & 8 deletions tests/surveyvalidatortests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@ export default QUnit.module("Validators");
QUnit.test("Numeric validator", function (assert) {
var validator = new NumericValidator();
assert.notEqual(validator.validate("s5").error, null, "Could not convert to numeric");
assert.equal(validator.validate(5), null, "There is no limits here");
assert.equal(validator.validate("5").value, 5, "Convert to numeric");
assert.equal(validator.validate("5").error, null, "There is no error");
assert.equal(validator.validate(5), null, "There are no limits (non-zero)");
assert.equal(validator.validate(0), null, "There are no limits (zero)");
assert.equal(validator.validate("5").value, 5, "Convert to numeric (non-zero)");
assert.equal(validator.validate("5").error, null, "There is no error (non-zero)");
assert.equal(validator.validate("0").value, 0, "Convert to numeric (zero)");
assert.equal(validator.validate("0").error, null, "There is no error (zero)");

validator.minValue = 10;
assert.notEqual(validator.validate(5).error, null, "There is should be an error. The value is low.");
validator.maxValue = 20;
assert.notEqual(validator.validate(25).error, null, "There is should be an error. The value is high.");
assert.equal(validator.validate("15").error, null, "There value is between minValue and maxValue");
assert.equal(validator.validate(15), null, "everything is fine - return null");
validator.maxValue = 20;
assert.notEqual(validator.validate(5).error, null, "Value is too low. Limits are not 0.");
assert.notEqual(validator.validate(25).error, null, "Value is too high. Limits are not 0.");
assert.equal(validator.validate("15").error, null, "Value is between minValue and maxValue. Limits are not 0.");
assert.equal(validator.validate(15), null, "Value is between minValue and maxValue. Return no errors. Limits are not 0.");

validator.minValue = 0;
validator.maxValue = 20;
assert.notEqual(validator.validate(-1).error, null, "Value is too low. Low limit is 0.");
assert.notEqual(validator.validate(25).error, null, "Value is too high. Low limit is 0.");
assert.equal(validator.validate("15").error, null, "Value is between minValue and maxValue. Low limit is 0.");
assert.equal(validator.validate(15), null, "Value is between minValue and maxValue. Return no errors. Low limit is 0.");

validator.minValue = -20;
validator.maxValue = 0;
assert.notEqual(validator.validate(-21).error, null, "Value is too low. High limit is 0.");
assert.notEqual(validator.validate(1).error, null, "Value is too high. High limit is 0.");
assert.equal(validator.validate("-5").error, null, "Value is between minValue and maxValue. High limit is 0.");
assert.equal(validator.validate(-5), null, "Value is between minValue and maxValue. Return no errors. High limit is 0.");
});

QUnit.test("Email validator", function (assert) {
Expand Down

0 comments on commit ab26b4e

Please sign in to comment.