Skip to content

Commit

Permalink
fix: parse arg typing to any
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongliang02 committed Nov 21, 2024
1 parent 2ab7449 commit 2d792ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
32 changes: 16 additions & 16 deletions etc/starter-kitty-validators.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,51 @@ export const createUrlSchema: (options?: UrlValidatorOptions) => ZodSchema<URL,
// @public
export interface EmailValidatorOptions {
domains?: {
domain: string;
includeSubdomains?: boolean;
}[];
domain: string
includeSubdomains?: boolean
}[]
}

// @public
export class OptionsError extends Error {
constructor(message: string);
constructor(message: string)
}

// @public
export interface PathValidatorOptions {
basePath: string;
basePath: string
}

// @public
export class RelUrlValidator extends UrlValidator {
constructor(origin: string | URL);
constructor(origin: string | URL)
}

// @public
export class UrlValidationError extends Error {
constructor(message: string);
constructor(message: string)
}

// @public
export class UrlValidator {
constructor(options?: UrlValidatorOptions);
parse(url: string, fallbackUrl: string | URL): URL;
constructor(options?: UrlValidatorOptions)
parse(url: any, fallbackUrl: string | URL): URL
// (undocumented)
parse(url: string): URL;
parse(url: any): URL
// (undocumented)
parse(url: string, fallbackUrl: undefined): URL;
parsePathname(url: string, fallbackUrl: string | URL): string;
parse(url: any, fallbackUrl: undefined): URL
parsePathname(url: any, fallbackUrl: string | URL): string
// (undocumented)
parsePathname(url: string): string;
parsePathname(url: any): string
// (undocumented)
parsePathname(url: string, fallbackUrl: undefined): string;
parsePathname(url: any, fallbackUrl: undefined): string
}

// @public
export interface UrlValidatorOptions {
baseOrigin?: string;
baseOrigin?: string
// Warning: (ae-forgotten-export) The symbol "UrlValidatorWhitelist" needs to be exported by the entry point index.d.ts
whitelist?: UrlValidatorWhitelist;
whitelist?: UrlValidatorWhitelist
}

```
2 changes: 1 addition & 1 deletion packages/validators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opengovsg/starter-kitty-validators",
"version": "1.2.9",
"version": "1.2.10",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
Expand Down
6 changes: 6 additions & 0 deletions packages/validators/src/__tests__/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('UrlValidator with default options', () => {
expect(() => validator.parse('javascript&colonalert(/xss/)').protocol).toThrow(UrlValidationError)
expect(() => validator.parse('javascript:alert(/xss/)')).toThrow(UrlValidationError)
})

it('should throw an error when given an invalid type', () => {
expect(() => validator.parse(123)).toThrow(UrlValidationError)
expect(() => validator.parse(undefined)).toThrow(UrlValidationError)
expect(() => validator.parse(['1', '2'])).toThrow(UrlValidationError)
})
})

describe('UrlValidator with custom protocol whitelist', () => {
Expand Down
22 changes: 12 additions & 10 deletions packages/validators/src/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { toSchema } from '@/url/schema'
* @public
*/
export class UrlValidator {
/* eslint-disable @typescript-eslint/no-explicit-any */
// parse functions perform type coercion, so input can be any
private schema

/**
Expand Down Expand Up @@ -46,8 +48,8 @@ export class UrlValidator {
*
* @internal
*/
#parse(url: string): URL {
const result = this.schema.safeParse(url)
#parse(url: any): URL {
const result = url instanceof URL ? this.schema.safeParse(url.href) : this.schema.safeParse(url)
if (result.success) {
return result.data
}
Expand All @@ -69,10 +71,10 @@ export class UrlValidator {
*
* @public
*/
parse(url: string, fallbackUrl: string | URL): URL
parse(url: string): URL
parse(url: string, fallbackUrl: undefined): URL
parse(url: string, fallbackUrl?: string | URL): URL {
parse(url: any, fallbackUrl: string | URL): URL
parse(url: any): URL
parse(url: any, fallbackUrl: undefined): URL
parse(url: any, fallbackUrl?: string | URL): URL {
try {
return this.#parse(url)
} catch (error) {
Expand All @@ -95,10 +97,10 @@ export class UrlValidator {
*
* @public
*/
parsePathname(url: string, fallbackUrl: string | URL): string
parsePathname(url: string): string
parsePathname(url: string, fallbackUrl: undefined): string
parsePathname(url: string, fallbackUrl?: string | URL): string {
parsePathname(url: any, fallbackUrl: string | URL): string
parsePathname(url: any): string
parsePathname(url: any, fallbackUrl: undefined): string
parsePathname(url: any, fallbackUrl?: string | URL): string {
const parsedUrl = fallbackUrl ? this.parse(url, fallbackUrl) : this.parse(url)
if (parsedUrl instanceof URL) return parsedUrl.pathname
return parsedUrl
Expand Down

0 comments on commit 2d792ff

Please sign in to comment.