Skip to content

Commit ddb84f7

Browse files
committed
feat: 🎉 added new first by method validator class
1 parent 795a58b commit ddb84f7

File tree

5 files changed

+330
-1822
lines changed

5 files changed

+330
-1822
lines changed

nuxt/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { resolve, join } from 'path'
2-
import merge from 'lodash.merge'
32

43
module.exports = function nuxtVueApiQueriesModule(moduleOptions = {}) {
54
const { apiQueries = {} } = this.options
6-
const options = merge({}, moduleOptions, apiQueries)
5+
const options = Object.assign({}, moduleOptions, apiQueries)
76
this.addPlugin({
87
src: resolve(__dirname, './templates/plugin.js'),
98
fileName: join('vue-api-queries.js'),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build": "yarn clean && tsc",
1414
"watch": "tsc -w",
1515
"start": "yarn link && nodemon",
16-
"semantic-release": "semantic-release",
16+
"release": "standard-version && git push --follow-tags origin main && yarn publish",
1717
"prepublish": "yarn test && yarn build",
1818
"clean": "rimraf dist",
1919
"prepare": "husky install"
@@ -65,7 +65,7 @@
6565
"nuxt": "^2.15.8",
6666
"prettier": "^2.4.1",
6767
"rimraf": "^3.0.2",
68-
"semantic-release": "^18.0.0",
68+
"standard-version": "^9.3.1",
6969
"ts-jest": "^27.0.5",
7070
"typescript": "^4.4.3"
7171
},

src/__tests__/validator.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,28 @@ describe('Validator', () => {
189189

190190
expect(validator.any(['first_name', 'last_name'], false)).toBeTruthy()
191191
})
192+
193+
it('get first by field', () => {
194+
const errors = {
195+
first_name: ['This first name field is required'],
196+
last_name: ['This last name field is required'],
197+
age: ['This age field is required'],
198+
}
199+
200+
expect(validator.firstBy(errors, 'age')).toEqual(
201+
'This age field is required',
202+
)
203+
})
204+
205+
it('get first by without any field', () => {
206+
const errors = {
207+
first_name: ['This fist name field is required'],
208+
last_name: ['This last name field is required'],
209+
age: ['This age field is required'],
210+
}
211+
212+
expect(validator.firstBy(errors)).toEqual(
213+
'This fist name field is required',
214+
)
215+
})
192216
})

src/core/Validator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ class Validator {
4646
return this.get(field)[0]
4747
}
4848

49+
firstBy(obj: Record<string, any>, field = '') {
50+
let value
51+
if (!field) {
52+
value = obj[Object.keys(obj)[0]]
53+
} else {
54+
value = obj[field]
55+
}
56+
if (isArray(value)) {
57+
return value[0]
58+
}
59+
return value
60+
}
61+
4962
missed(field?: string | string[]): boolean {
5063
return !this.has(field)
5164
}

0 commit comments

Comments
 (0)