Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/rule/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ 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, and dashes (or non-breaking hyphen \u2011).
* @see https://regexr.com/3c53v
* @see https://ihateregex.io/expr/phone/
* @see https://developers.google.com/style/phone-numbers using non-breaking hyphen \u2011
*/
tel: /^(\+[0-9]{1,3}[-\s\u2011]?)?(\([0-9]{1,4}\)[-\s\u2011]?)?([0-9]+[-\s\u2011]?)*[0-9]+$/,
hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,
};

Expand Down Expand Up @@ -58,6 +65,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 +89,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,
};
165 changes: 165 additions & 0 deletions tests/tel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
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 dashes', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '415-555-0132',
},
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 us phone number with nonbreaking hyphen', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '415‑555‑0132',
},
errors => {
expect(errors).toBe(null);
done();
},
);
});

it('forbid multiple spaces in a row', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '123 456',
},
errors => {
expect(errors[0].message).toBe('v is not a valid tel');
done();
},
);
});

it('forbid multiple dashes in a row', done => {
new Schema({
v: {
type: 'tel',
},
}).validate(
{
v: '123---456',
},
errors => {
expect(errors[0].message).toBe('v is not a valid tel');
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();
},
);
});
});