-
-
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 all commits
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,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, | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -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); | ||||||
|
||||||
| 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,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(); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.