@@ -42,27 +42,29 @@ export function numericId(schema: NumberSchema = YupNumber()) {
42
42
// Limited Character Sets
43
43
// -----------------------------------------------------------------------------
44
44
45
- export function limitCharSet (
45
+ export function matchesCharSet (
46
46
charSet : string ,
47
47
message : string ,
48
48
schema : StringSchema = YupString ( ) ,
49
49
) {
50
50
return schema . matches ( new RegExp ( `^[${ charSet } ]*$` ) , message )
51
51
}
52
52
53
- type CharSetOptions = Partial < {
53
+ export type BuildCharSetOptions = Partial < {
54
54
schema : StringSchema
55
55
spaces : boolean
56
56
specialChars : string
57
57
} >
58
58
59
- function charSet (
59
+ export function buildCharSet (
60
60
charSet : string ,
61
- message : string ,
62
- options : CharSetOptions = { } ,
61
+ description : string ,
62
+ options : BuildCharSetOptions = { } ,
63
63
) {
64
64
const { schema, spaces = false , specialChars } = options
65
65
66
+ let message = `can only contain: ${ description } `
67
+
66
68
if ( spaces ) {
67
69
charSet += " "
68
70
message += ", spaces"
@@ -72,57 +74,45 @@ function charSet(
72
74
message += `, special characters (${ specialChars } )`
73
75
}
74
76
75
- return limitCharSet ( charSet , message , schema )
77
+ return matchesCharSet ( charSet , message , schema )
76
78
}
77
79
78
- export function alphaString ( options ?: CharSetOptions ) {
79
- return charSet (
80
- "a-zA-Z" ,
81
- "can only contain alpha characters (a-z, A-Z)" ,
82
- options ,
83
- )
80
+ export function alphaString ( options ?: BuildCharSetOptions ) {
81
+ return buildCharSet ( "a-zA-Z" , "alpha characters (a-z, A-Z)" , options )
84
82
}
85
83
86
- export function lowercaseAlphaString ( options ?: CharSetOptions ) {
87
- return charSet (
88
- "a-z" ,
89
- "can only contain lowercase alpha characters (a-z)" ,
90
- options ,
91
- )
84
+ export function lowercaseAlphaString ( options ?: BuildCharSetOptions ) {
85
+ return buildCharSet ( "a-z" , "lowercase alpha characters (a-z)" , options )
92
86
}
93
87
94
- export function lowercaseAlphanumericString ( options ?: CharSetOptions ) {
95
- return charSet (
88
+ export function lowercaseAlphanumericString ( options ?: BuildCharSetOptions ) {
89
+ return buildCharSet (
96
90
"a-z0-9" ,
97
- "can only contain lowercase alphanumeric characters (a-z, 0-9)" ,
91
+ "lowercase alphanumeric characters (a-z, 0-9)" ,
98
92
options ,
99
93
)
100
94
}
101
95
102
- export function uppercaseAlphaString ( options ?: CharSetOptions ) {
103
- return charSet (
104
- "A-Z" ,
105
- "can only contain uppercase alpha characters (A-Z)" ,
106
- options ,
107
- )
96
+ export function uppercaseAlphaString ( options ?: BuildCharSetOptions ) {
97
+ return buildCharSet ( "A-Z" , "uppercase alpha characters (A-Z)" , options )
108
98
}
109
99
110
- export function uppercaseAlphanumericString ( options ?: CharSetOptions ) {
111
- return charSet (
100
+ export function uppercaseAlphanumericString ( options ?: BuildCharSetOptions ) {
101
+ return buildCharSet (
112
102
"A-Z0-9" ,
113
- "can only contain uppercase alphanumeric characters (A-Z, 0-9)" ,
103
+ "uppercase alphanumeric characters (A-Z, 0-9)" ,
114
104
options ,
115
105
)
116
106
}
117
107
118
- export function numericString ( options ?: CharSetOptions ) {
119
- return charSet ( "0-9" , "can only contain numbers (0-9)" , options )
108
+ export function numericString ( options ?: BuildCharSetOptions ) {
109
+ return buildCharSet ( "0-9" , "numbers (0-9)" , options )
120
110
}
121
111
122
- export function alphanumericString ( options ?: CharSetOptions ) {
123
- return charSet (
112
+ export function alphanumericString ( options ?: BuildCharSetOptions ) {
113
+ return buildCharSet (
124
114
"a-zA-Z0-9" ,
125
- "can only contain alphanumeric characters (a-z, A-Z, 0-9)" ,
115
+ "alphanumeric characters (a-z, A-Z, 0-9)" ,
126
116
options ,
127
117
)
128
118
}
0 commit comments