Skip to content

Commit c90e21a

Browse files
committed
fix: Align validators to native browser behavior
1 parent 43f46eb commit c90e21a

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/components/common/mixins/form-associated.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('Form associated mixin tests', () => {
121121
});
122122

123123
it('initial invalid with constraints', async () => {
124-
await createFixture({ minLength: 3 });
124+
await createFixture({ minLength: 3, value: 'a' });
125125

126126
expect(instance.checkValidity()).to.be.false;
127127
expect(hasValidityFlags(instance, 'tooShort')).to.be.true;

src/components/common/validators.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,81 +33,82 @@ export const requiredBooleanValidator: Validator<{
3333
};
3434

3535
export const minLengthValidator: Validator<{
36-
minLength: number;
36+
minLength?: number;
3737
value: string;
3838
}> = {
3939
key: 'tooShort',
4040
message: ({ minLength }) =>
4141
formatString(validatorMessages.minLength, minLength),
4242
isValid: ({ minLength, value }) =>
43-
minLength ? value.length >= minLength : true,
43+
minLength && value ? value.length >= asNumber(minLength) : true,
4444
};
4545

4646
export const maxLengthValidator: Validator<{
47-
maxLength: number;
47+
maxLength?: number;
4848
value: string;
4949
}> = {
5050
key: 'tooLong',
5151
message: ({ maxLength }) =>
5252
formatString(validatorMessages.maxLength, maxLength),
5353
isValid: ({ maxLength, value }) =>
54-
maxLength ? value.length <= maxLength : true,
54+
maxLength && value ? value.length <= asNumber(maxLength) : true,
5555
};
5656

57-
export const patternValidator: Validator<{ pattern: string; value: string }> = {
58-
key: 'patternMismatch',
59-
message: validatorMessages.pattern,
60-
isValid: ({ pattern, value }) =>
61-
pattern ? new RegExp(pattern, 'v').test(value) : true,
62-
};
57+
export const patternValidator: Validator<{ pattern?: string; value: string }> =
58+
{
59+
key: 'patternMismatch',
60+
message: validatorMessages.pattern,
61+
isValid: ({ pattern, value }) =>
62+
pattern && value ? new RegExp(pattern, 'u').test(value) : true,
63+
};
6364

6465
export const minValidator: Validator<{
65-
min: number | string;
66+
min?: number;
6667
value: number | string;
6768
}> = {
6869
key: 'rangeUnderflow',
6970
message: ({ min }) => formatString(validatorMessages.min, min),
7071
isValid: ({ min, value }) =>
71-
isDefined(min)
72-
? isDefined(value) && asNumber(value) >= asNumber(min)
72+
isDefined(value) && value !== '' && isDefined(min)
73+
? asNumber(value) >= asNumber(min)
7374
: true,
7475
};
7576

7677
export const maxValidator: Validator<{
77-
max: number | string;
78+
max?: number;
7879
value: number | string;
7980
}> = {
8081
key: 'rangeOverflow',
8182
message: ({ max }) => formatString(validatorMessages.max, max),
8283
isValid: ({ max, value }) =>
83-
isDefined(max)
84-
? isDefined(value) && asNumber(value) <= asNumber(max)
84+
isDefined(value) && value !== '' && isDefined(max)
85+
? asNumber(value) <= asNumber(max)
8586
: true,
8687
};
8788

8889
export const stepValidator: Validator<{
89-
min: number | string;
90-
step: number | string;
90+
min?: number;
91+
step?: number;
9192
value: number | string;
9293
}> = {
9394
key: 'stepMismatch',
9495
message: 'Value does not conform to step constraint',
9596
isValid: ({ min, step, value }) =>
96-
isDefined(step)
97+
isDefined(value) && value !== '' && isDefined(step)
9798
? (asNumber(value) - asNumber(min)) % asNumber(step, 1) === 0
9899
: true,
99100
};
100101

101102
export const emailValidator: Validator<{ value: string }> = {
102103
key: 'typeMismatch',
103104
message: validatorMessages.email,
104-
isValid: ({ value }) => emailRegex.test(value),
105+
isValid: ({ value }) => (value ? emailRegex.test(value) : true),
105106
};
106107

107108
export const urlValidator: Validator<{ value: string }> = {
108109
key: 'typeMismatch',
109110
message: validatorMessages.url,
110-
isValid: ({ value }) => URL.canParse(value),
111+
isValid: ({ value }) => (value ? URL.canParse(value) : true),
111112
};
112113

113114
export const minDateValidator: Validator<{

0 commit comments

Comments
 (0)