Skip to content

Commit 0bb5c40

Browse files
fix(zui): do not generate .optional().secret() (#626)
1 parent e583a4b commit 0bb5c40

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

zui/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bpinternal/zui",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A fork of Zod with additional features",
55
"type": "module",
66
"source": "./src/index.ts",
@@ -55,4 +55,4 @@
5555
"pnpm": "8.6.2"
5656
},
5757
"packageManager": "[email protected]"
58-
}
58+
}

zui/src/transforms/zui-to-typescript-schema/index.test.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test } from 'vitest'
1+
import { describe, expect, test, it } from 'vitest'
22
import { toTypescriptSchema as toTypescript } from '.'
33
import { evalZuiString } from '../common/eval-zui-string'
44
import * as errors from '../common/errors'
@@ -30,7 +30,7 @@ const assert = (source: ZodType) => ({
3030
},
3131
})
3232

33-
describe.concurrent('toTypescriptZuiString', () => {
33+
describe.concurrent('toTypescriptSchema', () => {
3434
describe.concurrent('string', () => {
3535
test('no checks', () => {
3636
const schema = z.string()
@@ -152,6 +152,62 @@ describe.concurrent('toTypescriptZuiString', () => {
152152
const evaluated = generate(schema)
153153
expect(evaluated._def.checks).toEqual([{ kind: 'ip', message: undefined }])
154154
})
155+
156+
describe.concurrent('optional', () => {
157+
it('should preserve .secret modifier', () => {
158+
// Arrange
159+
const schema = z.string().secret().optional()
160+
161+
// Act & Assert
162+
assert(schema).toGenerateItself()
163+
expect(toTypescript(schema)).toContain('z.string().secret(')
164+
})
165+
166+
it('should preserve .disabled modifier', () => {
167+
// Arrange
168+
const schema = z.string().disabled().optional()
169+
170+
// Act & Assert
171+
assert(schema).toGenerateItself()
172+
expect(toTypescript(schema)).toContain('z.string().disabled(')
173+
})
174+
175+
it('should preserve .hidden modifier', () => {
176+
// Arrange
177+
const schema = z.string().hidden().optional()
178+
179+
// Act & Assert
180+
assert(schema).toGenerateItself()
181+
expect(toTypescript(schema)).toContain('z.string().hidden(')
182+
})
183+
184+
it('should preserve .title modifier', () => {
185+
// Arrange
186+
const schema = z.string().title('a').optional()
187+
188+
// Act & Assert
189+
assert(schema).toGenerateItself()
190+
expect(toTypescript(schema)).toContain('z.string().title(')
191+
})
192+
193+
it('should preserve .placeholder modifier', () => {
194+
// Arrange
195+
const schema = z.string().placeholder('a').optional()
196+
197+
// Act & Assert
198+
assert(schema).toGenerateItself()
199+
expect(toTypescript(schema)).toContain('z.string().placeholder(')
200+
})
201+
202+
it('should preserve .displayAs modifier', () => {
203+
// Arrange
204+
const schema = z.string().displayAs({ id: '', params: {} }).optional()
205+
206+
// Act & Assert
207+
assert(schema).toGenerateItself()
208+
expect(toTypescript(schema)).toContain('z.string().displayAs(')
209+
})
210+
})
155211
})
156212
describe.concurrent('number', () => {
157213
test('no checks', () => {

zui/src/z/types/basetype/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ export type RawCreateParams =
134134
invalid_type_error?: string
135135
required_error?: string
136136
description?: string
137-
[zuiKey]?: any
137+
[zuiKey]?: ZuiExtensionObject
138138
}
139139
| undefined
140140
export type ProcessedCreateParams = {
141141
errorMap?: ZodErrorMap
142142
description?: string
143-
[zuiKey]?: any
143+
[zuiKey]?: ZuiExtensionObject
144144
}
145145
export type SafeParseSuccess<Output> = {
146146
success: true

zui/src/z/types/string/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ export class ZodString extends ZodType<string, ZodStringDef> {
523523
checks: [],
524524
typeName: ZodFirstPartyTypeKind.ZodString,
525525
coerce: params?.coerce ?? false,
526-
...processCreateParams(params),
526+
...processCreateParams({ ...params, supportsExtensions: ['secret'] }),
527527
})
528528
}
529529

zui/src/z/types/utils/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,32 @@ export const getParsedType = (data: any): ZodParsedType => {
232232
return ZodParsedType.unknown
233233
}
234234
}
235-
export function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
235+
236+
export function processCreateParams(
237+
params: RawCreateParams & ({ supportsExtensions?: 'secret'[] } | undefined),
238+
): ProcessedCreateParams {
236239
if (!params) return {}
237240

238-
const { errorMap, invalid_type_error, required_error, description } = params
241+
const {
242+
errorMap,
243+
invalid_type_error,
244+
required_error,
245+
description,
246+
supportsExtensions,
247+
[zuiKey]: zuiExtensions,
248+
} = params
239249

240250
if (errorMap && (invalid_type_error || required_error)) {
241251
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`)
242252
}
243253

244-
if (errorMap) return { errorMap: errorMap, description, [zuiKey]: params?.[zuiKey] }
254+
const filteredZuiExtensions = zuiExtensions
255+
? Object.fromEntries(
256+
Object.entries(zuiExtensions).filter(([key]) => key !== 'secret' || supportsExtensions?.includes('secret')),
257+
)
258+
: undefined
259+
260+
if (errorMap) return { errorMap: errorMap, description, [zuiKey]: filteredZuiExtensions }
245261

246262
const customMap: ZodErrorMap = (iss, ctx) => {
247263
if (iss.code !== 'invalid_type') return { message: ctx.defaultError }
@@ -250,5 +266,5 @@ export function processCreateParams(params: RawCreateParams): ProcessedCreatePar
250266
}
251267
return { message: invalid_type_error ?? ctx.defaultError }
252268
}
253-
return { errorMap: customMap, description, [zuiKey]: params?.[zuiKey] }
269+
return { errorMap: customMap, description, [zuiKey]: filteredZuiExtensions }
254270
}

0 commit comments

Comments
 (0)