Skip to content

Commit 694a0b4

Browse files
feat: implement interfaces
1 parent 2490f1a commit 694a0b4

File tree

13 files changed

+107
-15
lines changed

13 files changed

+107
-15
lines changed

src/types.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import type { alphanumeric } from './lib/isAlphanumeric'
2+
import type isDecimal from './lib/isDecimal'
3+
import type isEmail from './lib/isEmail'
4+
import type isURL from './lib/isURL'
25
import type { ArrayValidator } from './validators/arrays'
36
import type { BooleanValidator } from './validators/booleans'
47
import type { CustomValidator } from './validators/custom'
@@ -253,6 +256,86 @@ export interface ValidationConfig {
253256
errorMessages?: Record<string, string>
254257
}
255258

259+
export interface LengthValidator<T> {
260+
min: (length: number) => T
261+
max: (length: number) => T
262+
length: (length: number) => T
263+
}
264+
265+
export interface StringValidatorType extends Validator<string>, LengthValidator<StringValidator> {
266+
email: (options?: Parameters<typeof isEmail>[1]) => StringValidator
267+
url: (options?: Parameters<typeof isURL>[1]) => StringValidator
268+
matches: (pattern: RegExp) => StringValidator
269+
equals: (param: string) => StringValidator
270+
alphanumeric: () => StringValidator
271+
alpha: () => StringValidator
272+
numeric: () => StringValidator
273+
custom: (fn: (value: string) => boolean, message: string) => StringValidator
274+
}
275+
276+
export interface NumberValidatorType extends Validator<number> {
277+
min: (min: number) => NumberValidator
278+
max: (max: number) => NumberValidator
279+
integer: (options?: IsIntOptions) => NumberValidator
280+
float: (options?: IsFloatOptions) => NumberValidator
281+
decimal: (options?: Parameters<typeof isDecimal>[1]) => NumberValidator
282+
positive: () => NumberValidator
283+
negative: () => NumberValidator
284+
divisibleBy: (divisor: number) => NumberValidator
285+
custom: (fn: (value: number) => boolean, message: string) => NumberValidator
286+
}
287+
288+
export interface ArrayValidatorType<T> extends Validator<T[]>, LengthValidator<ArrayValidator<T>> {
289+
each: (validator: Validator<T>) => ArrayValidator<T>
290+
unique: () => ArrayValidator<T>
291+
}
292+
293+
export interface BooleanValidatorType extends Validator<boolean> {
294+
isTrue: () => BooleanValidator
295+
isFalse: () => BooleanValidator
296+
custom: (fn: (value: boolean) => boolean, message: string) => BooleanValidator
297+
}
298+
299+
export interface EnumValidatorType<T extends string | number> extends Validator<T> {
300+
custom: (fn: (value: T) => boolean, message: string) => EnumValidator<T>
301+
}
302+
303+
export interface DateValidatorType extends Validator<Date> {
304+
// Base date validator is simple, just implements the base Validator interface
305+
}
306+
307+
export interface DatetimeValidatorType extends Validator<Date> {
308+
// Datetime validator is simple, just implements the base Validator interface
309+
}
310+
311+
export interface ObjectValidatorType<T extends Record<string, any>> extends Validator<T> {
312+
shape: (schema: Record<string, Validator<any>>) => ObjectValidator<T>
313+
strict: (strict?: boolean) => ObjectValidator<T>
314+
}
315+
316+
export interface CustomValidatorType<T> extends Validator<T> {
317+
// Custom validator is simple, just implements the base Validator interface
318+
}
319+
320+
export interface TimestampValidatorType extends Validator<number | string> {
321+
// Timestamp validator is simple, just implements the base Validator interface
322+
}
323+
324+
export interface UnixValidatorType extends Validator<number | string> {
325+
// Unix validator is simple, just implements the base Validator interface
326+
}
327+
328+
export interface PasswordValidatorType extends Validator<string> {
329+
matches: (confirmPassword: string) => PasswordValidator
330+
min: (length?: number) => PasswordValidator
331+
max: (length?: number) => PasswordValidator
332+
hasUppercase: () => PasswordValidator
333+
hasLowercase: () => PasswordValidator
334+
hasNumbers: () => PasswordValidator
335+
hasSpecialCharacters: () => PasswordValidator
336+
alphanumeric: () => PasswordValidator
337+
}
338+
256339
export interface ValidationInstance {
257340
string: () => StringValidator
258341
number: () => NumberValidator

src/validators/arrays.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Validator } from '../types'
1+
import type { ArrayValidatorType, Validator } from '../types'
22
import { BaseValidator } from './base'
33

4-
export class ArrayValidator<T> extends BaseValidator<T[]> {
4+
export class ArrayValidator<T> extends BaseValidator<T[]> implements ArrayValidatorType<T> {
55
constructor() {
66
super()
77
this.addRule({

src/validators/booleans.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { BooleanValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class BooleanValidator extends BaseValidator<boolean> {
4+
export class BooleanValidator extends BaseValidator<boolean> implements BooleanValidatorType {
45
constructor() {
56
super()
67
this.addRule({

src/validators/custom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { CustomValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class CustomValidator<T> extends BaseValidator<T> {
4+
export class CustomValidator<T> extends BaseValidator<T> implements CustomValidatorType<T> {
45
constructor(validationFn: (value: T) => boolean, message: string) {
56
super()
67
this.addRule({

src/validators/dates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { DateValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class DateValidator extends BaseValidator<Date> {
4+
export class DateValidator extends BaseValidator<Date> implements DateValidatorType {
45
constructor() {
56
super()
67
this.addRule({

src/validators/datetimes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { DatetimeValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class DatetimeValidator extends BaseValidator<Date> {
4+
export class DatetimeValidator extends BaseValidator<Date> implements DatetimeValidatorType {
45
constructor() {
56
super()
67
this.addRule({

src/validators/enums.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { EnumValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class EnumValidator<T extends string | number> extends BaseValidator<T> {
4+
export class EnumValidator<T extends string | number> extends BaseValidator<T> implements EnumValidatorType<T> {
45
private allowedValues: readonly T[]
56

67
constructor(allowedValues: readonly T[]) {

src/validators/numbers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { IsFloatOptions, IsIntOptions } from '../types'
1+
import type { IsFloatOptions, IsIntOptions, NumberValidatorType } from '../types'
22
import isDecimal from '../lib/isDecimal'
33
import isDivisibleBy from '../lib/isDivisibleBy'
44
import isFloat from '../lib/isFloat'
55
import isInt from '../lib/isInt'
66
import { BaseValidator } from './base'
77

8-
export class NumberValidator extends BaseValidator<number> {
8+
export class NumberValidator extends BaseValidator<number> implements NumberValidatorType {
99
constructor() {
1010
super()
1111
this.addRule({

src/validators/objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { ValidationErrorMap, ValidationResult, Validator } from '../types'
1+
import type { ObjectValidatorType, ValidationErrorMap, ValidationResult, Validator } from '../types'
22
import { BaseValidator } from './base'
33

4-
export class ObjectValidator<T extends Record<string, any>> extends BaseValidator<T> {
4+
export class ObjectValidator<T extends Record<string, any>> extends BaseValidator<T> implements ObjectValidatorType<T> {
55
private schema: Record<string, Validator<any>> = {}
66
private strictMode = false
77

src/validators/password.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { PasswordValidatorType } from '../types'
12
import { BaseValidator } from './base'
23

3-
export class PasswordValidator extends BaseValidator<string> {
4+
export class PasswordValidator extends BaseValidator<string> implements PasswordValidatorType {
45
constructor() {
56
super()
67
this.addRule({

0 commit comments

Comments
 (0)