Skip to content

Commit b22aae0

Browse files
committed
feat: ✨ get multiple field by camel or snake case
1 parent 55e5be7 commit b22aae0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/__tests__/validator.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,12 @@ describe('Validator', () => {
226226

227227
expect(validator.first(['name[0].kh'])).toEqual('This fist name field is required')
228228
})
229+
230+
it('should reflect the field name by camel or snake case', () => {
231+
const errors = { firstName: ['This fist name field is required'] }
232+
validator.fill(errors)
233+
234+
expect(validator.has(['first_name'])).toBeTruthy()
235+
expect(validator.first(['first_name'])).toEqual('This fist name field is required')
236+
})
229237
})

src/core/Validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class Validator {
3131
}
3232

3333
first(field: string | string[]): string | Record<string, any> | undefined {
34-
const fields = this.fields(field)
3534
if (Array.isArray(field)) {
35+
const fields = this.fields(field)
3636
for (const f of fields) {
3737
if (!has(this.errors, f)) continue
3838
return this.first(f)
@@ -117,7 +117,7 @@ class Validator {
117117
this.clear(names)
118118
}
119119

120-
fields(field: string | string[]) {
120+
fields(field: string | string[]): string[] {
121121
const fields = []
122122
if (Array.isArray(field)) {
123123
for (const f of field) {

0 commit comments

Comments
 (0)