@bcrs-shared-components/base-address 3.0.2 → 3.0.3

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.
@@ -1,42 +1,42 @@
1
- import { ref } from 'vue-demi'
2
-
3
- import { AddressValidationRules } from '@bcrs-shared-components/enums'
4
-
5
- /* Sets up form validation functions */
6
- export function useBaseValidations () {
7
- /* this variable must be named the same as your ref=___ in your form */
8
- const addressForm = ref(null)
9
- const resetValidation = () => {
10
- addressForm.value?.resetValidation()
11
- }
12
- const validate = () => {
13
- addressForm.value?.validate()
14
- }
15
- return { addressForm, resetValidation, validate }
16
- }
17
-
18
- /* Rules used in most schemas */
19
- export const baseRules = {
20
- [AddressValidationRules.BC]: (v: string) => v === 'BC' || v === 'British Columbia' || 'Address must be in BC',
21
- [AddressValidationRules.CANADA]: (v: string) => v === 'CA' || 'Address must be in Canada',
22
- [AddressValidationRules.MAX_LENGTH]: (max: number) => {
23
- return (v: string) => (v || '').length <= max || `Maximum ${max} characters`
24
- },
25
- [AddressValidationRules.MIN_LENGTH]: (min: number) => {
26
- return (v: string) => (v || '').length >= min || `Minimum ${min} characters`
27
- },
28
- [AddressValidationRules.POSTAL_CODE]: (v: string) => (
29
- /^\s*[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d\s*$/i.test(v) ||
30
- 'Must be a valid postal code'
31
- ),
32
- [AddressValidationRules.REQUIRED]: (v: string) => v?.length > 0 || 'This field is required',
33
- [AddressValidationRules.ZIP_CODE]: (v: string) => (
34
- /^\s*[0-9]{5}([\s-]?[0-9]{4})?\s*$/i.test(v) ||
35
- 'Must be a valid zip code'
36
- )
37
- }
38
-
39
- /* Array of validation rules used by input elements to prevent extra whitespace. */
40
- export const spaceRules = [
41
- (v: string) => !/^\s/g.test(v) || 'Invalid spaces' // leading spaces
42
- ]
1
+ import { ref } from 'vue-demi'
2
+
3
+ import { AddressValidationRules } from '@bcrs-shared-components/enums'
4
+
5
+ /* Sets up form validation functions */
6
+ export function useBaseValidations () {
7
+ /* this variable must be named the same as your ref=___ in your form */
8
+ const addressForm = ref(null)
9
+ const resetValidation = () => {
10
+ addressForm.value?.resetValidation()
11
+ }
12
+ const validate = () => {
13
+ addressForm.value?.validate()
14
+ }
15
+ return { addressForm, resetValidation, validate }
16
+ }
17
+
18
+ /* Rules used in most schemas */
19
+ export const baseRules = {
20
+ [AddressValidationRules.BC]: (v: string) => v === 'BC' || v === 'British Columbia' || 'Address must be in BC',
21
+ [AddressValidationRules.CANADA]: (v: string) => v === 'CA' || 'Address must be in Canada',
22
+ [AddressValidationRules.MAX_LENGTH]: (max: number) => {
23
+ return (v: string) => (v || '').length <= max || `Maximum ${max} characters`
24
+ },
25
+ [AddressValidationRules.MIN_LENGTH]: (min: number) => {
26
+ return (v: string) => (v || '').length >= min || `Minimum ${min} characters`
27
+ },
28
+ [AddressValidationRules.POSTAL_CODE]: (v: string) => (
29
+ /^\s*[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s-]?\d[ABCEGHJ-NPRSTV-Z]\d\s*$/i.test(v) ||
30
+ 'Must be a valid postal code'
31
+ ),
32
+ [AddressValidationRules.REQUIRED]: (v: string) => v?.length > 0 || 'This field is required',
33
+ [AddressValidationRules.ZIP_CODE]: (v: string) => (
34
+ /^\s*[0-9]{5}([\s-]?[0-9]{4})?\s*$/i.test(v) ||
35
+ 'Must be a valid zip code'
36
+ )
37
+ }
38
+
39
+ /* Array of validation rules used by input elements to prevent extra whitespace. */
40
+ export const spaceRules = [
41
+ (v: string) => !/^\s/g.test(v) || 'Invalid spaces' // leading spaces
42
+ ]
@@ -1,118 +1,118 @@
1
- /* FUTURE:
2
- * Delete this if we decide not to move forward with vuelidate
3
- * Fix it to work otherwise
4
- */
5
- import { computed, Ref, reactive } from 'vue-demi'
6
-
7
- import { AddressIF, SchemaIF } from '@bcrs-shared-components/interfaces'
8
-
9
- import useVuelidate from '@vuelidate/core'
10
-
11
- export function useValidations (schema: Ref<SchemaIF>, address: Ref<AddressIF>) {
12
- const validations = reactive({ addressLocal: schema.value })
13
- const $v = useVuelidate(validations, reactive({ addressLocal: address.value }))
14
-
15
- /**
16
- * Misc Vuetify rules.
17
- * @param prop The name of the property object to validate.
18
- * @param key The name of the property key (field) to validate.
19
- * @returns True if the rule passes, otherwise an error string.
20
- */
21
- const vuetifyRules = {
22
- requiredRule: (prop: string, key: string): boolean | string => {
23
- return Boolean($v.value[prop] && !$v.value[prop][key].required.$invalid) || 'This field is required'
24
- },
25
- minLengthRule: (prop: string, key: string): boolean | string => {
26
- const min = validations.addressLocal[key].max
27
- const msg = min ? `Minimum length is ${min}` : 'Text is below minimum length'
28
- return Boolean($v.value[prop] && !$v.value[prop][key].minLength.$invalid) || msg
29
- },
30
- maxLengthRule: (prop: string, key: string): boolean | string => {
31
- const max = validations.addressLocal[key].max
32
- const msg = max ? `Maximum length is ${max}` : 'Text is over maximum length'
33
- return Boolean($v.value[prop] && !$v.value[prop][key].maxLength.$invalid) || msg
34
- },
35
- // FUTURE: generalize this rule to take a validation parameter (ie, 'CA')
36
- isCanadaRule: (prop: string, key: string): boolean | string => {
37
- return Boolean($v.value[prop] && !$v.value[prop][key].isCanada.$invalid) || 'Address must be in Canada'
38
- },
39
- // FUTURE: generalize this rule to take a validation parameter (ie, 'BC')
40
- isBCRule: (prop: string, key: string): boolean | string => {
41
- return Boolean($v.value[prop] && !$v.value[prop][key].isBC.$invalid) || 'Address must be in BC'
42
- }
43
- }
44
-
45
- /**
46
- * Creates a Vuetify rules object from the Vuelidate state.
47
- * @param model The name of the model we are validating.
48
- * @returns A Vuetify rules object.
49
- */
50
- const createVuetifyRulesObject = (model: string): { [attr: string]: Array<Function> } => {
51
- const obj = {
52
- street: [],
53
- streetAdditional: [],
54
- city: [],
55
- region: [],
56
- postalCode: [],
57
- country: [],
58
- deliveryInstructions: []
59
- }
60
-
61
- // ensure Vuelidate state object is initialized
62
- if ($v && $v.value[model]) {
63
- // iterate over Vuelidate object properties
64
- Object.keys($v.value[model])
65
- // only look at validation properties
66
- .filter(prop => prop.charAt(0) !== '$')
67
- .forEach(prop => {
68
- // create array for each validation property
69
- obj[prop] = []
70
- // iterate over validation property params
71
- Object.keys($v.value[model][prop])
72
- .forEach(param => {
73
- // add specified validation functions to array
74
- switch (param) {
75
- case 'required': obj[prop].push(() => vuetifyRules.requiredRule(model, prop)); break
76
- case 'minLength': obj[prop].push(() => vuetifyRules.minLengthRule(model, prop)); break
77
- case 'maxLength': obj[prop].push(() => vuetifyRules.maxLengthRule(model, prop)); break
78
- case 'isCanada': obj[prop].push(() => vuetifyRules.isCanadaRule(model, prop)); break
79
- case 'isBC': obj[prop].push(() => vuetifyRules.isBCRule(model, prop)); break
80
- // FUTURE: add extra validation functions here
81
- default: break
82
- }
83
- })
84
- })
85
- }
86
-
87
- // sample return object
88
- // street: [
89
- // () => this.requiredRule('addressLocal', 'street'),
90
- // () => this.minLengthRule('addressLocal', 'street'),
91
- // () => this.maxLengthRule('addressLocal', 'street')
92
- // ],
93
- // ...
94
-
95
- return obj
96
- }
97
-
98
- /**
99
- * The Vuetify rules object. Used to display any validation errors/styling.
100
- * NB: As a getter, this is initialized between created() and mounted().
101
- * @returns the Vuetify validation rules object
102
- */
103
- const rules = computed((): { [attr: string]: Array<Function> } => {
104
- return createVuetifyRulesObject('addressLocal')
105
- })
106
-
107
- /** Array of validation rules used by input elements to prevent extra whitespace. */
108
- const spaceRules = [
109
- (v: string) => !/^\s/g.test(v) || 'Invalid spaces', // leading spaces
110
- (v: string) => !/\s$/g.test(v) || 'Invalid spaces', // trailing spaces
111
- (v: string) => !/\s\s/g.test(v) || 'Invalid word spacing' // multiple inline spaces
112
- ]
113
- return {
114
- $v,
115
- rules,
116
- spaceRules
117
- }
118
- }
1
+ /* FUTURE:
2
+ * Delete this if we decide not to move forward with vuelidate
3
+ * Fix it to work otherwise
4
+ */
5
+ import { computed, Ref, reactive } from 'vue-demi'
6
+
7
+ import { AddressIF, SchemaIF } from '@bcrs-shared-components/interfaces'
8
+
9
+ import useVuelidate from '@vuelidate/core'
10
+
11
+ export function useValidations (schema: Ref<SchemaIF>, address: Ref<AddressIF>) {
12
+ const validations = reactive({ addressLocal: schema.value })
13
+ const $v = useVuelidate(validations, reactive({ addressLocal: address.value }))
14
+
15
+ /**
16
+ * Misc Vuetify rules.
17
+ * @param prop The name of the property object to validate.
18
+ * @param key The name of the property key (field) to validate.
19
+ * @returns True if the rule passes, otherwise an error string.
20
+ */
21
+ const vuetifyRules = {
22
+ requiredRule: (prop: string, key: string): boolean | string => {
23
+ return Boolean($v.value[prop] && !$v.value[prop][key].required.$invalid) || 'This field is required'
24
+ },
25
+ minLengthRule: (prop: string, key: string): boolean | string => {
26
+ const min = validations.addressLocal[key].max
27
+ const msg = min ? `Minimum length is ${min}` : 'Text is below minimum length'
28
+ return Boolean($v.value[prop] && !$v.value[prop][key].minLength.$invalid) || msg
29
+ },
30
+ maxLengthRule: (prop: string, key: string): boolean | string => {
31
+ const max = validations.addressLocal[key].max
32
+ const msg = max ? `Maximum length is ${max}` : 'Text is over maximum length'
33
+ return Boolean($v.value[prop] && !$v.value[prop][key].maxLength.$invalid) || msg
34
+ },
35
+ // FUTURE: generalize this rule to take a validation parameter (ie, 'CA')
36
+ isCanadaRule: (prop: string, key: string): boolean | string => {
37
+ return Boolean($v.value[prop] && !$v.value[prop][key].isCanada.$invalid) || 'Address must be in Canada'
38
+ },
39
+ // FUTURE: generalize this rule to take a validation parameter (ie, 'BC')
40
+ isBCRule: (prop: string, key: string): boolean | string => {
41
+ return Boolean($v.value[prop] && !$v.value[prop][key].isBC.$invalid) || 'Address must be in BC'
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Creates a Vuetify rules object from the Vuelidate state.
47
+ * @param model The name of the model we are validating.
48
+ * @returns A Vuetify rules object.
49
+ */
50
+ const createVuetifyRulesObject = (model: string): { [attr: string]: Array<Function> } => {
51
+ const obj = {
52
+ street: [],
53
+ streetAdditional: [],
54
+ city: [],
55
+ region: [],
56
+ postalCode: [],
57
+ country: [],
58
+ deliveryInstructions: []
59
+ }
60
+
61
+ // ensure Vuelidate state object is initialized
62
+ if ($v && $v.value[model]) {
63
+ // iterate over Vuelidate object properties
64
+ Object.keys($v.value[model])
65
+ // only look at validation properties
66
+ .filter(prop => prop.charAt(0) !== '$')
67
+ .forEach(prop => {
68
+ // create array for each validation property
69
+ obj[prop] = []
70
+ // iterate over validation property params
71
+ Object.keys($v.value[model][prop])
72
+ .forEach(param => {
73
+ // add specified validation functions to array
74
+ switch (param) {
75
+ case 'required': obj[prop].push(() => vuetifyRules.requiredRule(model, prop)); break
76
+ case 'minLength': obj[prop].push(() => vuetifyRules.minLengthRule(model, prop)); break
77
+ case 'maxLength': obj[prop].push(() => vuetifyRules.maxLengthRule(model, prop)); break
78
+ case 'isCanada': obj[prop].push(() => vuetifyRules.isCanadaRule(model, prop)); break
79
+ case 'isBC': obj[prop].push(() => vuetifyRules.isBCRule(model, prop)); break
80
+ // FUTURE: add extra validation functions here
81
+ default: break
82
+ }
83
+ })
84
+ })
85
+ }
86
+
87
+ // sample return object
88
+ // street: [
89
+ // () => this.requiredRule('addressLocal', 'street'),
90
+ // () => this.minLengthRule('addressLocal', 'street'),
91
+ // () => this.maxLengthRule('addressLocal', 'street')
92
+ // ],
93
+ // ...
94
+
95
+ return obj
96
+ }
97
+
98
+ /**
99
+ * The Vuetify rules object. Used to display any validation errors/styling.
100
+ * NB: As a getter, this is initialized between created() and mounted().
101
+ * @returns the Vuetify validation rules object
102
+ */
103
+ const rules = computed((): { [attr: string]: Array<Function> } => {
104
+ return createVuetifyRulesObject('addressLocal')
105
+ })
106
+
107
+ /** Array of validation rules used by input elements to prevent extra whitespace. */
108
+ const spaceRules = [
109
+ (v: string) => !/^\s/g.test(v) || 'Invalid spaces', // leading spaces
110
+ (v: string) => !/\s$/g.test(v) || 'Invalid spaces', // trailing spaces
111
+ (v: string) => !/\s\s/g.test(v) || 'Invalid word spacing' // multiple inline spaces
112
+ ]
113
+ return {
114
+ $v,
115
+ rules,
116
+ spaceRules
117
+ }
118
+ }
package/index.ts CHANGED
@@ -1 +1 @@
1
- export { default as BaseAddress } from './BaseAddress.vue'
1
+ export { default as BaseAddress } from './BaseAddress.vue'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bcrs-shared-components/base-address",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -17,5 +17,5 @@
17
17
  "vue-property-decorator": "^9.1.2",
18
18
  "vuelidate-property-decorators": "1.0.28"
19
19
  },
20
- "gitHead": "5f3b43cd73175662b749d8831ed3b207b3fe44fa"
20
+ "gitHead": "d80832aa4a621fdabf26d3e1d479f0701cf50fad"
21
21
  }
@@ -1,42 +1,42 @@
1
- import { AddressValidationRules } from '@bcrs-shared-components/enums'
2
- import { SchemaIF } from '@bcrs-shared-components/interfaces'
3
- import { baseRules, spaceRules } from '../factories/validation-factory'
4
-
5
- /* example of what to pass in for the schema */
6
- export const DefaultSchema: SchemaIF = {
7
- street: [
8
- baseRules[AddressValidationRules.REQUIRED],
9
- baseRules[AddressValidationRules.MAX_LENGTH](50),
10
- ...spaceRules
11
- ],
12
- streetAdditional: [
13
- baseRules[AddressValidationRules.MAX_LENGTH](50),
14
- ...spaceRules
15
- ],
16
- city: [
17
- baseRules[AddressValidationRules.REQUIRED],
18
- baseRules[AddressValidationRules.MAX_LENGTH](40),
19
- ...spaceRules
20
- ],
21
- country: [
22
- baseRules[AddressValidationRules.REQUIRED],
23
- ...spaceRules
24
- ],
25
- region: [
26
- baseRules[AddressValidationRules.REQUIRED],
27
- ...spaceRules
28
- ],
29
- /* NOTE: Canada/US postal code and zip code regex rules
30
- * are added automatically as extra rules based on country
31
- * inside the address components
32
- */
33
- postalCode: [
34
- baseRules[AddressValidationRules.REQUIRED],
35
- baseRules[AddressValidationRules.MAX_LENGTH](15),
36
- ...spaceRules
37
- ],
38
- deliveryInstructions: [
39
- baseRules[AddressValidationRules.MAX_LENGTH](80),
40
- ...spaceRules
41
- ]
42
- }
1
+ import { AddressValidationRules } from '@bcrs-shared-components/enums'
2
+ import { SchemaIF } from '@bcrs-shared-components/interfaces'
3
+ import { baseRules, spaceRules } from '../factories/validation-factory'
4
+
5
+ /* example of what to pass in for the schema */
6
+ export const DefaultSchema: SchemaIF = {
7
+ street: [
8
+ baseRules[AddressValidationRules.REQUIRED],
9
+ baseRules[AddressValidationRules.MAX_LENGTH](50),
10
+ ...spaceRules
11
+ ],
12
+ streetAdditional: [
13
+ baseRules[AddressValidationRules.MAX_LENGTH](50),
14
+ ...spaceRules
15
+ ],
16
+ city: [
17
+ baseRules[AddressValidationRules.REQUIRED],
18
+ baseRules[AddressValidationRules.MAX_LENGTH](40),
19
+ ...spaceRules
20
+ ],
21
+ country: [
22
+ baseRules[AddressValidationRules.REQUIRED],
23
+ ...spaceRules
24
+ ],
25
+ region: [
26
+ baseRules[AddressValidationRules.REQUIRED],
27
+ ...spaceRules
28
+ ],
29
+ /* NOTE: Canada/US postal code and zip code regex rules
30
+ * are added automatically as extra rules based on country
31
+ * inside the address components
32
+ */
33
+ postalCode: [
34
+ baseRules[AddressValidationRules.REQUIRED],
35
+ baseRules[AddressValidationRules.MAX_LENGTH](15),
36
+ ...spaceRules
37
+ ],
38
+ deliveryInstructions: [
39
+ baseRules[AddressValidationRules.MAX_LENGTH](80),
40
+ ...spaceRules
41
+ ]
42
+ }
@@ -1 +1 @@
1
- export { DefaultSchema } from './default-schema'
1
+ export { DefaultSchema } from './default-schema'