Could you please provide a way to allow localhost as a valid url string? Something like validate.js's allowLocal flag: https://validatejs.org/#validators-url:
let validationSchema = Yup.object().shape({
url: Yup.string().url('Please enter a valid URL, including scheme', { allowLocal: true })
});
Thanks.
This would be incredibly useful, my application requires setting OAuth redirect URL's within a form that I'm validating. For development these URL's need to be http://localhost:8080 but this does not validate.
Any chance of this @jquense
Happy to take a PR for this if someone wants to send it out 馃憤
I don't have time right this second to make and test the modification and submit a PR, though I might in the next few days. In case I don't get to it, here's a regex you can use to replace the one rUrl one you're already using in string.js:
^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$
I tested it here: https://www.regexpal.com/?fam=104762
It's derived from https://gist.github.com/dperini/729294 with a few modifications:
dperini's regex is pretty widely regarded as the best one for matching URLs. The alternative is to use a real URL parsing lib instead of a regex, which is probably a good idea, but will increase the size of this lib.
I don't have time right this second to make and test the modification and submit a PR, though I might in the next few days. In case I don't get to it, here's a regex you can use to replace the one
rUrlone you're already using instring.js:^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$I tested it here: https://www.regexpal.com/?fam=104762
It's derived from https://gist.github.com/dperini/729294 with a few modifications:
- Scheme restriction removed. Any RFC valid scheme is OK now, not just http(s) and ftp.
- Private IP restriction removed. (i.e. 192.168.1.2... is OK now)
- TLD requirement removed. ('localhost' and other single-word hostnames OK now)
dperini's regex is pretty widely regarded as the best one for matching URLs. The alternative is to use a real URL parsing lib instead of a regex, which is probably a good idea, but will increase the size of this lib.
@jquense
I'm having the same problem trying to use yup.string().url() to validate an URL starting with "http://localhost:3000/". I've seen that this issue was closed but I'm interested in fixing the problem. Would you still accept a PR for a regex that supports that kind of URL?
Any updates on this?
This isn't just for localhost. Yup doesn't not recognize as valid anything without a TLD such as internal Docker hosts etc.
I'd be really interested in this as well.
Most helpful comment
@jquense
I'm having the same problem trying to use
yup.string().url()to validate an URL starting with "http://localhost:3000/". I've seen that this issue was closed but I'm interested in fixing the problem. Would you still accept a PR for a regex that supports that kind of URL?