Skip to content

Commit

Permalink
Fix: Text validator validates empty string #3065 (#3068)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jul 8, 2021
1 parent 936bc3a commit 148125b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class TextValidator extends SurveyValidator {
values: any = null,
properties: any = null
): ValidatorResult {
if (value !== "" && this.isValueEmpty(value)) return null;
if (this.isValueEmpty(value)) return null;
if (!this.allowDigits) {
var reg = /^[A-Za-z\s]*$/;
if (!reg.test(value)) {
Expand Down Expand Up @@ -456,7 +456,7 @@ export class ExpressionValidator extends SurveyValidator {
properties: any = null
): ValidatorResult {
if (!this.ensureConditionRunner()) return null;
this.conditionRunner.onRunComplete = res => {
this.conditionRunner.onRunComplete = (res) => {
this.isRunningValue = false;
if (!!this.onAsyncCompleted) {
this.onAsyncCompleted(this.generateError(res, value, name));
Expand Down
111 changes: 72 additions & 39 deletions tests/surveyvalidatortests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { FunctionFactory } from "../src/functionsfactory";

export default QUnit.module("Validators");

QUnit.test("Numeric validator", function (assert) {
QUnit.test("Numeric validator", function(assert) {
var validator = new NumericValidator();
assert.notEqual(
validator.validate("s5").error,
Expand Down Expand Up @@ -109,7 +109,7 @@ QUnit.test("Numeric validator", function (assert) {
);
});

QUnit.test("Email validator", function (assert) {
QUnit.test("Email validator", function(assert) {
var validator = new EmailValidator();
assert.equal(
validator.validate("my@mail.com"),
Expand All @@ -123,12 +123,12 @@ QUnit.test("Email validator", function (assert) {
);
});

QUnit.test("Text validator", function (assert) {
QUnit.test("Text validator", function(assert) {
var validator = new TextValidator();
assert.equal(validator.validate(""), null, "Empty string");
validator.minLength = 1;
validator.maxLength = 5;
assert.notEqual(validator.validate(""), null, "Empty string");
assert.equal(validator.validate(""), null, "Empty string");
assert.equal(validator.validate("a"), null, "Shorter string");
assert.equal(validator.validate("abcde"), null, "Five letter string");
assert.notEqual(validator.validate("abcdef"), null, "Longer string");
Expand All @@ -137,30 +137,29 @@ QUnit.test("Text validator", function (assert) {
assert.notEqual(validator.validate("abc12"), null, "Not just text");
});

QUnit.test(
"Text validator calls onPropertyValueChangedCallback",
function (assert) {
var validator = new TextValidator();
var changes = {};
validator.onPropertyValueChangedCallback = (name) => {
changes[name] = true;
};
validator.fromJSON({
type: "text",
minLength: 11,
maxLength: 15,
allowDigits: true,
});
assert.equal(validator.minLength, 11, "minLength loaded");
assert.equal(validator.maxLength, 15, "maxLength loaded");
validator.minLength = 1;
assert.equal(changes["minLength"], true, "minLength changed");
validator.maxLength = 5;
assert.equal(changes["maxLength"], true, "maxLength changed");
validator.allowDigits = false;
assert.equal(changes["allowDigits"], true, "allowDigits changed");
}
);
QUnit.test("Text validator calls onPropertyValueChangedCallback", function(
assert
) {
var validator = new TextValidator();
var changes = {};
validator.onPropertyValueChangedCallback = (name) => {
changes[name] = true;
};
validator.fromJSON({
type: "text",
minLength: 11,
maxLength: 15,
allowDigits: true,
});
assert.equal(validator.minLength, 11, "minLength loaded");
assert.equal(validator.maxLength, 15, "maxLength loaded");
validator.minLength = 1;
assert.equal(changes["minLength"], true, "minLength changed");
validator.maxLength = 5;
assert.equal(changes["maxLength"], true, "maxLength changed");
validator.allowDigits = false;
assert.equal(changes["allowDigits"], true, "allowDigits changed");
});

export class CamelCaseValidator extends SurveyValidator {
public getType(): string {
Expand All @@ -177,13 +176,13 @@ export class CamelCaseValidator extends SurveyValidator {
Serializer.addClass(
"CamelCaseValidator",
[],
function () {
function() {
return new CamelCaseValidator();
},
"surveyvalidator"
);

QUnit.test("Support camel names in validators, Bug#994", function (assert) {
QUnit.test("Support camel names in validators, Bug#994", function(assert) {
var json = {
elements: [
{
Expand Down Expand Up @@ -214,7 +213,7 @@ QUnit.test("Support camel names in validators, Bug#994", function (assert) {

QUnit.test(
"Validators and isRequired in multipletext items, Bug#1055",
function (assert) {
function(assert) {
var json = {
questions: [
{
Expand Down Expand Up @@ -261,7 +260,7 @@ QUnit.test(

QUnit.test(
"text validator doesn't work correctly with empty text, Bug#1241",
function (assert) {
function(assert) {
var json = {
questions: [
{
Expand Down Expand Up @@ -297,7 +296,41 @@ QUnit.test(
}
);

QUnit.test("Expression validator", function (assert) {
QUnit.test(
"text validator doesn't work correctly with setting value to empty text, Bug#3065",
function(assert) {
var json = {
questions: [
{
type: "text",
name: "q1",
validators: [
{
type: "text",
minLength: 5,
},
],
},
],
};
var survey = new SurveyModel(json);
var q = survey.getQuestionByName("q1");
assert.equal(q.isEmpty(), true, "value is empty");
assert.equal(q.hasErrors(), false, "There is no errors, values are empty");
survey.setValue("q1", "abc");
assert.equal(q.isEmpty(), false, "value is not empty");
assert.equal(q.hasErrors(), true, "There is an error");
survey.setValue("q1", "");
assert.equal(q.isEmpty(), true, "value is empty #2");
assert.equal(
q.hasErrors(),
false,
"There is no error, again value is empty #2"
);
}
);

QUnit.test("Expression validator", function(assert) {
var json = {
questions: [
{
Expand Down Expand Up @@ -333,7 +366,7 @@ QUnit.test("Expression validator", function (assert) {
assert.equal(question.hasErrors(), false, "5 >= 3");
});

QUnit.test("Expression validator #2", function (assert) {
QUnit.test("Expression validator #2", function(assert) {
var json = {
questions: [
{
Expand Down Expand Up @@ -365,27 +398,27 @@ QUnit.test("Expression validator #2", function (assert) {
assert.equal(survey.isCurrentPageHasErrors, false, "satisfaction is low");
});

QUnit.test("ExceedSizeError", function (assert) {
QUnit.test("ExceedSizeError", function(assert) {
var error = new ExceedSizeError(102400);
assert.equal(error.getText(), "The file size should not exceed 100 KB.");
assert.equal(error.locText.text, "The file size should not exceed 100 KB.");
});

QUnit.test("MinRowCountError", function (assert) {
QUnit.test("MinRowCountError", function(assert) {
var error = new MinRowCountError(1);
assert.equal(error.getText(), "Please fill in at least 1 row(s).");
assert.equal(error.locText.text, "Please fill in at least 1 row(s).");
});

QUnit.test("Regex number validator, Bug#1775", function (assert) {
QUnit.test("Regex number validator, Bug#1775", function(assert) {
var validator = new RegexValidator("^0*(?:[2-9]|[1-9]dd*)$");
validator.text = "More than 0";
assert.equal(validator.validate(0).error.text, "More than 0", "0 give error");
assert.equal(validator.validate(2), null, "Parse correctly 2");
assert.equal(validator.validate(null), null, "Parse correctly null");
});

QUnit.test("validator.isAsync", function (assert) {
QUnit.test("validator.isAsync", function(assert) {
function asyncFunc(params: any): any {
this.returnResult(params[0] * 3);
return false;
Expand All @@ -411,7 +444,7 @@ QUnit.test("validator.isAsync", function (assert) {
FunctionFactory.Instance.unregister("asyncFunc");
});

QUnit.test("question with async validators", function (assert) {
QUnit.test("question with async validators", function(assert) {
var returnResult1: (res: any) => void;
var returnResult2: (res: any) => void;
function asyncFunc1(params: any): any {
Expand Down

0 comments on commit 148125b

Please sign in to comment.