Skip to content

Commit 31dada6

Browse files
chore: implement more types
1 parent bb5d305 commit 31dada6

File tree

13 files changed

+275
-3
lines changed

13 files changed

+275
-3
lines changed

src/types/base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export type ValidationNames = 'base' |
7575
'float' |
7676
'decimal' |
7777
'time' |
78-
'smallint'
78+
'smallint' |
79+
'integer' |
80+
'json' |
81+
'blob' |
82+
'binary'
7983

8084
export type Infer<T> = T extends Validator<infer U> ? U : never

src/types/binary.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { BinaryValidator } from '../validators/binary'
2+
import type { LengthValidator, Validator } from './base'
3+
4+
export interface BinaryValidatorType extends Validator<string>, LengthValidator<BinaryValidator> {
5+
custom: (fn: (value: string) => boolean, message: string) => BinaryValidator
6+
}

src/types/blob.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { BlobValidator } from '../validators/blob'
2+
import type { LengthValidator, Validator } from './base'
3+
4+
export interface BlobValidatorType extends Validator<string>, LengthValidator<BlobValidator> {
5+
custom: (fn: (value: string) => boolean, message: string) => BlobValidator
6+
}

src/types/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import type { ArrayValidatorType } from './array'
2+
import type { BinaryValidatorType } from './binary'
3+
import type { BlobValidatorType } from './blob'
24
import type { BooleanValidatorType } from './boolean'
35
import type { CustomValidatorType } from './custom'
46
import type { DatetimeValidatorType, DateValidatorType } from './date'
57
import type { DecimalValidatorType } from './decimal'
68
import type { EnumValidatorType } from './enum'
79
import type { FloatValidatorType } from './float'
10+
import type { IntegerValidatorType } from './integer'
11+
import type { JsonValidatorType } from './json'
812
import type { BigintValidatorType, NumberValidatorType } from './number'
913
import type { ObjectValidatorType } from './object'
1014
import type { PasswordValidatorType } from './password'
@@ -19,16 +23,19 @@ export * from './array'
1923

2024
// Base types
2125
export * from './base'
26+
export * from './binary'
27+
export * from './blob'
2228
export * from './boolean'
2329
export * from './custom'
2430
export * from './date'
2531
export * from './decimal'
26-
export * from './decimal'
2732
export * from './enum'
2833
export * from './float'
34+
2935
export * from './float'
36+
export * from './integer'
37+
export * from './json'
3038
export * from './number'
31-
3239
export * from './object'
3340
// Options types
3441
export * from './options'
@@ -60,6 +67,10 @@ export interface ValidationInstance {
6067
decimal: () => DecimalValidatorType
6168
time: () => TimeValidatorType
6269
smallint: () => SmallintValidatorType
70+
integer: () => IntegerValidatorType
71+
json: () => JsonValidatorType
72+
blob: () => BlobValidatorType
73+
binary: () => BinaryValidatorType
6374
}
6475

6576
export type ValidationType = {

src/types/integer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { IntegerValidator } from '../validators/integer'
2+
import type { NumberValidatorType } from './number'
3+
4+
export interface IntegerValidatorType extends NumberValidatorType {
5+
// IntegerValidator inherits all methods from NumberValidatorType
6+
// but ensures the value is an integer
7+
}

src/types/json.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { JsonValidator } from '../validators/json'
2+
import type { LengthValidator, Validator } from './base'
3+
4+
export interface JsonValidatorType extends Validator<string>, LengthValidator<JsonValidator> {
5+
custom: (fn: (value: string) => boolean, message: string) => JsonValidator
6+
}

src/validation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import type { ValidationInstance } from './types'
22
import { array } from './validators/arrays'
33
import { bigint } from './validators/bigint'
4+
import { binary } from './validators/binary'
5+
import { blob } from './validators/blob'
46
import { boolean } from './validators/booleans'
57
import { custom } from './validators/custom'
68
import { date } from './validators/dates'
79
import { datetime } from './validators/datetimes'
810
import { decimal } from './validators/decimal'
911
import { enum_ } from './validators/enums'
1012
import { float } from './validators/float'
13+
import { integer } from './validators/integer'
14+
import { json } from './validators/json'
1115
import { number } from './validators/numbers'
1216
import { object } from './validators/objects'
1317
import { password } from './validators/password'
@@ -39,4 +43,8 @@ export const v: ValidationInstance = {
3943
decimal,
4044
time,
4145
smallint,
46+
integer,
47+
json,
48+
blob,
49+
binary,
4250
}

src/validators/binary.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { BinaryValidatorType, ValidationNames } from '../types'
2+
import { BaseValidator } from './base'
3+
4+
export class BinaryValidator extends BaseValidator<string> implements BinaryValidatorType {
5+
public name: ValidationNames = 'binary'
6+
7+
constructor() {
8+
super()
9+
this.addRule({
10+
name: 'binary',
11+
test: (value: unknown): value is string => {
12+
if (typeof value !== 'string') {
13+
return false
14+
}
15+
// Basic binary validation - could be base64 encoded data or hex string
16+
// This is a simple check, could be enhanced based on specific requirements
17+
return value.length > 0
18+
},
19+
message: 'Must be a valid binary string',
20+
})
21+
}
22+
23+
min(min: number): this {
24+
return this.addRule({
25+
name: 'min',
26+
test: (value: string) => value.length >= min,
27+
message: 'Must be at least {min} characters',
28+
params: { min },
29+
})
30+
}
31+
32+
max(max: number): this {
33+
return this.addRule({
34+
name: 'max',
35+
test: (value: string) => value.length <= max,
36+
message: 'Must be at most {max} characters',
37+
params: { max },
38+
})
39+
}
40+
41+
length(length: number): this {
42+
return this.addRule({
43+
name: 'length',
44+
test: (value: string) => value.length === length,
45+
message: 'Must be exactly {length} characters',
46+
params: { length },
47+
})
48+
}
49+
50+
custom(fn: (value: string) => boolean, message: string): this {
51+
return this.addRule({
52+
name: 'custom',
53+
test: fn,
54+
message,
55+
})
56+
}
57+
}
58+
59+
// Export a function to create binary validators
60+
export function binary(): BinaryValidator {
61+
return new BinaryValidator()
62+
}

src/validators/blob.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { BlobValidatorType, ValidationNames } from '../types'
2+
import { BaseValidator } from './base'
3+
4+
export class BlobValidator extends BaseValidator<string> implements BlobValidatorType {
5+
public name: ValidationNames = 'blob'
6+
7+
constructor() {
8+
super()
9+
this.addRule({
10+
name: 'blob',
11+
test: (value: unknown): value is string => {
12+
if (typeof value !== 'string') {
13+
return false
14+
}
15+
// Basic blob validation - could be base64 encoded data
16+
// This is a simple check, could be enhanced based on specific requirements
17+
return value.length > 0
18+
},
19+
message: 'Must be a valid blob string',
20+
})
21+
}
22+
23+
min(min: number): this {
24+
return this.addRule({
25+
name: 'min',
26+
test: (value: string) => value.length >= min,
27+
message: 'Must be at least {min} characters',
28+
params: { min },
29+
})
30+
}
31+
32+
max(max: number): this {
33+
return this.addRule({
34+
name: 'max',
35+
test: (value: string) => value.length <= max,
36+
message: 'Must be at most {max} characters',
37+
params: { max },
38+
})
39+
}
40+
41+
length(length: number): this {
42+
return this.addRule({
43+
name: 'length',
44+
test: (value: string) => value.length === length,
45+
message: 'Must be exactly {length} characters',
46+
params: { length },
47+
})
48+
}
49+
50+
custom(fn: (value: string) => boolean, message: string): this {
51+
return this.addRule({
52+
name: 'custom',
53+
test: fn,
54+
message,
55+
})
56+
}
57+
}
58+
59+
// Export a function to create blob validators
60+
export function blob(): BlobValidator {
61+
return new BlobValidator()
62+
}

src/validators/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
export * from './arrays'
2+
export * from './binary'
3+
export * from './blob'
24
export * from './booleans'
35
export * from './custom'
46
export * from './dates'
57
export * from './datetimes'
68
export * from './decimal'
79
export * from './enums'
810
export * from './float'
11+
export * from './integer'
12+
export * from './json'
913
export * from './numbers'
1014
export * from './objects'
1115
export * from './password'

0 commit comments

Comments
 (0)