Skip to content

Commit

Permalink
Revert "feat(): change required logic \n\n BREAKING CHANGE: rules for…
Browse files Browse the repository at this point in the history
… maxLength, maxNumber etc. will not raise an error if the field is empty and there is no required rule. To validate for required you should defined required rule directly."

This reverts commit 62561a9.
  • Loading branch information
horprogs committed Dec 27, 2021
1 parent aad6d4c commit a1bce7e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
6 changes: 0 additions & 6 deletions site/examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,6 @@ <h2>

validation
.addField('#example4_name', [
{
rule: 'required',
},
{
rule: 'minLength',
value: 3,
Expand Down Expand Up @@ -838,9 +835,6 @@ <h2>

validation
.addField('#example5_name', [
{
rule: 'required',
},
{
rule: 'minLength',
value: 3,
Expand Down
6 changes: 5 additions & 1 deletion src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ const basicExample = () => {
validation
.addField('#basic_name', [
{
rule: 'maxNumber' as Rules,
rule: 'minNumber' as Rules,
value: 10,
},
{
rule: 'maxNumber' as Rules,
value: 100,
},
])
.onSuccess((ev) => {
ev?.preventDefault();
Expand Down
44 changes: 8 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,26 +252,22 @@ class JustValidate {
`Value for ${fieldRule.rule} rule for [${field}] field is not defined. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof ruleValue !== 'number') {
console.error(
`Value for ${fieldRule.rule} rule for [${field}] should be a number. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof elemValue !== 'string') {
this.setFieldInvalid(field, fieldRule);
break;
}

if (elemValue === '') {
break;
}

if (isLengthMoreThanMax(elemValue, ruleValue)) {
this.setFieldInvalid(field, fieldRule);
}
Expand All @@ -284,26 +280,22 @@ class JustValidate {
`Value for ${fieldRule.rule} rule for [${field}] field is not defined. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof ruleValue !== 'number') {
console.error(
`Value for ${fieldRule.rule} rule for [${field}] should be a number. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof elemValue !== 'string') {
this.setFieldInvalid(field, fieldRule);
break;
}

if (elemValue === '') {
break;
}

if (isLengthLessThanMin(elemValue, ruleValue)) {
this.setFieldInvalid(field, fieldRule);
}
Expand All @@ -316,10 +308,6 @@ class JustValidate {
break;
}

if (elemValue === '') {
break;
}

if (!isPassword(elemValue)) {
this.setFieldInvalid(field, fieldRule);
}
Expand All @@ -332,10 +320,6 @@ class JustValidate {
break;
}

if (elemValue === '') {
break;
}

if (!isStrongPassword(elemValue)) {
this.setFieldInvalid(field, fieldRule);
}
Expand All @@ -348,10 +332,6 @@ class JustValidate {
break;
}

if (elemValue === '') {
break;
}

if (!isNumber(elemValue)) {
this.setFieldInvalid(field, fieldRule);
}
Expand All @@ -364,26 +344,22 @@ class JustValidate {
`Value for ${fieldRule.rule} rule for [${field}] field is not defined. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof ruleValue !== 'number') {
console.error(
`Value for ${fieldRule.rule} rule for [${field}] field should be a number. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof elemValue !== 'string') {
this.setFieldInvalid(field, fieldRule);
break;
}

if (elemValue === '') {
break;
}

const num = +elemValue;

if (Number.isNaN(num) || isNumberMoreThanMax(num, ruleValue)) {
Expand All @@ -398,26 +374,22 @@ class JustValidate {
`Value for ${fieldRule.rule} rule for [${field}] field is not defined. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof ruleValue !== 'number') {
console.error(
`Value for ${fieldRule.rule} rule for [${field}] field should be a number. The field will be always invalid.`
);
this.setFieldInvalid(field, fieldRule);
break;
return;
}

if (typeof elemValue !== 'string') {
this.setFieldInvalid(field, fieldRule);
break;
}

if (elemValue === '') {
break;
}

const num = +elemValue;

if (Number.isNaN(num) || isNumberLessThanMin(num, ruleValue)) {
Expand Down
14 changes: 8 additions & 6 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,11 @@ describe('Validation', () => {
expect(getElem('button')).toBeEnabled();
});

expect(onSubmit).toHaveBeenCalled();
onSubmit.mockReset();
expect(onSubmit).not.toHaveBeenCalled();

expect(getElemByTestId('error-label-#password')).not.toBeInTheDocument();
expect(getElemByTestId('error-label-#password')).toHaveTextContent(
'Password must contain minimum eight characters, at least one letter and one number'
);

changeTextBySelector('#password', '12345678');
clickBySelector('#submit-btn');
Expand Down Expand Up @@ -1140,10 +1141,11 @@ describe('Validation', () => {
expect(getElem('button')).toBeEnabled();
});

expect(onSubmit).toHaveBeenCalled();
onSubmit.mockReset();
expect(onSubmit).not.toHaveBeenCalled();

expect(getElemByTestId('error-label-#password')).not.toBeInTheDocument();
expect(getElemByTestId('error-label-#password')).toHaveTextContent(
'Password should contain minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character'
);

changeTextBySelector('#password', '12345678a');
clickBySelector('#submit-btn');
Expand Down

0 comments on commit a1bce7e

Please sign in to comment.