9
9
type Schema ,
10
10
type TypeFromShape ,
11
11
type ValidateOptions ,
12
+ type StringSchema ,
13
+ string as YupString ,
12
14
} from "yup"
13
15
14
16
export type _ < T > = T extends { }
@@ -30,6 +32,54 @@ export type ObjectSchemaFromShape<Shape extends ObjectShape> = ObjectSchema<
30
32
""
31
33
>
32
34
35
+ // -----------------------------------------------------------------------------
36
+ // Limited Character Sets
37
+ // -----------------------------------------------------------------------------
38
+
39
+ export function limitCharSet (
40
+ charSet : string ,
41
+ message : string ,
42
+ schema : StringSchema = YupString ( ) ,
43
+ ) {
44
+ return schema . matches ( new RegExp ( `^[${ charSet } ]*$` ) , message )
45
+ }
46
+
47
+ export function alphaString ( schema : StringSchema = YupString ( ) ) {
48
+ return limitCharSet (
49
+ "a-zA-Z" ,
50
+ "can only contain alpha characters (a-z, A-Z)" ,
51
+ schema ,
52
+ )
53
+ }
54
+
55
+ export function lowercaseAlphaString ( schema : StringSchema = YupString ( ) ) {
56
+ return limitCharSet (
57
+ "a-z" ,
58
+ "can only contain lowercase alpha characters (a-z)" ,
59
+ schema ,
60
+ )
61
+ }
62
+
63
+ export function uppercaseAlphaString ( schema : StringSchema = YupString ( ) ) {
64
+ return limitCharSet (
65
+ "A-Z" ,
66
+ "can only contain uppercase alpha characters (A-Z)" ,
67
+ schema ,
68
+ )
69
+ }
70
+
71
+ export function numericString ( schema : StringSchema = YupString ( ) ) {
72
+ return limitCharSet ( "0-9" , "can only contain numbers (0-9)" , schema )
73
+ }
74
+
75
+ export function alphanumericString ( schema : StringSchema = YupString ( ) ) {
76
+ return limitCharSet (
77
+ "a-zA-Z0-9" ,
78
+ "can only contain alphanumeric characters (a-z, A-Z, 0-9)" ,
79
+ schema ,
80
+ )
81
+ }
82
+
33
83
// -----------------------------------------------------------------------------
34
84
// Try Validate Sync
35
85
// -----------------------------------------------------------------------------
0 commit comments