Validate string is URL
What would be the easiest way to validate that the url in here is an actual valid url?
19 Replies
Use a morph that checks the string length and regex it I guess
Depending on what you want to consider a valid URL
I mean seeing if it would parse in
new URL()
If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown.Since an error is thrown when it's invalid, you can just try/catch
yea but how can I do that inside of the
type({...})
? Or is that not possiblesomething like this I guess
You could ofc have the function in-line if you wanted
@Bas950 ^
hmmm, okay thanks.
Never used those morphs before, but that should work I guess, thanks.
Might be nice if @ssalbdivad can add url as a thing itself just like dates etc.
Yeah, it might even be planned
For now I will use:
Looks good 👍
Awesome, thanks for helping @TizzySaurus. I'll probably add
url
as just validating the string format, then parse.url
would be validate it and then transform? Something like thatSounds good!
isnt this narrow?
Also I doubt you would want
Yeah you're right haha I didn't actually double check 😬
This would convert it to a boolean
At least that would be clear when you tried to use the type though because your output data would be a boolean
oh yea thanks, forgot, you just have to check if the protocol is
http:
or https:
And I don't think you need minLength then
Don't bamboozle people with beta syntax 🤣
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Ahh nice catch, thank you! I will update this for the next release 😊
I'm going to not worry about keys including
:
for now 😅