11import { expect } from 'chai'
2+ import { oneLineTrim } from 'common-tags'
23import { z } from 'zod'
34
5+ import { ZodToStringMapper } from '../../src/codegen/language-mappers/zod-to-string-mapper.js'
46import { SchemaExtractor } from '../../src/codegen/schema-extractor.js'
57import { type Config , type ConfigFile } from '../../src/codegen/types.js'
68
79// Custom duration type map returning a string
810const durationTypeMap = ( ) => z . string ( )
911
12+ function expectSchemaMatchesString ( schema : z . ZodTypeAny , expectedString : string ) : void {
13+ const schemaString = new ZodToStringMapper ( ) . resolveType ( schema ) . split ( ' ' ) . join ( '' )
14+ const expectedStringResult = expectedString . split ( ' ' ) . join ( '' )
15+
16+ expect ( oneLineTrim ( schemaString ) ) . to . equal ( oneLineTrim ( expectedStringResult ) )
17+ }
18+
1019describe ( 'SchemaExtractor' , ( ) => {
1120 let mockLog : ( category : string | unknown , message ?: unknown ) => void
1221 let schemaExtractor : SchemaExtractor
@@ -54,10 +63,15 @@ describe('SchemaExtractor', () => {
5463
5564 const result = schemaExtractor . execute ( { config, configFile} )
5665
57- expect ( result . _def . typeName ) . equal ( 'ZodObject' )
58- expect ( Object . keys ( result . _def . shape ( ) ) . sort ( ) ) . to . deep . equal ( [ 'age' , 'name' ] )
59- expect ( result . _def . shape ( ) . age . _def . typeName ) . equal ( 'ZodNumber' )
60- expect ( result . _def . shape ( ) . name . _def . typeName ) . equal ( 'ZodString' )
66+ expectSchemaMatchesString (
67+ result ,
68+ `
69+ z.object({
70+ name: z.string();
71+ age: z.number().int()
72+ })
73+ ` ,
74+ )
6175 } )
6276
6377 it ( 'should infer schema when no user-defined schema is available' , ( ) => {
@@ -84,9 +98,7 @@ describe('SchemaExtractor', () => {
8498 }
8599
86100 const result = schemaExtractor . execute ( { config, configFile} )
87-
88- // Verify the result is a string schema
89- expect ( result . _def . typeName ) . to . equal ( 'ZodString' )
101+ expectSchemaMatchesString ( result , 'z.string()' )
90102 } )
91103
92104 it ( 'should infer a union of schema when multiple schemas are found' , ( ) => {
@@ -135,16 +147,17 @@ describe('SchemaExtractor', () => {
135147
136148 const result = schemaExtractor . execute ( { config, configFile} )
137149
138- expect ( result . _def . typeName ) . to . equal ( 'ZodUnion' )
139- expect ( result . _def . options . length ) . to . equal ( 2 )
140- expect ( result . _def . options [ 0 ] . _def . typeName ) . to . equal ( 'ZodObject' )
141- expect ( Object . keys ( result . _def . options [ 0 ] . _def . shape ( ) ) . sort ( ) ) . to . deep . equal ( [
142- 'enterprise' ,
143- 'premium' ,
144- 'standard' ,
145- ] )
146- expect ( result . _def . options [ 1 ] . _def . typeName ) . to . equal ( 'ZodObject' )
147- expect ( Object . keys ( result . _def . options [ 1 ] . _def . shape ( ) ) . sort ( ) ) . to . deep . equal ( [ 'freemium' , 'premium' , 'standard' ] )
150+ expectSchemaMatchesString (
151+ result ,
152+ `
153+ z.union(
154+ [
155+ z.object({enterprise: z.number(); premium: z.number(); standard: z.number()}),
156+ z.object({freemium: z.number(); premium: z.number(); standard: z.number()})
157+ ]
158+ )
159+ ` ,
160+ )
148161 } )
149162
150163 it ( 'should use custom duration type map when provided' , ( ) => {
@@ -172,8 +185,7 @@ describe('SchemaExtractor', () => {
172185
173186 const result = schemaExtractor . execute ( { config, configFile, durationTypeMap} )
174187
175- // Verify the result uses our custom duration type
176- expect ( result . _def . typeName ) . to . equal ( 'ZodString' )
188+ expectSchemaMatchesString ( result , `z.string()` )
177189 } )
178190
179191 it ( 'should replace strings with Mustache templates when found' , ( ) => {
@@ -201,14 +213,20 @@ describe('SchemaExtractor', () => {
201213
202214 const result = schemaExtractor . execute ( { config, configFile} )
203215
204- // Verify the result is a function schema that takes a name string param and returns a string
205- expect ( result . _def . typeName ) . to . equal ( 'ZodFunction' )
206- expect ( result . _def . args . _def . typeName ) . to . equal ( 'ZodTuple' )
207- expect ( result . _def . args . _def . items . length ) . to . equal ( 1 )
208- expect ( result . _def . args . _def . items [ 0 ] . _def . typeName ) . to . equal ( 'ZodObject' )
209- expect ( Object . keys ( result . _def . args . _def . items [ 0 ] . _def . shape ( ) ) ) . to . deep . equal ( [ 'name' ] )
210- expect ( result . _def . args . _def . items [ 0 ] . _def . shape ( ) . name . _def . typeName ) . to . equal ( 'ZodString' )
211- expect ( result . _def . returns . _def . typeName ) . to . equal ( 'ZodString' )
216+ expectSchemaMatchesString (
217+ result ,
218+ `
219+ z.function()
220+ .args(
221+ z.object({
222+ name: z.string()
223+ })
224+ )
225+ .returns(
226+ z.string()
227+ )
228+ ` ,
229+ )
212230 } )
213231
214232 it ( 'should replace strings with a union of Mustache templates when found' , ( ) => {
@@ -241,22 +259,25 @@ describe('SchemaExtractor', () => {
241259
242260 const result = schemaExtractor . execute ( { config, configFile} )
243261
244- // Verify the result is a function schema that takes a name string param and returns a string
245- expect ( result . _def . typeName ) . to . equal ( 'ZodFunction' )
246- expect ( result . _def . args . _def . typeName ) . to . equal ( 'ZodTuple' )
247- expect ( result . _def . args . _def . items . length ) . to . equal ( 1 )
248- expect ( result . _def . args . _def . items [ 0 ] . _def . typeName ) . to . equal ( 'ZodUnion' )
249- expect ( result . _def . args . _def . items [ 0 ] . options . length ) . to . equal ( 2 )
250- expect ( result . _def . args . _def . items [ 0 ] . options [ 1 ] . _def . typeName ) . to . equal ( 'ZodObject' )
251- expect ( Object . keys ( result . _def . args . _def . items [ 0 ] . options [ 0 ] . _def . shape ( ) ) . sort ( ) ) . to . deep . equal ( [ 'name' ] )
252- expect ( result . _def . args . _def . items [ 0 ] . _def . options [ 0 ] . _def . shape ( ) . name . _def . typeName ) . to . equal ( 'ZodString' )
253- expect ( Object . keys ( result . _def . args . _def . items [ 0 ] . options [ 1 ] . _def . shape ( ) ) . sort ( ) ) . to . deep . equal ( [
254- 'differentName' ,
255- ] )
256- expect ( result . _def . args . _def . items [ 0 ] . _def . options [ 1 ] . _def . shape ( ) . differentName . _def . typeName ) . to . equal (
257- 'ZodString' ,
262+ expectSchemaMatchesString (
263+ result ,
264+ `
265+ z.function()
266+ .args(
267+ z.union([
268+ z.object({
269+ name: z.string()
270+ }),
271+ z.object({
272+ differentName: z.string()
273+ })
274+ ])
275+ )
276+ .returns(
277+ z.string()
278+ )
279+ ` ,
258280 )
259- expect ( result . _def . returns . _def . typeName ) . to . equal ( 'ZodString' )
260281 } )
261282 } )
262283} )
0 commit comments