@sbc-connect/nuxt-forms 0.1.32 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @sbc-connect/nuxt-forms
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#105](https://github.com/bcgov/connect-nuxt/pull/105) [`78195c7`](https://github.com/bcgov/connect-nuxt/commit/78195c765aa337be53fbb4ecf60e45bea44b36ff) Thanks [@deetz99](https://github.com/deetz99)! - Add strict Postal and Zip code validation for CA or US addresses.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`d714785`](https://github.com/bcgov/connect-nuxt/commit/d71478573f1fe11be34d5d588d68fb75eb5fd159), [`5f1b383`](https://github.com/bcgov/connect-nuxt/commit/5f1b3839ed993654a7f20fe161047ba38ab86274)]:
12
+ - @sbc-connect/nuxt-base@0.3.0
13
+
14
+ ## 0.1.33
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [[`09323e1`](https://github.com/bcgov/connect-nuxt/commit/09323e1fcaff9a2738528c8161288c1d4bcd0677), [`033f6c6`](https://github.com/bcgov/connect-nuxt/commit/033f6c624fbf953f1d7cf491c876cc9b58619b22)]:
19
+ - @sbc-connect/nuxt-base@0.2.0
20
+
3
21
  ## 0.1.32
4
22
 
5
23
  ### Patch Changes
@@ -1,5 +1,17 @@
1
1
  import { z } from 'zod'
2
2
 
3
+ /**
4
+ * Canadian postal code regex (eg, accepts A1A 1A1 or A1A1A1).
5
+ * Ref: https://en.wikipedia.org/wiki/Postal_codes_in_Canada
6
+ */
7
+ const CanadaPostalCodeRegex = /^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z][ ]?[0-9][ABCEGHJ-NPRSTV-Z][0-9]$/i
8
+
9
+ /**
10
+ * US ZIP code regex (eg, accepts 12345 or 12345-6789).
11
+ * Ref: https://faq.usps.com/s/article/ZIP-Code-The-Basics
12
+ */
13
+ const USZipCodeRegex = /^\d{5}(?:-\d{4})?$/
14
+
3
15
  export function getRequiredAddressSchema() {
4
16
  const t = useNuxtApp().$i18n.t
5
17
 
@@ -21,8 +33,8 @@ export function getRequiredAddressSchema() {
21
33
  .optional(),
22
34
  postalCode: z
23
35
  .string()
24
- .min(1, t('connect.validation.fieldRequired'))
25
- .max(15, t('connect.validation.maxChars', { count: 15 })),
36
+ .max(15, t('connect.validation.maxChars', { count: 15 }))
37
+ .optional(),
26
38
  country: z
27
39
  .string()
28
40
  .min(1, t('connect.validation.fieldRequired')),
@@ -31,11 +43,7 @@ export function getRequiredAddressSchema() {
31
43
  .max(80, t('connect.validation.maxChars', { count: 80 }))
32
44
  .optional()
33
45
  }).superRefine((data, ctx) => {
34
- // validate region based on country
35
- // required if country is US or CA
36
- // optional and max 2 characters if not US or CA
37
- const country = data.country
38
- const region = data.region
46
+ const { country, region, postalCode } = data
39
47
 
40
48
  if (region && region.length > 2) {
41
49
  ctx.addIssue({
@@ -45,12 +53,46 @@ export function getRequiredAddressSchema() {
45
53
  })
46
54
  }
47
55
 
48
- if ((country === 'US' || country === 'CA') && region?.length === 0) {
49
- ctx.addIssue({
50
- code: 'custom',
51
- message: t('connect.validation.fieldRequired'),
52
- path: ['region']
53
- })
56
+ const isRequiredRegion = country === 'US' || country === 'CA'
57
+
58
+ if (isRequiredRegion) {
59
+ if (!region) {
60
+ ctx.addIssue({
61
+ code: 'custom',
62
+ message: t('connect.validation.fieldRequired'),
63
+ path: ['region']
64
+ })
65
+ }
66
+
67
+ if (!postalCode) {
68
+ ctx.addIssue({
69
+ code: 'custom',
70
+ message: t('connect.validation.fieldRequired'),
71
+ path: ['postalCode']
72
+ })
73
+ }
74
+ }
75
+
76
+ if (!postalCode) {
77
+ return
78
+ }
79
+
80
+ if (country === 'CA') {
81
+ if (!CanadaPostalCodeRegex.test(postalCode)) {
82
+ ctx.addIssue({
83
+ code: 'custom',
84
+ message: t('connect.validation.invalidPostalCodeFormat'),
85
+ path: ['postalCode']
86
+ })
87
+ }
88
+ } else if (country === 'US') {
89
+ if (!USZipCodeRegex.test(postalCode)) {
90
+ ctx.addIssue({
91
+ code: 'custom',
92
+ message: t('connect.validation.invalidZipCodeFormat'),
93
+ path: ['postalCode']
94
+ })
95
+ }
54
96
  }
55
97
  })
56
98
  }
@@ -80,10 +122,8 @@ export function getNonRequiredAddressSchema() {
80
122
  .optional(),
81
123
  postalCode: z
82
124
  .string()
83
- .min(1, t('connect.validation.fieldRequired'))
84
125
  .max(15, t('connect.validation.maxChars', { count: 15 }))
85
- .optional()
86
- .or(z.literal('')),
126
+ .optional(),
87
127
  country: z
88
128
  .string()
89
129
  .min(1, t('connect.validation.fieldRequired'))
@@ -38,7 +38,9 @@ export default {
38
38
  fieldRequired: 'This field is required',
39
39
  maxChars: 'Maximum 0 characters | Maximum 1 character | Maximum {count} characters', // 0/1 most likely will never be used but required for pluralization
40
40
  minChars: 'Minimum 0 characters | Minimum 1 character | Minimum {count} characters', // 0/1 most likely will never be used but required for pluralization
41
- required: 'Required'
41
+ required: 'Required',
42
+ invalidPostalCodeFormat: 'Invalid Postal Code format (e.g., 1X1 X1X)',
43
+ invalidZipCodeFormat: 'Invalid ZIP Code format (e.g., 12345 or 12345-6789)'
42
44
  }
43
45
  }
44
46
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sbc-connect/nuxt-forms",
3
3
  "type": "module",
4
- "version": "0.1.32",
4
+ "version": "0.2.0",
5
5
  "repository": "github:bcgov/connect-nuxt",
6
6
  "license": "BSD-3-Clause",
7
7
  "main": "./nuxt.config.ts",
@@ -17,7 +17,7 @@
17
17
  "@sbc-connect/vitest-config": "0.0.6"
18
18
  },
19
19
  "dependencies": {
20
- "@sbc-connect/nuxt-base": "0.1.29"
20
+ "@sbc-connect/nuxt-base": "0.3.0"
21
21
  },
22
22
  "scripts": {
23
23
  "preinstall": "npx only-allow pnpm",