Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Indicates the `type` of validator to use. Recognised type values are:
- `url`: Must be of type `url`.
- `hex`: Must be of type `hex`.
- `email`: Must be of type `email`.
- `tel`: Must be of type `tel`.
- `any`: Can be any type.

#### Required
Expand Down
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type RuleType =
| 'url'
| 'hex'
| 'email'
| 'tel'
| 'pattern'
| 'any';

Expand Down Expand Up @@ -146,6 +147,7 @@ export interface ValidateMessages {
float?: ValidateMessage<[FullField, Type]>;
regexp?: ValidateMessage<[FullField, Type]>;
email?: ValidateMessage<[FullField, Type]>;
tel?: ValidateMessage<[FullField, Type]>;
url?: ValidateMessage<[FullField, Type]>;
hex?: ValidateMessage<[FullField, Type]>;
};
Expand Down
1 change: 1 addition & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function newMessages(): InternalValidateMessages {
float: '%s is not a %s',
regexp: '%s is not a valid %s',
email: '%s is not a valid %s',
tel: '%s is not a valid %s',
url: '%s is not a valid %s',
hex: '%s is not a valid %s',
},
Expand Down
10 changes: 10 additions & 0 deletions src/rule/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const pattern = {
// '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
// 'i',
// ),
/**
* Phone number regex, support country code, brackets, spaces, dots, and dashes.
* @see https://regexr.com/3c53v
* @see https://ihateregex.io/expr/phone/
*/
tel: /^(\+[0-9]{1,3}[-\s\.]?)?(\([0-9]{1,4}\))?[-\s\.0-9]+$/,
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phone number regex pattern is too permissive and will accept invalid inputs. The pattern uses [-\s\.0-9]+$ as the main number part, which allows:

  • Single digit numbers like "1" or "12"
  • Only special characters like "---" or "..." or " "
  • Any combination of digits, spaces, dots, and dashes without requiring actual digits

Consider strengthening the regex to require:

  • A minimum number of actual digits (e.g., at least 7-10 digits depending on requirements)
  • At least one digit in the main number part to prevent inputs like "---" from being valid
  • More specific formatting rules if you want to enforce stricter validation

A more robust pattern might ensure that there are actual digits present and not just separators.

Suggested change
tel: /^(\+[0-9]{1,3}[-\s\.]?)?(\([0-9]{1,4}\))?[-\s\.0-9]+$/,
tel: /^(\+[0-9]{1,3}[-\s.]?)?(?:\([0-9]{1,4}\))?(?:[-\s.]?\d){7,}$/,

Copilot uses AI. Check for mistakes.
hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,
};

Expand Down Expand Up @@ -58,6 +64,9 @@ const types = {
email(value: Value) {
return typeof value === 'string' && value.length <= 320 && !!value.match(pattern.email);
},
tel(value: Value) {
return typeof value === 'string' && value.length <= 32 && !!value.match(pattern.tel);
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 32 character limit for phone numbers might be too restrictive for some international phone number formats that include country codes, area codes, and extensions. Consider whether this limit is appropriate for your use case. For reference, the E.164 standard allows up to 15 digits, but with formatting characters (spaces, dashes, parentheses, plus sign), phone numbers could legitimately exceed 32 characters, especially with extensions.

Suggested change
return typeof value === 'string' && value.length <= 32 && !!value.match(pattern.tel);
return typeof value === 'string' && value.length <= 64 && !!value.match(pattern.tel);

Copilot uses AI. Check for mistakes.
},
url(value: Value) {
return typeof value === 'string' && value.length <= 2048 && !!value.match(getUrlRegex());
},
Expand All @@ -79,6 +88,7 @@ const type: ExecuteRule = (rule, value, source, errors, options) => {
'object',
'method',
'email',
'tel',
'number',
'date',
'url',
Expand Down
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function isNativeStringType(type: string) {
type === 'hex' ||
type === 'email' ||
type === 'date' ||
type === 'pattern'
type === 'pattern' ||
type === 'tel'
);
}

Expand Down
1 change: 1 addition & 0 deletions src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default {
url: type,
hex: type,
email: type,
tel: type,
required,
any,
};
102 changes: 102 additions & 0 deletions tests/tel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Schema from '../src';

describe('tel', () => {
it('works for empty string', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('works for china mobile phone number', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '13156451303',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('works for china mobile phone number with country code', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '+8613156451303',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('works for china mobile phone number with spaces', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '+86 131 5645 1303',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('works for us phone number with brackets, dashes, and spaces', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '(123) 456-7890',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('works for required empty string', done => {
new Schema({
v: {
type: 'tel',
required: true,
},
}).validate(
{
v: '',
},
errors => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe('v is required');
done();
},
);
});

});
Comment on lines 1 to 165
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite lacks negative test cases to verify that invalid phone numbers are properly rejected. Consider adding test cases for invalid inputs such as:

  • Invalid formats like "abc123" or "123abc"
  • Single digit inputs like "1" or "12"
  • Only special characters like "---" or " "
  • Too long or too short phone numbers
  • Invalid character combinations

Other type validators in the codebase (e.g., url.spec.ts) include negative test cases to ensure invalid inputs are rejected. This helps ensure the regex pattern works correctly and catches edge cases.

Copilot uses AI. Check for mistakes.