Skip to content

Commit 84baf63

Browse files
committed
test: ✅ make test run the same as before
1 parent dd04523 commit 84baf63

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

src/__tests__/validator.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ describe('Validator', () => {
8888
})
8989
test('Clear all errors by flush', () => {
9090
const errors = {
91-
name: ['The name field is required.'],
91+
nameKh: ['The name field is required.'],
92+
name_kh: ['The name field is required.'],
9293
email: ['The email field is required.'],
9394
}
9495
validator.fill(errors)

src/core/Validator.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { cloneDeep, get, has, omit, isArray } from 'lodash'
2-
import { is } from '../util'
1+
import { cloneDeep, get, has, isArray, omit } from 'lodash'
2+
import { is, toCamelCase, toSnakeCase } from '../util'
33

44
class Validator {
55
public errors: Record<string, any>
@@ -12,42 +12,36 @@ class Validator {
1212
this.errors = errors
1313
}
1414

15-
add(attribute: string, message: string, forceUpdate?: boolean) {
16-
if (this.missed(attribute)) {
17-
this.errors[attribute] = []
15+
add(field: string, message: string, forceUpdate?: boolean) {
16+
if (this.missed(field)) {
17+
this.errors[field] = []
1818
}
19-
if (!this.errors[attribute].includes(message)) {
20-
this.errors[attribute].unshift(message)
19+
if (!this.errors[field].includes(message)) {
20+
this.errors[field].unshift(message)
2121
}
2222
if (forceUpdate) {
23-
this.errors[attribute] = []
24-
this.errors[attribute].push(message)
23+
this.errors[field] = []
24+
this.errors[field].push(message)
2525
}
2626
}
2727

2828
has(field: string | string[]) {
29-
if (isArray(field)) {
30-
return is(Object.keys(this.errors), field)
31-
}
32-
let hasError = has(this.errors, field)
33-
if (!hasError) {
34-
const errors = Object.keys(this.errors).filter(
35-
(e: string) => e.startsWith(`${field}.`) || e.startsWith(`${field}[`),
36-
)
37-
hasError = errors.length > 0
38-
}
39-
return hasError
29+
const fields = this.fields(field)
30+
return is(Object.keys(this.errors), fields)
4031
}
4132

42-
first(field: string | string[]): string | object {
33+
first(field: string | string[]): string | Record<string, any> | undefined {
34+
const fields = this.fields(field)
4335
if (Array.isArray(field)) {
44-
for (const f of field) {
45-
if (has(this.errors, f)) return this.first(f)
36+
for (const f of fields) {
37+
if (!has(this.errors, f)) continue
38+
return this.first(f)
4639
}
40+
} else {
41+
const value = this.get(field)
42+
if (Array.isArray(value)) return value[0]
43+
return value
4744
}
48-
const value = this.get(field)
49-
if (Array.isArray(value)) return value[0]
50-
return value
5145
}
5246

5347
firstBy(obj: Record<string, any>, field?: string) {
@@ -86,7 +80,7 @@ class Validator {
8680
return Object.keys(errors).length > 0
8781
}
8882

89-
get(field: string | string[]): string | string[] {
83+
get(field: string): string | string[] {
9084
return get(this.errors, field, [])
9185
}
9286

@@ -106,9 +100,9 @@ class Validator {
106100
this.fill({})
107101
}
108102

109-
clear(attribute?: string | string[]) {
110-
if (!attribute) return this.flush()
111-
const errors = omit(cloneDeep(this.errors), attribute)
103+
clear(field?: string | string[]) {
104+
if (!field) return this.flush()
105+
const errors = omit(cloneDeep(this.errors), field)
112106
this.fill(errors)
113107
}
114108

@@ -122,6 +116,18 @@ class Validator {
122116
const names = prefix ? [name, `${prefix}.${name}`] : [name]
123117
this.clear(names)
124118
}
119+
120+
fields(field: string | string[]) {
121+
const fields = []
122+
if (Array.isArray(field)) {
123+
for (const f of field) {
124+
fields.push(toCamelCase(f), toSnakeCase(f))
125+
}
126+
} else {
127+
fields.push(toCamelCase(field), toSnakeCase(field))
128+
}
129+
return [...new Set(fields)].filter(Boolean)
130+
}
125131
}
126132

127133
export type { Validator as ValidatorType }

src/util/string.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
export function removeDoubleSlash(url: string) {
22
return url.replace(/\/\//g, '/')
33
}
4+
export const toCamelCase = (e: string) => {
5+
return e.replace(/_([a-z])/g, (g) => g[1].toUpperCase())
6+
}
7+
export const toSnakeCase = (e: string) => {
8+
if (!e.match(/([A-Z])/g)) return e
9+
return e.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
10+
}

0 commit comments

Comments
 (0)