11import { generateZodSchemaCode } from './generateZodSchemaCode.js'
2- import { getDetailsFromDefinition } from './getDetailsFromDefinition.js'
2+ import { type FieldDetail , getDetailsFromDefinition } from './getDetailsFromDefinition.js'
33
44describe ( 'generateZodSchemaCode' , ( ) => {
55 describe ( 'basic schema' , ( ) => {
@@ -25,6 +25,51 @@ describe('generateZodSchemaCode', () => {
2525 } )
2626 } )
2727
28+ describe ( 'Backticked and special field name handling' , ( ) => {
29+ it ( 'strips backticks from simple field names' , ( ) => {
30+ const fields : FieldDetail [ ] = [
31+ { name : '`type`' , table : 'test' , zodString : 'z.string()' , skip : false } ,
32+ { name : '`value`' , table : 'test' , zodString : 'z.number()' , skip : false } ,
33+ ]
34+ const result = generateZodSchemaCode ( fields , 'testSchema' )
35+ expect ( result ) . toEqualIgnoringWhitespace ( `
36+ const testSchema = z.object({
37+ type: z.string(),
38+ value: z.number()
39+ })
40+ ` )
41+ } )
42+
43+ it ( 'strips backticks and quotes field names containing hyphens' , ( ) => {
44+ const fields : FieldDetail [ ] = [
45+ { name : '`max-value`' , table : 'test' , zodString : 'z.number()' , skip : false } ,
46+ { name : '`field-with-hyphen`' , table : 'test' , zodString : 'z.string()' , skip : false } ,
47+ ]
48+ const result = generateZodSchemaCode ( fields , 'testSchema' )
49+ expect ( result ) . toEqualIgnoringWhitespace ( `
50+ const testSchema = z.object({
51+ "max-value": z.number(),
52+ "field-with-hyphen": z.string()
53+ })
54+ ` )
55+ } )
56+
57+ it ( 'strips backticks from field names that are JavaScript reserved words and quotes them' , ( ) => {
58+ const fields : FieldDetail [ ] = [
59+ { name : '`default`' , table : 'test' , zodString : 'z.string()' , skip : false } ,
60+ { name : '`const`' , table : 'test' , zodString : 'z.number()' , skip : false } ,
61+ ]
62+ const result = generateZodSchemaCode ( fields , 'testSchema' )
63+ // Assuming your sanitizeJSKey or equivalent quotes reserved words
64+ expect ( result ) . toEqualIgnoringWhitespace ( `
65+ const testSchema = z.object({
66+ default: z.string(),
67+ const: z.number()
68+ })
69+ ` )
70+ } )
71+ } )
72+
2873 describe ( 'object schema' , ( ) => {
2974 it ( 'returns schema for simple object' , ( ) => {
3075 const definition = `
0 commit comments