-
Notifications
You must be signed in to change notification settings - Fork 37
Required postal code #302
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
Merged
Merged
Required postal code #302
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './validate-postal-code' | ||
| export * from './is-valid-postal-code' | ||
| export * from './is-required-postal-code' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * Countries that do not use postal codes. | ||
| * Postal code is not required for addresses in these countries. | ||
| */ | ||
| const NO_POSTAL_CODE_COUNTRY_CODES = new Set([ | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'AO', 'AG', 'AW', 'BS', 'BZ', 'BJ', 'BM', 'BO', 'BQ', 'BW', 'BF', 'BI', | ||
| 'CM', 'CF', 'TD', 'KM', 'CG', 'CD', 'CK', 'CI', 'CW', 'DJ', 'DM', 'GQ', | ||
| 'ER', 'FJ', 'TF', 'GA', 'GM', 'GH', 'GD', 'GY', 'HM', 'HK', | ||
| 'KI', 'KP', 'LY', 'MO', 'MW', 'ML', 'MR', 'NR', | ||
| 'AN', 'NU', 'QA', 'RW', 'KN', 'ST', 'SC', 'SL', 'SX', 'SB', 'SO', 'SR', 'SY', | ||
| 'TL', 'TG', 'TK', 'TO', 'TT', 'TV', 'UG', 'AE', 'VU', 'YE', 'ZW' | ||
| ]) | ||
|
|
||
| /** | ||
| * Custom validator for postal code required. | ||
| * Returns true if postal code has a value, or if the country doesn't use postal codes. | ||
| */ | ||
| export function isRequiredPostalCode (value: string, parentVm: any): boolean { | ||
loneil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If the country doesn't use postal codes, no value is required | ||
| if (NO_POSTAL_CODE_COUNTRY_CODES.has(parentVm.addressCountry)) { | ||
| return true | ||
| } | ||
| // Otherwise, postal code is required | ||
| return !!value?.trim() | ||
loneil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import Vue from 'vue' | ||
| import { shallowMount } from '@vue/test-utils' | ||
| import { isRequiredPostalCode } from '@/validators' | ||
|
|
||
| const Dummy = Vue.component('DummyComponent', { template: '<div />' }) | ||
|
|
||
| describe('Validate Postal Code Required', () => { | ||
| let vm: any | ||
|
|
||
| beforeAll(async () => { | ||
| // mount the component and wait for everything to stabilize | ||
| // (this can be any component since we are not really using it) | ||
| const wrapper = shallowMount(Dummy) | ||
| vm = wrapper.vm | ||
| await Vue.nextTick() | ||
| }) | ||
|
|
||
loneil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| it.each([ | ||
| 'A1A 1A1', | ||
| '12345', | ||
| 'some-postal-code', | ||
| ' value ' | ||
| ])('returns true when postal code "%s" is provided for a country requiring postal codes', (postalCode) => { | ||
| expect(isRequiredPostalCode(postalCode, { addressCountry: 'CA' })).toBe(true) | ||
| expect(isRequiredPostalCode(postalCode, { addressCountry: 'US' })).toBe(true) | ||
| }) | ||
|
|
||
| it.each([ | ||
| '', | ||
| ' ', | ||
| null, | ||
| undefined | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ])('returns false when postal code is "%s" for a country requiring postal codes', (postalCode) => { | ||
| expect(isRequiredPostalCode(postalCode as any, { addressCountry: 'CA' })).toBe(false) | ||
| expect(isRequiredPostalCode(postalCode as any, { addressCountry: 'US' })).toBe(false) | ||
| }) | ||
|
|
||
| it.each([ | ||
| 'AO', 'AG', 'AW', 'BS', 'BZ', 'BJ', 'BM', 'BO', 'BQ', 'BW', 'BF', 'BI', | ||
| 'CM', 'CF', 'TD', 'KM', 'CG', 'CD', 'CK', 'CI', 'CW', 'DJ', 'DM', 'GQ', | ||
| 'ER', 'FJ', 'TF', 'GA', 'GM', 'GH', 'GD', 'GY', 'HM', 'HK', | ||
| 'KI', 'KP', 'LY', 'MO', 'MW', 'ML', 'MR', 'NR', | ||
| 'AN', 'NU', 'QA', 'RW', 'KN', 'ST', 'SC', 'SL', 'SX', 'SB', 'SO', 'SR', 'SY', | ||
| 'TL', 'TG', 'TK', 'TO', 'TT', 'TV', 'UG', 'AE', 'VU', 'YE', 'ZW' | ||
| ])('returns true for country "%s" even when postal code is empty', (countryCode) => { | ||
| expect(isRequiredPostalCode('', { addressCountry: countryCode })).toBe(true) | ||
| expect(isRequiredPostalCode(' ', { addressCountry: countryCode })).toBe(true) | ||
| }) | ||
|
|
||
| it.each([ | ||
| 'AO', 'HK', 'AE', 'QA', 'ZW' | ||
| ])('returns true for country "%s" when postal code is provided', (countryCode) => { | ||
| expect(isRequiredPostalCode('12345', { addressCountry: countryCode })).toBe(true) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.