Skip to content

Commit 32f062b

Browse files
committed
chore: few minor updates
1 parent 6f4ce71 commit 32f062b

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

src/lib/isEmail.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function validateDisplayName(display_name: string): boolean {
6060
return true
6161
}
6262

63-
export default function isEmail(str: string, options: any): boolean {
63+
export default function isEmail(str: string, options: any = {}): boolean {
6464
assertString(str)
6565
options = merge(options, default_email_options)
6666

src/lib/ltrim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assertString from './util/assertString'
77
* @param chars - Options object
88
* @returns The processed string
99
*/
10-
export default function ltrim(str: string, chars: string): string {
10+
export default function ltrim(str: string, chars?: string): string {
1111
assertString(str)
1212
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
1313
const pattern = chars ? new RegExp(`^[${chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}]+`, 'g') : /^\s+/g

src/lib/rtrim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assertString from './util/assertString'
77
* @param chars - Options object
88
* @returns The processed string
99
*/
10-
export default function rtrim(str: string, chars: string): string {
10+
export default function rtrim(str: string, chars?: string): string {
1111
assertString(str)
1212
if (chars) {
1313
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping

src/lib/trim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import rtrim from './rtrim'
88
* @param chars - Options object
99
* @returns The processed string
1010
*/
11-
export default function trim(str: string, chars: string): string {
11+
export default function trim(str: string, chars?: string): string {
1212
return rtrim(ltrim(str, chars), chars)
1313
}

src/validators/objects.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ export class ObjectValidator<T extends Record<string, any>> extends BaseValidato
77
private schema: Record<string, Validator<any>> = {}
88
private strictMode = false
99

10-
constructor() {
10+
constructor(schema?: Record<string, Validator<any>>) {
1111
super()
1212
this.addRule({
1313
name: 'object',
1414
test: (value: unknown): value is T =>
1515
typeof value === 'object' && value !== null && !Array.isArray(value),
1616
message: 'Must be an object',
1717
})
18+
19+
// If schema is provided in constructor, set it up immediately
20+
if (schema) {
21+
this.shape(schema)
22+
}
1823
}
1924

2025
shape(schema: Record<string, Validator<any>>): this {
@@ -53,6 +58,14 @@ export class ObjectValidator<T extends Record<string, any>> extends BaseValidato
5358
return this
5459
}
5560

61+
custom(validationFn: (value: T) => boolean, message: string): this {
62+
return this.addRule({
63+
name: 'custom',
64+
test: (value: T) => value === undefined || value === null || validationFn(value),
65+
message,
66+
})
67+
}
68+
5669
validate(value: T): ValidationResult {
5770
const result = super.validate(value)
5871
if (!result.valid)
@@ -97,6 +110,6 @@ export class ObjectValidator<T extends Record<string, any>> extends BaseValidato
97110
}
98111
}
99112

100-
export function object<T extends Record<string, any>>(): ObjectValidator<T> {
101-
return new ObjectValidator<T>()
113+
export function object<T extends Record<string, any>>(schema?: Record<string, Validator<any>>): ObjectValidator<T> {
114+
return new ObjectValidator<T>(schema)
102115
}

0 commit comments

Comments
 (0)