File tree 2 files changed +25
-8
lines changed
packages/zod-mock/src/lib
2 files changed +25
-8
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,22 @@ describe('zod-mock', () => {
113
113
return ;
114
114
} ) ;
115
115
116
+ it ( 'Should manually mock string key names to set values when the validation has regex' , ( ) => {
117
+ const schema = z . object ( {
118
+ telephone : z . string ( ) . regex ( / ^ \+ [ 1 - 9 ] \d { 1 , 14 } $ / )
119
+ } ) ;
120
+
121
+ const stringMap = {
122
+ telephone : ( ) => '+919367788755' ,
123
+ } ;
124
+
125
+ const mockData = generateMock ( schema , { stringMap } ) ;
126
+
127
+ expect ( mockData . telephone ) . toEqual ( '+919367788755' ) ;
128
+
129
+ return ;
130
+ } ) ;
131
+
116
132
it ( 'should convert values produced by Faker to string when the schema type is string.' , ( ) => {
117
133
const schema = z . object ( {
118
134
number : z . string ( ) ,
Original file line number Diff line number Diff line change @@ -125,6 +125,15 @@ function parseString(
125
125
zodRef : z . ZodString ,
126
126
options ?: GenerateMockOptions
127
127
) : string | number | boolean {
128
+ // Prioritize user provided generators.
129
+ if ( options ?. keyName && options . stringMap ) {
130
+ // min/max length handling is not applied here
131
+ const generator = options . stringMap [ options . keyName ] ;
132
+ if ( generator ) {
133
+ return generator ( ) ;
134
+ }
135
+ }
136
+
128
137
const fakerInstance = options ?. faker || faker ;
129
138
const { checks = [ ] } = zodRef . _def ;
130
139
@@ -142,14 +151,6 @@ function parseString(
142
151
}
143
152
144
153
const lowerCaseKeyName = options ?. keyName ?. toLowerCase ( ) ;
145
- // Prioritize user provided generators.
146
- if ( options ?. keyName && options . stringMap ) {
147
- // min/max length handling is not applied here
148
- const generator = options . stringMap [ options . keyName ] ;
149
- if ( generator ) {
150
- return generator ( ) ;
151
- }
152
- }
153
154
const stringOptions : {
154
155
min ?: number ;
155
156
max ?: number ;
You can’t perform that action at this time.
0 commit comments