-
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: support tel type validator #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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]+$/, | ||||||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
guoyunhe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i, | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -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); | ||||||
guoyunhe marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| return typeof value === 'string' && value.length <= 32 && !!value.match(pattern.tel); | |
| return typeof value === 'string' && value.length <= 64 && !!value.match(pattern.tel); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ export default { | |
| url: type, | ||
| hex: type, | ||
| email: type, | ||
| tel: type, | ||
| required, | ||
| any, | ||
| }; | ||
| 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
|
||
There was a problem hiding this comment.
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:Consider strengthening the regex to require:
A more robust pattern might ensure that there are actual digits present and not just separators.