url
The string is a URL with an allowed protocol. Only http and https by default.
Signature
ts
function url(msg?: Message, protocols?: string[]): Action<string>Parameters
| Parameter | Type | Description |
|---|---|---|
msg | Message | Custom error message |
protocols | string[] | Allowed protocols. Default ['http', 'https'] |
Error codes
| Code | When | Default message |
|---|---|---|
url | Not a URL or another protocol | Invalid URL |
You can change the texts: see Messages & i18n.
Notes
javascript: and data: links won't pass — the value is safe to put into href. A domain without a protocol (example.com) is an error too: add the protocol with preprocess or check with startsWith.
Example
ts
const Profile = object({
website: optional(pipe(string(), trim(), url())),
ftp: optional(pipe(string(), url(undefined, ['ftp', 'sftp']))),
});