@wmfs/json-to-vuelidate 1.22.3 → 1.24.1

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,3 +1,48 @@
1
+ ## [1.24.1](https://github.com/wmfs/json-to-vuelidate/compare/v1.24.0...v1.24.1) (2022-06-16)
2
+
3
+
4
+ ### 🐛 Bug Fixes
5
+
6
+ * convert instance from Proxy object to regular object for parsing template ([23607c4](https://github.com/wmfs/json-to-vuelidate/commit/23607c4c5447681dc2134f59e829494a47d9b727))
7
+ * ensure all validators are same format using the vuelidate helper function ([8e680e0](https://github.com/wmfs/json-to-vuelidate/commit/8e680e078c670216d8dc15a576863f80027afac7))
8
+ * use latest vuelidate validators for Vue 3 ([c9e86b4](https://github.com/wmfs/json-to-vuelidate/commit/c9e86b40fd2800310cf1ca89c8540261f3be00c6))
9
+
10
+
11
+ ### 🛠 Builds
12
+
13
+ * **deps-dev:** update dependency chai to v4.3.5 ([6fc9656](https://github.com/wmfs/json-to-vuelidate/commit/6fc9656888e1bbb59743b3acea5024fd2ed4011a))
14
+ * **deps-dev:** update dependency chai to v4.3.6 ([cd80d93](https://github.com/wmfs/json-to-vuelidate/commit/cd80d9350c434b13b6bebd6bee792790914e4aad))
15
+ * **deps-dev:** update dependency mocha to v10 ([a2e7b1a](https://github.com/wmfs/json-to-vuelidate/commit/a2e7b1a579ecbecd943e6eaacd558b507530a180))
16
+ * **deps-dev:** update dependency mocha to v9.2.1 ([0439260](https://github.com/wmfs/json-to-vuelidate/commit/04392606932397356737bf8d2e3f6468de9d21c9))
17
+ * **deps-dev:** update dependency mocha to v9.2.2 ([8be430b](https://github.com/wmfs/json-to-vuelidate/commit/8be430b62495df2e140d77247ffbef762de1d661))
18
+ * **deps-dev:** update dependency semantic-release to v19.0.3 ([255ed3b](https://github.com/wmfs/json-to-vuelidate/commit/255ed3b9263e2d39173831f9ee80d8c7288ab3ba))
19
+ * **deps-dev:** update dependency standard to v17 ([d962379](https://github.com/wmfs/json-to-vuelidate/commit/d9623792a31c905aaab5bc3f2a4c3285340dbb50))
20
+
21
+ # [1.24.0](https://github.com/wmfs/json-to-vuelidate/compare/v1.23.0...v1.24.0) (2022-01-25)
22
+
23
+
24
+ ### 🛠 Builds
25
+
26
+ * **deps-dev:** update dependency mocha to v9.1.4 ([656944c](https://github.com/wmfs/json-to-vuelidate/commit/656944c937c202d005de92dce768d377ff945a69))
27
+ * **deps-dev:** update dependency mocha to v9.2.0 ([c2b27c8](https://github.com/wmfs/json-to-vuelidate/commit/c2b27c82651e0cb76427e34d9195edfc0251800a))
28
+ * **deps-dev:** update dependency semantic-release to v19 ([3086076](https://github.com/wmfs/json-to-vuelidate/commit/30860760fd8db420b3a8ddc75b1618b72b089f1b))
29
+ * **deps:** update dependency google-libphonenumber to v3.2.26 ([cfd1c95](https://github.com/wmfs/json-to-vuelidate/commit/cfd1c9581ddd32e847651ce639c925bb18aa7494))
30
+
31
+ # [1.23.0](https://github.com/wmfs/json-to-vuelidate/compare/v1.22.3...v1.23.0) (2021-12-09)
32
+
33
+
34
+ ### 🛠 Builds
35
+
36
+ * **deps-dev:** update dependency semantic-release to v18.0.1 ([4fed5c6](https://github.com/wmfs/json-to-vuelidate/commit/4fed5c6439b66e3de4d339f30954dcc9edb149ac))
37
+ * **deps-dev:** update semantic-release monorepo ([8ca62db](https://github.com/wmfs/json-to-vuelidate/commit/8ca62dbb892ff384edb65ee1936f82e324a851bb))
38
+ * **deps:** update dependency vuelidate to v0.7.7 ([aedd1af](https://github.com/wmfs/json-to-vuelidate/commit/aedd1af0bf754447ec98f35eaf09904cb0576e9d))
39
+
40
+
41
+ ### 🚨 Tests
42
+
43
+ * better labels for comparing min/max dates ([ccc5f43](https://github.com/wmfs/json-to-vuelidate/commit/ccc5f434c4ae9142ade2c7c62db7bf65a8915df9))
44
+ * enhance tests for min/max dates ([72ea867](https://github.com/wmfs/json-to-vuelidate/commit/72ea867adeaf4487927b33ae50fe894c6c030bb7))
45
+
1
46
  ## [1.22.3](https://github.com/wmfs/json-to-vuelidate/compare/v1.22.2...v1.22.3) (2021-10-27)
2
47
 
3
48
 
package/lib/index.js CHANGED
@@ -1,9 +1,11 @@
1
- const validators = require('vuelidate/lib/validators')
1
+ // const validators = require('vuelidate/lib/validators')
2
+ const validators = require('@vuelidate/validators')
2
3
  const template = require('lodash.template')
3
4
  const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance()
4
5
  const moment = require('moment')
5
6
  const isNotDefined = value => value === undefined || value === null || value === ''
6
- const parseTemplate = (_template, data) => template(_template)(data)
7
+ const cloneJson = obj => JSON.parse(JSON.stringify(obj))
8
+ const parseTemplate = (_template, instance) => template(_template)(instance)
7
9
 
8
10
  module.exports = function (json) {
9
11
  const result = {}
@@ -39,46 +41,55 @@ function parse (val, key) {
39
41
  if (attributeValue) result.email = validators.email
40
42
  break
41
43
  case 'regex':
42
- result.regex = value => {
43
- // validators.helpers.regex
44
- if (value !== null && value !== undefined) {
45
- const regex = new RegExp(attributeValue)
46
- return regex.test(value)
47
- } else {
48
- return true
44
+ result.regex = validators.helpers.withParams(
45
+ { type: 'regex' },
46
+ function (value) {
47
+ // validators.helpers.regex
48
+ if (value !== null && value !== undefined) {
49
+ const regex = new RegExp(attributeValue)
50
+ return regex.test(value)
51
+ } else {
52
+ return true
53
+ }
49
54
  }
50
- }
55
+ )
51
56
  break
52
57
  case 'postcode':
53
58
  if (attributeValue) {
54
- result.postcode = value => {
55
- if (value && value.trim().length > 0) {
56
- const v = value.replace(/\s/g, '')
57
- const regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i
58
- // const regex = /^([A-Z]){1}([0-9][0-9]|[0-9]|[A-Z][0-9][A-Z]|[A-Z][0-9][0-9]|[A-Z][0-9]|[0-9][A-Z]){1}([ ])?([0-9][A-z][A-z]){1}$/i
59
- return regex.test(v)
60
- } else {
61
- return true
59
+ result.postcode = validators.helpers.withParams(
60
+ { type: 'postcode' },
61
+ function (value) {
62
+ if (value && value.trim().length > 0) {
63
+ const v = value.replace(/\s/g, '')
64
+ const regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i
65
+ // const regex = /^([A-Z]){1}([0-9][0-9]|[0-9]|[A-Z][0-9][A-Z]|[A-Z][0-9][0-9]|[A-Z][0-9]|[0-9][A-Z]){1}([ ])?([0-9][A-z][A-z]){1}$/i
66
+ return regex.test(v)
67
+ } else {
68
+ return true
69
+ }
62
70
  }
63
- }
71
+ )
64
72
  }
65
73
  break
66
74
  case 'telephoneNumber':
67
75
  if (attributeValue) {
68
- result.telephoneNumber = value => {
69
- // todo: differ between landline and mobile?
70
- if (value && value.trim().length > 0) {
71
- try {
72
- const parsed = phoneUtil.parse(value, 'GB') // todo: define country code in cardscript?
73
- return phoneUtil.isValidNumber(parsed)
74
- } catch (err) {
75
- console.warn(`Failed to parse telephone number on element '${key}'. Error: ${err}`)
76
- return false
76
+ result.telephoneNumber = validators.helpers.withParams(
77
+ { type: 'telephoneNumber' },
78
+ function (value) {
79
+ // todo: differ between landline and mobile?
80
+ if (value && value.trim().length > 0) {
81
+ try {
82
+ const parsed = phoneUtil.parse(value, 'GB') // todo: define country code in cardscript?
83
+ return phoneUtil.isValidNumber(parsed)
84
+ } catch (err) {
85
+ console.warn(`Failed to parse telephone number on element '${key}'. Error: ${err}`)
86
+ return false
87
+ }
88
+ } else {
89
+ return true
77
90
  }
78
- } else {
79
- return true
80
91
  }
81
- }
92
+ )
82
93
  }
83
94
  break
84
95
  case 'required':
@@ -86,7 +97,7 @@ function parse (val, key) {
86
97
  break
87
98
  case 'requiredIf':
88
99
  result.requiredIf = validators.requiredIf(function () {
89
- const result = parseTemplate('${' + attributeValue + '}', this)
100
+ const result = parseTemplate('${' + attributeValue + '}', cloneJson({ data: this.data }))
90
101
  return result === 'true'
91
102
  })
92
103
  break
@@ -97,42 +108,52 @@ function parse (val, key) {
97
108
  result.maxValue = validators.maxValue(attributeValue)
98
109
  break
99
110
  case 'maximum.$':
100
- result.maximum = function (value) {
101
- if (isNotDefined(value)) return true
102
-
103
- const result = parseTemplate('${' + attributeValue + '}', this)
111
+ result.minimumDate = validators.helpers.withParams(
112
+ { type: 'minimumDate' },
113
+ function (value) {
114
+ if (isNotDefined(value)) return true
115
+ const result = parseTemplate('${' + attributeValue + '}', cloneJson({ data: this.data }))
116
+ return value <= +result
117
+ }
118
+ )
104
119
 
105
- return value <= +result
106
- }
107
120
  break
108
121
  case 'minimumDate': {
109
- result.minimumDate = function (value) {
110
- if (isNotDefined(value)) return true
122
+ result.minimumDate = validators.helpers.withParams(
123
+ { type: 'minimumDate' },
124
+ function (value) {
125
+ if (isNotDefined(value)) return true
126
+
127
+ const mValue = moment(value)
128
+ const mMin = moment(attributeValue)
111
129
 
112
- const mValue = moment(value)
113
- const mMin = moment(attributeValue)
130
+ if (!mValue || !mValue.isValid() || !mMin || !mMin.isValid()) {
131
+ return false
132
+ }
114
133
 
115
- if (!mValue || !mValue.isValid() || !mMin || !mMin.isValid()) {
116
- return false
134
+ return mMin.isBefore(mValue, 'day') || mMin.isSame(mValue, 'day')
117
135
  }
136
+ )
118
137
 
119
- return mMin.isBefore(mValue, 'day') || mMin.isSame(mValue, 'day')
120
- }
121
138
  break
122
139
  }
123
140
  case 'maximumDate': {
124
- result.maximumDate = function (value) {
125
- if (isNotDefined(value)) return true
141
+ result.maximumDate = validators.helpers.withParams(
142
+ { type: 'maximumDate' },
143
+ function (value) {
144
+ if (isNotDefined(value)) return true
126
145
 
127
- const mValue = moment(value)
128
- const mMax = moment(attributeValue)
146
+ const mValue = moment(value)
147
+ const mMax = moment(attributeValue)
129
148
 
130
- if (!mValue || !mValue.isValid() || !mMax || !mMax.isValid()) {
131
- return false
149
+ if (!mValue || !mValue.isValid() || !mMax || !mMax.isValid()) {
150
+ return false
151
+ }
152
+
153
+ return mValue.isBefore(mMax, 'day') || mValue.isSame(mMax, 'day')
132
154
  }
155
+ )
133
156
 
134
- return mValue.isBefore(mMax, 'day') || mValue.isSame(mMax, 'day')
135
- }
136
157
  break
137
158
  }
138
159
  case 'minLength':
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wmfs/json-to-vuelidate",
3
3
  "description": "Takes in a JSON object and products an object of vuelidate expressions.",
4
- "version": "1.22.3",
4
+ "version": "1.24.1",
5
5
  "author": "West Midlands Fire Service",
6
6
  "keywords": [
7
7
  "tymly",
@@ -9,24 +9,25 @@
9
9
  ],
10
10
  "main": "./lib/index.js",
11
11
  "dependencies": {
12
- "vuelidate": "0.7.6",
12
+ "@vue/composition-api": "1.6.2",
13
+ "@vuelidate/validators": "2.0.0-alpha.28",
14
+ "google-libphonenumber": "3.2.26",
13
15
  "lodash.template": "4.5.0",
14
- "google-libphonenumber": "3.2.21",
15
16
  "moment": "2.29.1"
16
17
  },
17
18
  "devDependencies": {
18
- "babel-register": "6.26.0",
19
+ "@semantic-release/changelog": "6.0.1",
20
+ "@semantic-release/git": "10.0.1",
19
21
  "babel-preset-env": "1.7.0",
20
- "chai": "4.3.4",
22
+ "babel-register": "6.26.0",
23
+ "chai": "4.3.6",
21
24
  "codecov": "3.8.3",
22
25
  "conventional-changelog-metahub": "4.0.1",
23
26
  "cz-conventional-changelog": "3.3.0",
24
- "mocha": "9.1.3",
27
+ "mocha": "10.0.0",
25
28
  "nyc": "15.1.0",
26
- "semantic-release": "18.0.0",
27
- "standard": "16.0.4",
28
- "@semantic-release/changelog": "6.0.0",
29
- "@semantic-release/git": "10.0.0"
29
+ "semantic-release": "19.0.3",
30
+ "standard": "17.0.0"
30
31
  },
31
32
  "scripts": {
32
33
  "lint": "standard",
package/test/test.js CHANGED
@@ -4,7 +4,7 @@ const converter = require('./../lib/')
4
4
  const expect = require('chai').expect
5
5
  const moment = require('moment')
6
6
 
7
- const JSON = {
7
+ const input = {
8
8
  numberBetween5And10: {
9
9
  between: [5, 10]
10
10
  },
@@ -43,6 +43,9 @@ const JSON = {
43
43
  },
44
44
  min3DaysAgo: {
45
45
  minimumDate: moment().subtract(3, 'days').format('YYYY/MM/DD')
46
+ },
47
+ telNum: {
48
+ telephoneNumber: true
46
49
  }
47
50
  }
48
51
 
@@ -50,67 +53,92 @@ describe('Test the vuelidate converter', function () {
50
53
  let vuelidate
51
54
 
52
55
  it('Convert some JSON', () => {
53
- vuelidate = converter(JSON)
54
-
55
- console.log('\n------\n', vuelidate)
56
- expect(vuelidate.numberBetween5And10.between).to.be.a('function')
57
-
58
- expect(vuelidate.numberMin10Max50.minValue).to.be.a('function')
59
- expect(vuelidate.numberMin10Max50.maxValue).to.be.a('function')
60
-
61
- expect(vuelidate.textMinLen5MaxLen50.minLength).to.be.a('function')
62
- expect(vuelidate.textMinLen5MaxLen50.maxLength).to.be.a('function')
56
+ vuelidate = converter(input)
63
57
 
64
- expect(vuelidate.textRequired.required).to.be.a('function')
58
+ console.log('------')
59
+ console.log(JSON.stringify(vuelidate, null, 2))
60
+ console.log('------')
65
61
 
66
- expect(vuelidate.textRequiredIf.requiredIf).to.be.a('function')
62
+ expect(vuelidate.numberBetween5And10.between.$validator).to.be.a('function')
67
63
 
68
- expect(vuelidate.textEmail.email).to.be.a('function')
69
- expect(vuelidate.inputEmail.email).to.be.a('function')
64
+ expect(vuelidate.numberMin10Max50.minValue.$validator).to.be.a('function')
65
+ expect(vuelidate.numberMin10Max50.maxValue.$validator).to.be.a('function')
70
66
 
71
- expect(vuelidate.list.required).to.be.a('function')
72
- expect(vuelidate.apiLookup.params.apiLookupList.required).to.be.a('function')
67
+ expect(vuelidate.textMinLen5MaxLen50.minLength.$validator).to.be.a('function')
68
+ expect(vuelidate.textMinLen5MaxLen50.maxLength.$validator).to.be.a('function')
73
69
 
74
- expect(vuelidate.dateOfBirth.maximumDate).to.be.a('function')
70
+ expect(vuelidate.textRequired.required.$validator).to.be.a('function')
75
71
 
76
- expect(vuelidate.min3DaysAgo.minimumDate).to.be.a('function')
77
- })
78
-
79
- it('test the validation function for minimum date', () => {
80
- const fn = vuelidate.min3DaysAgo.minimumDate
72
+ expect(vuelidate.textRequiredIf.requiredIf.$validator).to.be.a('function')
81
73
 
82
- const oneWeekAgo = moment().subtract(7, 'day')
83
- const today = moment()
84
- const tomorrow = moment().add(1, 'day')
85
- const threeDaysAgo = moment().subtract(3, 'days')
74
+ expect(vuelidate.textEmail.email.$validator).to.be.a('function')
75
+ expect(vuelidate.inputEmail.email.$validator).to.be.a('function')
86
76
 
87
- const oneWeekAgoResult = fn(oneWeekAgo)
88
- expect(oneWeekAgoResult).to.eql(false)
77
+ expect(vuelidate.list.required.$validator).to.be.a('function')
78
+ expect(vuelidate.apiLookup.params.apiLookupList.required.$validator).to.be.a('function')
89
79
 
90
- const threeDaysAgoResult = fn(threeDaysAgo)
91
- expect(threeDaysAgoResult).to.eql(true)
80
+ expect(vuelidate.dateOfBirth.maximumDate.$validator).to.be.a('function')
81
+ expect(vuelidate.min3DaysAgo.minimumDate.$validator).to.be.a('function')
92
82
 
93
- const todayResult = fn(today)
94
- expect(todayResult).to.eql(true)
95
-
96
- const tomorrowResult = fn(tomorrow)
97
- expect(tomorrowResult).to.eql(true)
83
+ expect(vuelidate.telNum.telephoneNumber.$validator).to.be.a('function')
98
84
  })
99
85
 
100
- it('test the validation function for maximum date', () => {
101
- const fn = vuelidate.dateOfBirth.maximumDate
102
-
103
- const yesterday = moment().subtract(1, 'day')
104
- const today = moment()
105
- const tomorrow = moment().add(1, 'day')
86
+ const dates = [
87
+ ['1 year ago', moment().subtract(1, 'year'), false, true],
88
+ ['7 days ago', moment().subtract(7, 'day'), false, true],
89
+ ['4 days ago', moment().subtract(4, 'day'), false, true],
90
+ ['3 days ago', moment().subtract(3, 'day'), true, true],
91
+ ['yesterday', moment().subtract(1, 'day'), true, true],
92
+ ['today', moment(), true, true],
93
+ ['tomorrow', moment().add(1, 'day'), true, false],
94
+ ['3 days ahead', moment().add(3, 'day'), true, false],
95
+ ['4 days ahead', moment().add(4, 'day'), true, false],
96
+ ['7 days ahead', moment().add(7, 'day'), true, false],
97
+ ['1 year ahead', moment().add(1, 'year'), true, false]
98
+ ]
99
+
100
+ describe('Test the validation function for minimum date', () => {
101
+ for (const [label, date, minResult] of dates) {
102
+ it(`${label}, should${minResult ? '' : ' not'} be after 3 days ago`, () => expect(vuelidate.min3DaysAgo.minimumDate.$validator(date)).to.eql(minResult))
103
+ }
104
+ })
106
105
 
107
- const yesterdayResult = fn(yesterday)
108
- expect(yesterdayResult).to.eql(true)
106
+ describe('Test the validation function for maximum date', () => {
107
+ for (const [label, date, , maxResult] of dates) {
108
+ it(`${label}, should${maxResult ? '' : ' not'} be before today`, () => expect(vuelidate.dateOfBirth.maximumDate.$validator(date)).to.eql(maxResult))
109
+ }
110
+ })
109
111
 
110
- const todayResult = fn(today)
111
- expect(todayResult).to.eql(true)
112
+ describe('Test the validation function for between', () => {
113
+ for (let i = 0; i < 30; i++) {
114
+ it(`Is ${i} between 5 and 10?`, () => {
115
+ const expected = i >= 5 && i <= 10
116
+
117
+ expect(
118
+ vuelidate.numberBetween5And10.between.$validator(i)
119
+ ).to.eql(
120
+ expected
121
+ )
122
+ })
123
+ }
124
+ })
112
125
 
113
- const tomorrowResult = fn(tomorrow)
114
- expect(tomorrowResult).to.eql(false)
126
+ describe('Test the validation function for telephone number', () => {
127
+ const telephoneNumbers = [
128
+ ['07123456789', true],
129
+ ['01211234567', true],
130
+ ['0121133', false],
131
+ ['fjahsf', false]
132
+ ]
133
+
134
+ for (const [telephoneNumber, expected] of telephoneNumbers) {
135
+ it(`Is ${telephoneNumber} a telephone number?`, () => {
136
+ expect(
137
+ vuelidate.telNum.telephoneNumber.$validator(telephoneNumber)
138
+ ).to.eql(
139
+ expected
140
+ )
141
+ })
142
+ }
115
143
  })
116
144
  })