-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(zui): toTypescriptSchema transform supports checks for most types…
… like string and number (#557)
- Loading branch information
1 parent
8c6c35d
commit 7dccc49
Showing
10 changed files
with
446 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
zui/src/transforms/zui-to-typescript-schema/array-checks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
zui/src/transforms/zui-to-typescript-schema/bigint-checks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
zui/src/transforms/zui-to-typescript-schema/date-checks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})` | ||
} |
Oops, something went wrong.