-
Notifications
You must be signed in to change notification settings - Fork 37
31953 Added new validators package #301
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './validate-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,12 @@ | ||
| { | ||
| "name": "@bcrs-shared-components/validators", | ||
| "version": "0.0.0", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lerna will update this when I run it after merging this change.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll choose 1.0.0 for the first published version :) |
||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| }, | ||
| "devDependencies": { | ||
| }, | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "gitHead": "31c1b1c7fa82101f90ccd7842e436608bdfca752" | ||
| } | ||
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,14 @@ | ||
| /** | ||
| * Canadian postal code regex (eg, accepts A1A 1A1 or A1A1A1). | ||
| * Ref: https://en.wikipedia.org/wiki/Postal_codes_in_Canada | ||
| */ | ||
| const CanadaPostalCodeRegex = /^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z][ ]?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/i | ||
|
|
||
| /** Custom validator for postal codes. */ | ||
| export function validatePostalCode (value: string, parentVm: any): boolean { | ||
| // if Canada, validate postal code format | ||
| // empty value is considered valid -- "required" validation is handled separately | ||
| if (parentVm.addressCountry === 'CA') return !value || CanadaPostalCodeRegex.test(value) | ||
| // otherwise, no validation | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true | ||
severinbeauvais 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import Vue from 'vue' | ||
| import { shallowMount } from '@vue/test-utils' | ||
| import { validatePostalCode } from '@/validators' | ||
|
|
||
| const Dummy = Vue.component('DummyComponent', { template: '<div />' }) | ||
|
|
||
| describe('Validate Postal Code', () => { | ||
| 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() | ||
| }) | ||
|
|
||
| it.each([ | ||
| '', // empty value is valid | ||
| 'A1A 1A1', | ||
| 'A1A1A1', | ||
| 'a1a 1a1', | ||
| 'a1a1a1' | ||
| ])('accepts valid postal code "%s" for Canada', (postalCode) => { | ||
| expect(validatePostalCode(postalCode, { addressCountry: 'CA' })).toBe(true) | ||
| }) | ||
|
|
||
| it.each([ | ||
| '123456', | ||
| 'A1A-1A1', | ||
| 'AA1 1A1', | ||
| 'A11 1A1', | ||
| 'A1 A1A1', | ||
| 'A1A 1AA', | ||
| 'A1A 11A', | ||
| ' A1A 1A1', | ||
| 'A1A 1A1', | ||
| 'A1A 1A1 ', | ||
| 'D1D 1D1', // D is not allowed in Canadian postal codes | ||
| 'W1W 1W1', // W is not allowed in Canadian postal codes | ||
| 'Z1Z 1Z1' // Z is not allowed in Canadian postal codes | ||
| ])('rejects invalid postal code "%s" for Canada', (postalCode) => { | ||
| expect(validatePostalCode(postalCode, { addressCountry: 'CA' })).toBe(false) | ||
| }) | ||
|
|
||
| it.each([ | ||
| '', | ||
| 'invalid', | ||
| 'A1A 1A1', | ||
| 'A1A-1A1', | ||
| 'D1D 1D1', | ||
| 'W1W 1W1', | ||
| 'Z1Z 1Z1' | ||
| ])('accepts any postal code "%s" for non-Canada', (postalCode) => { | ||
| expect(validatePostalCode(postalCode, { addressCountry: 'XX' })).toBe(true) | ||
| }) | ||
| }) |
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.
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.
I followed my own instructions here:
https://github.com/bcgov/bcrs-shared-components/blob/main/README.md#special-procedures