Skip to content

Commit

Permalink
Merge pull request #262 from asteasolutions/feature/#256-more-string-…
Browse files Browse the repository at this point in the history
…formats

#256 added more string formats
  • Loading branch information
AGalabov authored Oct 4, 2024
2 parents 01e33a3 + c3148bd commit 8a7b324
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,25 @@ The list of all supported types as of now is:
- `ZodReadonly`
- `ZodRecord`
- `ZodString`
- adding `format` for `.datetime()`, `.uuid()`, `.email()` and `.url()` and `pattern` for `.regex()` is also supported
- adding `format` for:
- `.emoji()`
- `.cuid()`
- `.cuid2()`
- `.ulid()`
- `.ip()`
- `.datetime()`
- `.uuid()`
- `.email()`
- `.url()`
- adding `pattern` for `.regex()` is also supported


${'emoji'} | ${z.string().emoji()} | ${'emoji'}
${'cuid'} | ${z.string().cuid()} | ${'cuid'}
${'cuid2'} | ${z.string().cuid2()} | ${'cuid2'}
${'ulid'} | ${z.string().ulid()} | ${'ulid'}
${'ip'} | ${z.string().ip()} | ${'ip'}

- `ZodTuple`
- `ZodUnion`
- `ZodUnknown`
Expand Down
7 changes: 6 additions & 1 deletion spec/types/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ describe('string', () => {
});
});

it.each`
fit.each`
format | zodString | expected
${'emoji'} | ${z.string().emoji()} | ${'emoji'}
${'cuid'} | ${z.string().cuid()} | ${'cuid'}
${'cuid2'} | ${z.string().cuid2()} | ${'cuid2'}
${'ulid'} | ${z.string().ulid()} | ${'ulid'}
${'ip'} | ${z.string().ip()} | ${'ip'}
${'uuid'} | ${z.string().uuid()} | ${'uuid'}
${'email'} | ${z.string().email()} | ${'email'}
${'url'} | ${z.string().url()} | ${'uri'}
Expand Down
24 changes: 9 additions & 15 deletions src/transformers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@ export class StringTransformer {
* https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats
*/
private mapStringFormat(zodString: ZodString): string | undefined {
if (zodString.isUUID) {
return 'uuid';
}

if (zodString.isEmail) {
return 'email';
}

if (zodString.isURL) {
return 'uri';
}

if (zodString.isDatetime) {
return 'date-time';
}
if (zodString.isUUID) return 'uuid';
if (zodString.isEmail) return 'email';
if (zodString.isURL) return 'uri';
if (zodString.isDatetime) return 'date-time';
if (zodString.isCUID) return 'cuid';
if (zodString.isCUID2) return 'cuid2';
if (zodString.isULID) return 'ulid';
if (zodString.isIP) return 'ip';
if (zodString.isEmoji) return 'emoji';

return undefined;
}
Expand Down

0 comments on commit 8a7b324

Please sign in to comment.