Skip to content

Commit ed0dcb2

Browse files
authored
Merge pull request #413 from chantouchsek/feat/access-deep-array-nest
feat: add deep array access for first()
2 parents da4706f + 921ca27 commit ed0dcb2

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
"@commitlint/config-conventional": "^17.0.0",
5353
"@nuxt/types": "^2.15.8",
5454
"@types/jest": "^27.4.0",
55+
"@types/lodash.get": "^4.4.7",
56+
"@types/lodash.has": "^4.5.7",
5557
"@types/node": "^18.0.0",
5658
"@types/qs": "^6.9.7",
5759
"@typescript-eslint/eslint-plugin": "^5.9.0",
@@ -81,6 +83,8 @@
8183
],
8284
"dependencies": {
8385
"axios": "^0.27.2",
86+
"lodash.get": "^4.4.2",
87+
"lodash.has": "^4.5.2",
8488
"qs": "^6.10.2"
8589
},
8690
"lint-staged": {

src/__tests__/validator.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('Validator', () => {
5858
test('Get all errors by keys', () => {
5959
validator.add('email', 'The email field is required.')
6060
validator.add('form.email', 'The form.name field is required.')
61-
expect(validator.first(['name', 'form.email']).length).toBeGreaterThan(0)
61+
expect(validator.first(['name', 'form.email'])).toBeDefined()
6262
})
6363
test('Get all errors message', () => {
6464
validator.add('email', 'The email field is required.')
@@ -213,4 +213,20 @@ describe('Validator', () => {
213213
'This fist name field is required',
214214
)
215215
})
216+
217+
it('get first array by nested array', () => {
218+
const errors = { name: [{ kh: ['This fist name field is required'] }] }
219+
validator.fill(errors)
220+
221+
expect(validator.first(['name[0]'])).toEqual(errors.name[0])
222+
})
223+
224+
it('get first by nested array', () => {
225+
const errors = { name: [{ kh: ['This fist name field is required'] }] }
226+
validator.fill(errors)
227+
228+
expect(validator.first(['name[0].kh'])).toEqual(
229+
'This fist name field is required',
230+
)
231+
})
216232
})

src/core/Validator.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { hasOwnProperty, is, isArray } from '../util'
2+
import get from 'lodash.get'
3+
import has from 'lodash.has'
24

35
class Validator {
46
public errors: Record<string, any>
@@ -20,11 +22,11 @@ class Validator {
2022
}
2123
}
2224

23-
has(field: any | any[]) {
25+
has(field: string | string[]) {
2426
if (isArray(field)) {
2527
return is(Object.keys(this.errors), field)
2628
}
27-
let hasError = hasOwnProperty(this.errors, field)
29+
let hasError = has(this.errors, field)
2830
if (!hasError) {
2931
const errors = Object.keys(this.errors).filter(
3032
(e: string) => e.startsWith(`${field}.`) || e.startsWith(`${field}[`),
@@ -34,16 +36,18 @@ class Validator {
3436
return hasError
3537
}
3638

37-
first(field: any | any[]): string {
38-
if (field instanceof Array) {
39+
first(field: string | string[]): string | object {
40+
if (Array.isArray(field)) {
3941
for (let i = 0; i < field.length; i++) {
40-
if (!hasOwnProperty(this.errors, field[i])) {
42+
if (!has(this.errors, field[i])) {
4143
continue
4244
}
4345
return this.first(field[i])
4446
}
4547
}
46-
return this.get(field)[0]
48+
const value = this.get(field as string)
49+
if (Array.isArray(value)) return value[0]
50+
return value // return it if object like
4751
}
4852

4953
firstBy(obj: Record<string, any>, field?: string): string {
@@ -53,17 +57,15 @@ class Validator {
5357
} else {
5458
value = obj[field]
5559
}
56-
if (isArray(value)) {
57-
value = value[0]
58-
}
60+
if (isArray(value)) value = value[0]
5961
return value
6062
}
6163

62-
missed(field?: string | string[]): boolean {
64+
missed(field: string | string[]): boolean {
6365
return !this.has(field)
6466
}
6567

66-
nullState(field?: string | string[]): boolean | null {
68+
nullState(field: string | string[]): boolean | null {
6769
return this.has(field) ? this.missed(field) : null
6870
}
6971

@@ -85,7 +87,7 @@ class Validator {
8587
}
8688

8789
get(field: string): string | string[] {
88-
return this.errors[field] || []
90+
return get(this.errors, field) || []
8991
}
9092

9193
all() {

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"@types/jest",
2424
"axios",
2525
"axios-mock-adapter",
26-
"@types/qs"
26+
"@types/qs",
27+
"@types/lodash.get",
28+
"@types/lodash.has"
2729
]
2830
},
2931
"include": ["src"],

yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,25 @@
24052405
resolved "https://registry.yarnpkg.com/@types/less/-/less-3.0.2.tgz#2761d477678c8374cb9897666871662eb1d1115e"
24062406
integrity sha512-62vfe65cMSzYaWmpmhqCMMNl0khen89w57mByPi1OseGfcV/LV03fO8YVrNj7rFQsRWNJo650WWyh6m7p8vZmA==
24072407

2408+
"@types/lodash.get@^4.4.7":
2409+
version "4.4.7"
2410+
resolved "https://registry.yarnpkg.com/@types/lodash.get/-/lodash.get-4.4.7.tgz#1ea63d8b94709f6bc9e231f252b31440abe312cf"
2411+
integrity sha512-af34Mj+KdDeuzsJBxc/XeTtOx0SZHZNLd+hdrn+PcKGQs0EG2TJTzQAOTCZTgDJCArahlCzLWSy8c2w59JRz7Q==
2412+
dependencies:
2413+
"@types/lodash" "*"
2414+
2415+
"@types/lodash.has@^4.5.7":
2416+
version "4.5.7"
2417+
resolved "https://registry.yarnpkg.com/@types/lodash.has/-/lodash.has-4.5.7.tgz#9e1c3da3ee67f68fadc3d168ffdf8e11a3f3ccd3"
2418+
integrity sha512-nfbAzRbsZBdzSAkL9iiLy4SQk89uuFcXBFwZ7pf6oZhBgPvNys8BY5Twp/w8XvZKGt1o6cAa85wX4QhqO3uQ7A==
2419+
dependencies:
2420+
"@types/lodash" "*"
2421+
2422+
"@types/lodash@*":
2423+
version "4.14.182"
2424+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
2425+
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==
2426+
24082427
"@types/mime@^1":
24092428
version "1.3.2"
24102429
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
@@ -7975,6 +7994,16 @@ lodash.debounce@^4.0.8:
79757994
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
79767995
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
79777996

7997+
lodash.get@^4.4.2:
7998+
version "4.4.2"
7999+
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
8000+
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==
8001+
8002+
lodash.has@^4.5.2:
8003+
version "4.5.2"
8004+
resolved "https://registry.yarnpkg.com/lodash.has/-/lodash.has-4.5.2.tgz#d19f4dc1095058cccbe2b0cdf4ee0fe4aa37c862"
8005+
integrity sha512-rnYUdIo6xRCJnQmbVFEwcxF144erlD+M3YcJUVesflU9paQaE8p+fJDcIQrlMYbxoANFL+AB9hZrzSBBk5PL+g==
8006+
79788007
lodash.ismatch@^4.4.0:
79798008
version "4.4.0"
79808009
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"

0 commit comments

Comments
 (0)