Skip to content

Commit 370ede9

Browse files
chore: improve interfaces and types
1 parent 041d000 commit 370ede9

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

src/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ export interface StringValidatorType extends Validator<string>, LengthValidator<
277277
custom: (fn: (value: string) => boolean, message: string) => StringValidator
278278
}
279279

280-
export interface NumberValidatorType extends Validator<number> {
281-
min: (min: number) => NumberValidator
282-
max: (max: number) => NumberValidator
280+
export interface NumberValidatorType extends Validator<number>, LengthValidator<NumberValidator> {
283281
integer: (options?: IsIntOptions) => NumberValidator
284282
float: (options?: IsFloatOptions) => NumberValidator
285283
decimal: (options?: Parameters<typeof isDecimal>[1]) => NumberValidator
@@ -329,10 +327,8 @@ export interface UnixValidatorType extends Validator<number | string> {
329327
// Unix validator is simple, just implements the base Validator interface
330328
}
331329

332-
export interface PasswordValidatorType extends Validator<string> {
330+
export interface PasswordValidatorType extends Validator<string>, LengthValidator<PasswordValidator> {
333331
matches: (confirmPassword: string) => PasswordValidator
334-
min: (length?: number) => PasswordValidator
335-
max: (length?: number) => PasswordValidator
336332
hasUppercase: () => PasswordValidator
337333
hasLowercase: () => PasswordValidator
338334
hasNumbers: () => PasswordValidator

src/validators/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ValidationError, ValidationErrorMap, ValidationNames, ValidationRe
22

33
export abstract class BaseValidator<T> {
44
protected rules: ValidationRule<T>[] = []
5-
protected isRequired = true
5+
protected isRequired = false
66
protected fieldName = 'value'
77
protected isPartOfShape = false
88
public name: ValidationNames = 'base'

src/validators/numbers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export class NumberValidator extends BaseValidator<number> implements NumberVali
3535
})
3636
}
3737

38+
length(length: number): this {
39+
return this.addRule({
40+
name: 'length',
41+
test: (value: number) => value.toString().length === length,
42+
message: 'Must be exactly {length} digits',
43+
params: { length },
44+
})
45+
}
46+
3847
integer(options?: IsIntOptions): this {
3948
return this.addRule({
4049
name: 'integer',

src/validators/password.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export class PasswordValidator extends BaseValidator<string> implements Password
66

77
constructor() {
88
super()
9-
this.isRequired = false // Make password optional by default
109
this.addRule({
1110
name: 'string',
1211
test: (value: unknown): value is string => typeof value === 'string',
@@ -40,6 +39,15 @@ export class PasswordValidator extends BaseValidator<string> implements Password
4039
})
4140
}
4241

42+
length(length: number): this {
43+
return this.addRule({
44+
name: 'length',
45+
test: (value: string) => value.length === length,
46+
message: 'Must be exactly {length} characters long',
47+
params: { length },
48+
})
49+
}
50+
4351
hasUppercase(): this {
4452
return this.addRule({
4553
name: 'hasUppercase',

src/validators/strings.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class StringValidator extends BaseValidator<string> implements StringVali
1111

1212
constructor() {
1313
super()
14-
this.isRequired = false // Make string optional by default
1514
this.addRule({
1615
name: 'string',
1716
test: (value: unknown): value is string => typeof value === 'string',

test/validation.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Validation Library', () => {
2121
})
2222

2323
test('required string validation', () => {
24-
const validator = v.string().required()
24+
const validator = v.string().min(1).required()
2525
expect(validator.test('hello')).toBe(true)
2626
expect(validator.test('123')).toBe(true)
2727
expect(validator.test('')).toBe(false)
@@ -39,7 +39,7 @@ describe('Validation Library', () => {
3939
})
4040

4141
test('required string with other validations', () => {
42-
const validator = v.string().required().min(3).max(10)
42+
const validator = v.string().min(3).max(10).required()
4343
expect(validator.test('hello')).toBe(true)
4444
expect(validator.test('hi')).toBe(false) // too short
4545
expect(validator.test('hello world')).toBe(false) // too long
@@ -474,7 +474,7 @@ describe('Validation Library', () => {
474474

475475
describe('Object Validator', () => {
476476
test('basic object validation', () => {
477-
const validator = v.object()
477+
const validator = v.object().required()
478478
expect(validator.test({})).toBe(true)
479479
expect(validator.test({ foo: 'bar' })).toBe(true)
480480
expect(validator.test(null as any)).toBe(false)

0 commit comments

Comments
 (0)