@@ -10,7 +10,9 @@ import {
10
10
type TypeFromShape ,
11
11
type ValidateOptions ,
12
12
type StringSchema ,
13
+ type NumberSchema ,
13
14
string as YupString ,
15
+ number as YupNumber ,
14
16
} from "yup"
15
17
16
18
export type _ < T > = T extends { }
@@ -32,6 +34,10 @@ export type ObjectSchemaFromShape<Shape extends ObjectShape> = ObjectSchema<
32
34
""
33
35
>
34
36
37
+ export function numericId ( schema : NumberSchema = YupNumber ( ) ) {
38
+ return schema . min ( 1 )
39
+ }
40
+
35
41
// -----------------------------------------------------------------------------
36
42
// Limited Character Sets
37
43
// -----------------------------------------------------------------------------
@@ -44,39 +50,80 @@ export function limitCharSet(
44
50
return schema . matches ( new RegExp ( `^[${ charSet } ]*$` ) , message )
45
51
}
46
52
47
- export function alphaString ( schema : StringSchema = YupString ( ) ) {
48
- return limitCharSet (
53
+ type CharSetOptions = Partial < {
54
+ schema : StringSchema
55
+ spaces : boolean
56
+ specialChars : string
57
+ } >
58
+
59
+ function charSet (
60
+ charSet : string ,
61
+ message : string ,
62
+ options : CharSetOptions = { } ,
63
+ ) {
64
+ const { schema, spaces = false , specialChars } = options
65
+
66
+ if ( spaces ) {
67
+ charSet += " "
68
+ message += ", spaces"
69
+ }
70
+ if ( specialChars ) {
71
+ charSet += specialChars
72
+ message += `, special characters (${ specialChars } )`
73
+ }
74
+
75
+ return limitCharSet ( charSet , message , schema )
76
+ }
77
+
78
+ export function alphaString ( options ?: CharSetOptions ) {
79
+ return charSet (
49
80
"a-zA-Z" ,
50
81
"can only contain alpha characters (a-z, A-Z)" ,
51
- schema ,
82
+ options ,
52
83
)
53
84
}
54
85
55
- export function lowercaseAlphaString ( schema : StringSchema = YupString ( ) ) {
56
- return limitCharSet (
86
+ export function lowercaseAlphaString ( options ?: CharSetOptions ) {
87
+ return charSet (
57
88
"a-z" ,
58
89
"can only contain lowercase alpha characters (a-z)" ,
59
- schema ,
90
+ options ,
60
91
)
61
92
}
62
93
63
- export function uppercaseAlphaString ( schema : StringSchema = YupString ( ) ) {
64
- return limitCharSet (
94
+ export function lowercaseAlphanumericString ( options ?: CharSetOptions ) {
95
+ return charSet (
96
+ "a-z0-9" ,
97
+ "can only contain lowercase alphanumeric characters (a-z, 0-9)" ,
98
+ options ,
99
+ )
100
+ }
101
+
102
+ export function uppercaseAlphaString ( options ?: CharSetOptions ) {
103
+ return charSet (
65
104
"A-Z" ,
66
105
"can only contain uppercase alpha characters (A-Z)" ,
67
- schema ,
106
+ options ,
107
+ )
108
+ }
109
+
110
+ export function uppercaseAlphanumericString ( options ?: CharSetOptions ) {
111
+ return charSet (
112
+ "A-Z0-9" ,
113
+ "can only contain uppercase alphanumeric characters (A-Z, 0-9)" ,
114
+ options ,
68
115
)
69
116
}
70
117
71
- export function numericString ( schema : StringSchema = YupString ( ) ) {
72
- return limitCharSet ( "0-9" , "can only contain numbers (0-9)" , schema )
118
+ export function numericString ( options ?: CharSetOptions ) {
119
+ return charSet ( "0-9" , "can only contain numbers (0-9)" , options )
73
120
}
74
121
75
- export function alphanumericString ( schema : StringSchema = YupString ( ) ) {
76
- return limitCharSet (
122
+ export function alphanumericString ( options ?: CharSetOptions ) {
123
+ return charSet (
77
124
"a-zA-Z0-9" ,
78
125
"can only contain alphanumeric characters (a-z, A-Z, 0-9)" ,
79
- schema ,
126
+ options ,
80
127
)
81
128
}
82
129
0 commit comments