Skip to content

Commit

Permalink
refactor: path exports
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 authored Sep 25, 2024
1 parent db41f04 commit a4b48e3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ build
dist
cache
temp/
tsdoc-metadata.json

# Debug
npm-debug.log*
Expand Down
46 changes: 19 additions & 27 deletions apps/docs/examples/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ npm i --save @opengovsg/starter-kitty-validators
## Path Validation

```javascript
import { createPathSchema } from '@opengovsg/starter-kitty-validators'
import { createPathSchema } from '@opengovsg/starter-kitty-validators/path'

const pathSchema = createPathSchema({
basePath: '/app/content',
Expand All @@ -21,15 +21,15 @@ const contentSubmissionSchema = z.object({
content: z.string(),
})

type ContentSubmission = z.infer<typeof contentSchema>
type ContentSubmission = z.infer<typeof contentSubmissionSchema>
```

`fullPermalink`, when resolved relative to the working directory of the Node process, must lie within `/app/content`.

## Email Validation

```javascript
import { createEmailSchema } from '@opengovsg/starter-kitty-validators'
import { createEmailSchema } from '@opengovsg/starter-kitty-validators/email'

const emailSchema = createEmailSchema({
domains: [{ domain: 'gov.sg', includeSubdomains: true }],
Expand All @@ -47,8 +47,10 @@ type FormValues = z.infer<typeof formSchema>

## URL Validation

Validating a post-login redirect URL provided in a query parameter:

```javascript
import { UrlValidator } from '@opengovsg/starter-kitty-validators'
import { UrlValidator } from '@opengovsg/starter-kitty-validators/url'

const validator = new UrlValidator({
whitelist: {
Expand All @@ -58,8 +60,6 @@ const validator = new UrlValidator({
})
```

Validating a post-login redirect URL provided in a query parameter:

```javascript
try {
router.push(validator.parse(redirectUrl))
Expand All @@ -68,33 +68,25 @@ try {
}
```

Consider using the validator as part of a Zod schema to validate the URL and fall back to a default URL if the URL is invalid.
Using the validator as part of a Zod schema to validate the URL and fall back to a default URL if the URL is invalid:

```javascript
const baseUrl = getBaseUrl()
import { createUrlSchema } from '@opengovsg/starter-kitty-validators/url'

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))
```
32 changes: 28 additions & 4 deletions packages/validators/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
{
"name": "@opengovsg/starter-kitty-validators",
"version": "1.2.4",
"version": "1.2.5",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"exports": {
"./url": {
"types": "./dist/url/index.d.ts",
"default": "./dist/url/index.js"
},
"./path": {
"types": "./dist/path/index.d.ts",
"default": "./dist/path/index.js"
},
"./email": {
"types": "./dist/email/index.d.ts",
"default": "./dist/email/index.js"
}
},
"typesVersions": {
"*": {
"url": [
"./dist/url/index.d.ts"
],
"path": [
"./dist/path/index.d.ts"
],
"email": [
"./dist/email/index.d.ts"
]
}
},
"scripts": {
"build": "tsc && tsc-alias",
"build:report": "api-extractor run --local --verbose",
Expand Down
2 changes: 2 additions & 0 deletions packages/validators/src/email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export const createEmailSchema = (options: EmailValidatorOptions = {}): ZodSchem
}
throw new OptionsError(fromError(result.error).toString())
}

export type { EmailValidatorOptions } from '@/email/options'
4 changes: 0 additions & 4 deletions packages/validators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@

export type * from '@/common/errors'
export * from '@/email'
export type { EmailValidatorOptions } from '@/email/options'
export * from '@/path'
export type { PathValidatorOptions } from '@/path/options'
export * from '@/url'
export type * from '@/url/errors'
export type { UrlValidatorOptions } from '@/url/options'
2 changes: 2 additions & 0 deletions packages/validators/src/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export const createPathSchema = (options: PathValidatorOptions): ZodSchema<strin
}
throw new OptionsError(fromError(result.error).toString())
}

export type { PathValidatorOptions } from '@/path/options'
3 changes: 3 additions & 0 deletions packages/validators/src/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ export const createUrlSchema = (options: UrlValidatorOptions = defaultOptions):
}
throw new OptionsError(fromError(result.error).toString())
}

export type * from '@/url/errors'
export type { UrlValidatorOptions } from '@/url/options'

0 comments on commit a4b48e3

Please sign in to comment.