@wmfs/json-to-vuelidate 1.23.0 → 1.25.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 +37 -0
- package/lib/index.js +75 -54
- package/package.json +11 -10
- package/test/test.js +58 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
# [1.25.0](https://github.com/wmfs/json-to-vuelidate/compare/v1.24.1...v1.25.0) (2022-06-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### 🛠 Builds
|
|
5
|
+
|
|
6
|
+
* **deps:** change [@vue](https://github.com/vue)/composition-api to vue ([65984c7](https://github.com/wmfs/json-to-vuelidate/commit/65984c7b05d176f6702a4da55700de46a019688e))
|
|
7
|
+
|
|
8
|
+
## [1.24.1](https://github.com/wmfs/json-to-vuelidate/compare/v1.24.0...v1.24.1) (2022-06-16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### 🐛 Bug Fixes
|
|
12
|
+
|
|
13
|
+
* convert instance from Proxy object to regular object for parsing template ([23607c4](https://github.com/wmfs/json-to-vuelidate/commit/23607c4c5447681dc2134f59e829494a47d9b727))
|
|
14
|
+
* ensure all validators are same format using the vuelidate helper function ([8e680e0](https://github.com/wmfs/json-to-vuelidate/commit/8e680e078c670216d8dc15a576863f80027afac7))
|
|
15
|
+
* use latest vuelidate validators for Vue 3 ([c9e86b4](https://github.com/wmfs/json-to-vuelidate/commit/c9e86b40fd2800310cf1ca89c8540261f3be00c6))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### 🛠 Builds
|
|
19
|
+
|
|
20
|
+
* **deps-dev:** update dependency chai to v4.3.5 ([6fc9656](https://github.com/wmfs/json-to-vuelidate/commit/6fc9656888e1bbb59743b3acea5024fd2ed4011a))
|
|
21
|
+
* **deps-dev:** update dependency chai to v4.3.6 ([cd80d93](https://github.com/wmfs/json-to-vuelidate/commit/cd80d9350c434b13b6bebd6bee792790914e4aad))
|
|
22
|
+
* **deps-dev:** update dependency mocha to v10 ([a2e7b1a](https://github.com/wmfs/json-to-vuelidate/commit/a2e7b1a579ecbecd943e6eaacd558b507530a180))
|
|
23
|
+
* **deps-dev:** update dependency mocha to v9.2.1 ([0439260](https://github.com/wmfs/json-to-vuelidate/commit/04392606932397356737bf8d2e3f6468de9d21c9))
|
|
24
|
+
* **deps-dev:** update dependency mocha to v9.2.2 ([8be430b](https://github.com/wmfs/json-to-vuelidate/commit/8be430b62495df2e140d77247ffbef762de1d661))
|
|
25
|
+
* **deps-dev:** update dependency semantic-release to v19.0.3 ([255ed3b](https://github.com/wmfs/json-to-vuelidate/commit/255ed3b9263e2d39173831f9ee80d8c7288ab3ba))
|
|
26
|
+
* **deps-dev:** update dependency standard to v17 ([d962379](https://github.com/wmfs/json-to-vuelidate/commit/d9623792a31c905aaab5bc3f2a4c3285340dbb50))
|
|
27
|
+
|
|
28
|
+
# [1.24.0](https://github.com/wmfs/json-to-vuelidate/compare/v1.23.0...v1.24.0) (2022-01-25)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### 🛠 Builds
|
|
32
|
+
|
|
33
|
+
* **deps-dev:** update dependency mocha to v9.1.4 ([656944c](https://github.com/wmfs/json-to-vuelidate/commit/656944c937c202d005de92dce768d377ff945a69))
|
|
34
|
+
* **deps-dev:** update dependency mocha to v9.2.0 ([c2b27c8](https://github.com/wmfs/json-to-vuelidate/commit/c2b27c82651e0cb76427e34d9195edfc0251800a))
|
|
35
|
+
* **deps-dev:** update dependency semantic-release to v19 ([3086076](https://github.com/wmfs/json-to-vuelidate/commit/30860760fd8db420b3a8ddc75b1618b72b089f1b))
|
|
36
|
+
* **deps:** update dependency google-libphonenumber to v3.2.26 ([cfd1c95](https://github.com/wmfs/json-to-vuelidate/commit/cfd1c9581ddd32e847651ce639c925bb18aa7494))
|
|
37
|
+
|
|
1
38
|
# [1.23.0](https://github.com/wmfs/json-to-vuelidate/compare/v1.22.3...v1.23.0) (2021-12-09)
|
|
2
39
|
|
|
3
40
|
|
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
|
|
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 =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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 =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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 =
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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 =
|
|
110
|
-
|
|
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
|
-
|
|
113
|
-
|
|
130
|
+
if (!mValue || !mValue.isValid() || !mMin || !mMin.isValid()) {
|
|
131
|
+
return false
|
|
132
|
+
}
|
|
114
133
|
|
|
115
|
-
|
|
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 =
|
|
125
|
-
|
|
141
|
+
result.maximumDate = validators.helpers.withParams(
|
|
142
|
+
{ type: 'maximumDate' },
|
|
143
|
+
function (value) {
|
|
144
|
+
if (isNotDefined(value)) return true
|
|
126
145
|
|
|
127
|
-
|
|
128
|
-
|
|
146
|
+
const mValue = moment(value)
|
|
147
|
+
const mMax = moment(attributeValue)
|
|
129
148
|
|
|
130
|
-
|
|
131
|
-
|
|
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.
|
|
4
|
+
"version": "1.25.0",
|
|
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
|
-
"
|
|
12
|
+
"vue": "3.2.33",
|
|
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
|
-
"
|
|
19
|
+
"@semantic-release/changelog": "6.0.1",
|
|
20
|
+
"@semantic-release/git": "10.0.1",
|
|
19
21
|
"babel-preset-env": "1.7.0",
|
|
20
|
-
"
|
|
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": "
|
|
27
|
+
"mocha": "10.0.0",
|
|
25
28
|
"nyc": "15.1.0",
|
|
26
|
-
"semantic-release": "
|
|
27
|
-
"standard": "
|
|
28
|
-
"@semantic-release/changelog": "6.0.1",
|
|
29
|
-
"@semantic-release/git": "10.0.1"
|
|
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
|
|
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,30 +53,34 @@ describe('Test the vuelidate converter', function () {
|
|
|
50
53
|
let vuelidate
|
|
51
54
|
|
|
52
55
|
it('Convert some JSON', () => {
|
|
53
|
-
vuelidate = converter(
|
|
56
|
+
vuelidate = converter(input)
|
|
57
|
+
|
|
58
|
+
console.log('------')
|
|
59
|
+
console.log(JSON.stringify(vuelidate, null, 2))
|
|
60
|
+
console.log('------')
|
|
54
61
|
|
|
55
|
-
|
|
56
|
-
expect(vuelidate.numberBetween5And10.between).to.be.a('function')
|
|
62
|
+
expect(vuelidate.numberBetween5And10.between.$validator).to.be.a('function')
|
|
57
63
|
|
|
58
|
-
expect(vuelidate.numberMin10Max50.minValue).to.be.a('function')
|
|
59
|
-
expect(vuelidate.numberMin10Max50.maxValue).to.be.a('function')
|
|
64
|
+
expect(vuelidate.numberMin10Max50.minValue.$validator).to.be.a('function')
|
|
65
|
+
expect(vuelidate.numberMin10Max50.maxValue.$validator).to.be.a('function')
|
|
60
66
|
|
|
61
|
-
expect(vuelidate.textMinLen5MaxLen50.minLength).to.be.a('function')
|
|
62
|
-
expect(vuelidate.textMinLen5MaxLen50.maxLength).to.be.a('function')
|
|
67
|
+
expect(vuelidate.textMinLen5MaxLen50.minLength.$validator).to.be.a('function')
|
|
68
|
+
expect(vuelidate.textMinLen5MaxLen50.maxLength.$validator).to.be.a('function')
|
|
63
69
|
|
|
64
|
-
expect(vuelidate.textRequired.required).to.be.a('function')
|
|
70
|
+
expect(vuelidate.textRequired.required.$validator).to.be.a('function')
|
|
65
71
|
|
|
66
|
-
expect(vuelidate.textRequiredIf.requiredIf).to.be.a('function')
|
|
72
|
+
expect(vuelidate.textRequiredIf.requiredIf.$validator).to.be.a('function')
|
|
67
73
|
|
|
68
|
-
expect(vuelidate.textEmail.email).to.be.a('function')
|
|
69
|
-
expect(vuelidate.inputEmail.email).to.be.a('function')
|
|
74
|
+
expect(vuelidate.textEmail.email.$validator).to.be.a('function')
|
|
75
|
+
expect(vuelidate.inputEmail.email.$validator).to.be.a('function')
|
|
70
76
|
|
|
71
|
-
expect(vuelidate.list.required).to.be.a('function')
|
|
72
|
-
expect(vuelidate.apiLookup.params.apiLookupList.required).to.be.a('function')
|
|
77
|
+
expect(vuelidate.list.required.$validator).to.be.a('function')
|
|
78
|
+
expect(vuelidate.apiLookup.params.apiLookupList.required.$validator).to.be.a('function')
|
|
73
79
|
|
|
74
|
-
expect(vuelidate.dateOfBirth.maximumDate).to.be.a('function')
|
|
80
|
+
expect(vuelidate.dateOfBirth.maximumDate.$validator).to.be.a('function')
|
|
81
|
+
expect(vuelidate.min3DaysAgo.minimumDate.$validator).to.be.a('function')
|
|
75
82
|
|
|
76
|
-
expect(vuelidate.
|
|
83
|
+
expect(vuelidate.telNum.telephoneNumber.$validator).to.be.a('function')
|
|
77
84
|
})
|
|
78
85
|
|
|
79
86
|
const dates = [
|
|
@@ -92,13 +99,46 @@ describe('Test the vuelidate converter', function () {
|
|
|
92
99
|
|
|
93
100
|
describe('Test the validation function for minimum date', () => {
|
|
94
101
|
for (const [label, date, minResult] of dates) {
|
|
95
|
-
it(`${label}, should${minResult ? '' : ' not'} be after 3 days ago`, () => expect(vuelidate.min3DaysAgo.minimumDate(date)).to.eql(minResult))
|
|
102
|
+
it(`${label}, should${minResult ? '' : ' not'} be after 3 days ago`, () => expect(vuelidate.min3DaysAgo.minimumDate.$validator(date)).to.eql(minResult))
|
|
96
103
|
}
|
|
97
104
|
})
|
|
98
105
|
|
|
99
106
|
describe('Test the validation function for maximum date', () => {
|
|
100
107
|
for (const [label, date, , maxResult] of dates) {
|
|
101
|
-
it(`${label}, should${maxResult ? '' : ' not'} be before today`, () => expect(vuelidate.dateOfBirth.maximumDate(date)).to.eql(maxResult))
|
|
108
|
+
it(`${label}, should${maxResult ? '' : ' not'} be before today`, () => expect(vuelidate.dateOfBirth.maximumDate.$validator(date)).to.eql(maxResult))
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
|
|
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
|
+
})
|
|
125
|
+
|
|
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
|
+
})
|
|
102
142
|
}
|
|
103
143
|
})
|
|
104
144
|
})
|