-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: url and email validation (#335)
* refactor: starter-kitty usage * fix: install starter-kitty from public registry
- Loading branch information
Showing
4 changed files
with
41 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,21 @@ | ||
import { z } from 'zod' | ||
import { HOME } from '~/lib/routes' | ||
import { UrlValidator } from '@opengovsg/starter-kitty-validators' | ||
import { createUrlSchema } from '@opengovsg/starter-kitty-validators/url' | ||
import { getBaseUrl } from '~/utils/getBaseUrl' | ||
|
||
const baseUrl = getBaseUrl() | ||
|
||
const validator = new UrlValidator({ | ||
baseOrigin: new URL(baseUrl).origin, | ||
whitelist: { | ||
protocols: ['http', 'https'], | ||
hosts: [new URL(baseUrl).host], | ||
}, | ||
}) | ||
const baseUrl = new URL(getBaseUrl()) | ||
|
||
export const callbackUrlSchema = z | ||
.string() | ||
.optional() | ||
.default(HOME) | ||
.transform((url, ctx) => { | ||
try { | ||
return validator.parse(url) | ||
} catch (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: (error as Error).message, | ||
}) | ||
return z.NEVER | ||
} | ||
}) | ||
.catch(new URL(HOME, baseUrl)) | ||
.pipe( | ||
createUrlSchema({ | ||
baseOrigin: baseUrl.origin, | ||
whitelist: { | ||
protocols: ['http', 'https'], | ||
hosts: [baseUrl.host], | ||
}, | ||
}), | ||
) | ||
.catch(new URL(HOME, baseUrl.origin)) |
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