Skip to content

Commit

Permalink
chore: 🏷️ create simpleobject types and assign toe validator class
Browse files Browse the repository at this point in the history
  • Loading branch information
chantouchsek committed Sep 27, 2023
1 parent 5fdfe01 commit bfb733e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/core/Validator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { SimpleObject } from '../types'
import { cloneDeep, get, has, omit } from 'lodash'
import { is, toCamelCase, toSnakeCase } from '../util'

class Validator {
public successful: boolean
public processing: boolean

constructor(public errors: Record<string, any> = {}) {
constructor(public errors: SimpleObject<any> = {}) {
this.processing = false
this.successful = false
}
Expand Down Expand Up @@ -42,7 +43,7 @@ class Validator {
}
}

firstBy(obj: Record<string, any>, field?: string) {
firstBy(obj: SimpleObject<any>, field?: string) {
let value: string
if (!field) {
value = obj[Object.keys(obj)[0]]
Expand All @@ -64,7 +65,7 @@ class Validator {
any(field: string[] = [], returnObject?: boolean) {
const fields = this.fields(field)
if (returnObject) {
const errors: Record<string, any> = {}
const errors: SimpleObject<any> = {}
if (!fields.length) return {}
for (const f of fields) {
const val = this.get(f)
Expand All @@ -74,7 +75,7 @@ class Validator {
return errors
}
if (!fields.length) return Object.keys(this.errors).length > 0
const errors: Record<string, any> = {}
const errors: SimpleObject<any> = {}
fields.forEach((key: string) => (errors[key] = this.get(key)))
return Object.keys(errors).length > 0
}
Expand All @@ -91,7 +92,7 @@ class Validator {
return Object.keys(this.errors).length
}

fill(errors: Record<string, any>) {
fill(errors: SimpleObject<any>) {
this.errors = errors
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type SimpleObject<V> = Record<string, V>

0 comments on commit bfb733e

Please sign in to comment.