Skip to content

Commit

Permalink
fix(zui): toTypescriptSchema transform supports checks for most types…
Browse files Browse the repository at this point in the history
… like string and number (#557)
  • Loading branch information
franklevasseur authored Feb 20, 2025
1 parent 8c6c35d commit 7dccc49
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 30 deletions.
2 changes: 1 addition & 1 deletion zui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/zui",
"version": "0.15.0",
"version": "0.16.0",
"description": "A fork of Zod with additional features",
"type": "module",
"source": "./src/index.ts",
Expand Down
4 changes: 2 additions & 2 deletions zui/src/transforms/common/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type BaseZuiJsonSchema<Def extends Partial<z.ZodDef> = {}> = util.Satisfies<
type _StringSchema = util.Satisfies<{ type: 'string' }, JSONSchema7> // TODO: support all string checks
type _NumberSchema = util.Satisfies<{ type: 'number' | 'integer' }, JSONSchema7> // TODO: support all number checks
type _BigIntSchema = util.Satisfies<{ type: 'integer' }, JSONSchema7> // TODO: support all bigint checks
type _BooleanSchema = util.Satisfies<{ type: 'boolean' }, JSONSchema7> // TODO: support all boolean checks
type _DateSchema = util.Satisfies<{ type: 'string'; format: 'date-time' }, JSONSchema7>
type _BooleanSchema = util.Satisfies<{ type: 'boolean' }, JSONSchema7>
type _DateSchema = util.Satisfies<{ type: 'string'; format: 'date-time' }, JSONSchema7> // TODO: support all date checks
type _NullSchema = util.Satisfies<{ type: 'null' }, JSONSchema7>
type _UndefinedSchema = util.Satisfies<{ not: true }, JSONSchema7>
type _NeverSchema = util.Satisfies<{ not: true }, JSONSchema7>
Expand Down
19 changes: 19 additions & 0 deletions zui/src/transforms/zui-to-typescript-schema/array-checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { primitiveToTypescriptValue as toTs } from '../common/utils'
import { ZodArrayDef } from '../../z/types/array'

export const generateArrayChecks = (def: ZodArrayDef): string => {
const checks: string[] = []
if (def.exactLength) {
const { value, message } = def.exactLength
checks.push(`.length(${toTs(value)}, ${toTs(message)})`)
}
if (def.minLength) {
const { value, message } = def.minLength
checks.push(`.min(${toTs(value)}, ${toTs(message)})`)
}
if (def.maxLength) {
const { value, message } = def.maxLength
checks.push(`.max(${toTs(value)}, ${toTs(message)})`)
}
return checks.join('')
}
25 changes: 25 additions & 0 deletions zui/src/transforms/zui-to-typescript-schema/bigint-checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { primitiveToTypescriptValue as toTs } from '../common/utils'
import { ZodBigIntCheck, ZodBigIntDef } from '../../z/types/bigint'
import { util } from '../../z'

export const generateBigIntChecks = (def: ZodBigIntDef): string => {
const checks = def.checks
if (checks.length === 0) {
return ''
}
return checks.map(_generateBigIntCheck).join('')
}

const _generateBigIntCheck = (check: ZodBigIntCheck): string => {
switch (check.kind) {
case 'min':
return `.min(${toTs(check.value)}, ${toTs(check.message)})`
case 'max':
return `.max(${toTs(check.value)}, ${toTs(check.message)})`
case 'multipleOf':
return `.multipleOf(${toTs(check.value)}, ${toTs(check.message)})`
default:
type _assertion = util.AssertNever<typeof check>
return ''
}
}
29 changes: 29 additions & 0 deletions zui/src/transforms/zui-to-typescript-schema/date-checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { primitiveToTypescriptValue as toTs } from '../common/utils'
import { ZodDateCheck, ZodDateDef } from '../../z/types/date'
import { util } from '../../z'

export const generateDateChecks = (def: ZodDateDef): string => {
const checks = def.checks
if (checks.length === 0) {
return ''
}
return checks.map(_generateDateCheck).join('')
}

const _generateDateCheck = (check: ZodDateCheck): string => {
switch (check.kind) {
case 'min':
const minDate = dateTs(check.value)
return `.min(${minDate}, ${toTs(check.message)})`
case 'max':
const maxDate = dateTs(check.value)
return `.max(${maxDate}, ${toTs(check.message)})`
default:
type _assertion = util.AssertNever<typeof check>
return ''
}
}

const dateTs = (d: number): string => {
return `new Date(${d})`
}
Loading

0 comments on commit 7dccc49

Please sign in to comment.