Skip to content

Commit 122f82f

Browse files
Merge pull request #78 from sebastianwessel/literal-type
Handle backticked fields
2 parents 1388ba7 + 8bfddb7 commit 122f82f

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/genSchema/generateZodSchemaCode.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { generateZodSchemaCode } from './generateZodSchemaCode.js'
2-
import { getDetailsFromDefinition } from './getDetailsFromDefinition.js'
2+
import { type FieldDetail, getDetailsFromDefinition } from './getDetailsFromDefinition.js'
33

44
describe('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 = `

src/genSchema/generateZodSchemaCode.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ export const generateZodSchemaCode = (fields: FieldDetail[], schemaName: string)
1313
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
1414
const buildSchema = (fieldMap: { [key: string]: any }, fields: FieldDetail[]) => {
1515
for (const field of fields) {
16-
const parts = field.name.split('.').map(part => part.replace('[*]', ''))
16+
const parts = field.name.split('.').map(originalPart => {
17+
const part = originalPart.replace('[*]', '')
18+
19+
if (part.startsWith('`') && part.endsWith('`')) {
20+
if (part.includes('-')) {
21+
return part.replace(/`/g, '"')
22+
}
23+
return part.replace(/`/g, '')
24+
}
25+
return part
26+
})
27+
1728
let current = fieldMap
1829

1930
// Todo - handle default values

0 commit comments

Comments
 (0)