Skip to content

Commit b88b186

Browse files
committed
feat: add URLProtocolType enum for supported protocols
1 parent 37ed942 commit b88b186

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Shared/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,11 @@ export interface BatchConfigType {
615615
export interface scrollableInterface {
616616
autoBottomScroll: boolean
617617
}
618+
619+
export enum URLProtocolType {
620+
HTTP = 'http:',
621+
HTTPS = 'https:',
622+
SSH = 'ssh:',
623+
SMTP = 'smtp:',
624+
S3 = 's3:',
625+
}

src/Shared/validations.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { URLProtocolType } from './types'
18+
1719
export interface ValidationResponseType {
1820
isValid: boolean
1921
message?: string
@@ -197,6 +199,38 @@ export const validateURL = (url: string, allowBase64Url: boolean = true): Valida
197199
}
198200
}
199201

202+
export const validateProtocols = (
203+
url: string,
204+
protocols: URLProtocolType[],
205+
isRequired?: boolean,
206+
): ValidationResponseType => {
207+
if (isRequired && !url) {
208+
return {
209+
isValid: false,
210+
message: 'This field is required',
211+
}
212+
}
213+
214+
try {
215+
const { protocol } = new URL(url)
216+
if (protocol && protocols.includes(protocol as URLProtocolType)) {
217+
return {
218+
isValid: true,
219+
}
220+
}
221+
222+
return {
223+
isValid: false,
224+
message: `Invalid URL/protocol. Supported protocols are: ${protocols.join(', ')}`,
225+
}
226+
} catch (error) {
227+
return {
228+
isValid: false,
229+
message: `Invalid URL/protocol. Supported protocols are: ${protocols.join(', ')}`,
230+
}
231+
}
232+
}
233+
200234
export const validateIfImageExist = (url: string): Promise<ValidationResponseType> =>
201235
new Promise<ValidationResponseType>((resolve) => {
202236
const img = new Image()

0 commit comments

Comments
 (0)