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
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"src/interfaces",
"src/mixins",
"src/modules/*",
"src/services",
"src/types",
"src/services"
"src/validators"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

],
"command": {
"publish": {
Expand Down
1 change: 1 addition & 0 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './validate-postal-code'
12 changes: 12 additions & 0 deletions src/validators/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@bcrs-shared-components/validators",
"version": "0.0.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lerna will update this when I run it after merging this change.

Copy link
Collaborator Author

@severinbeauvais severinbeauvais Feb 9, 2026

Choose a reason for hiding this comment

The 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": {
},
"gitHead": "31c1b1c7fa82101f90ccd7842e436608bdfca752"
}
14 changes: 14 additions & 0 deletions src/validators/validate-postal-code.ts
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
return true
}
57 changes: 57 additions & 0 deletions tests/unit/validate-postal-code.spec.ts
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)
})
})
Loading