Skip to content

Commit e3576c4

Browse files
committed
feat: ✨ clear the field and get any
1 parent b22aae0 commit e3576c4

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/__tests__/validator.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,10 @@ describe('Validator', () => {
233233

234234
expect(validator.has(['first_name'])).toBeTruthy()
235235
expect(validator.first(['first_name'])).toEqual('This fist name field is required')
236+
expect(validator.first(['firstName'])).toEqual('This fist name field is required')
237+
238+
validator.clear(['first_name'])
239+
240+
expect(validator.has(['firstName'])).toBeFalsy()
236241
})
237242
})

src/core/Validator.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ class Validator {
6363
return this.has(field) ? this.missed(field) : null
6464
}
6565

66-
any(fields: string[] = [], returnObject?: boolean) {
66+
any(field: string[] = [], returnObject?: boolean) {
67+
const fields = this.fields(field)
6768
if (returnObject) {
6869
const errors: Record<string, any> = {}
69-
if (!fields.length) {
70-
return {}
70+
if (!fields.length) return {}
71+
for (const f of fields) {
72+
const val = this.get(f)
73+
if (!val.length) continue
74+
errors[f] = val
7175
}
72-
fields.forEach((key: string) => (errors[key] = this.get(key)))
7376
return errors
7477
}
7578
if (!fields.length) {
@@ -102,7 +105,7 @@ class Validator {
102105

103106
clear(field?: string | string[]) {
104107
if (!field) return this.flush()
105-
const errors = omit(cloneDeep(this.errors), field)
108+
const errors = omit(cloneDeep(this.errors), this.fields(field))
106109
this.fill(errors)
107110
}
108111

0 commit comments

Comments
 (0)