url

Validates the value to be formatted as a valid URL string.

import { schema, rules } from '@ioc:Adonis/Core/Validator'
{
website: schema.string({}, [
rules.url()
])
}

Along with the format validation, you can also enforce the url to be from a certain domain. For example:

{
twitterProfile: schema.string({}, [
rules.url({
// Only twitter.com urls are allowed
allowedHosts: ['twitter.com']
})
])
}

The inverse of allowedHosts is the bannedHosts.

{
website: schema.string({}, [
rules.url({
bannedHosts: [
'acme.com',
'example.com'
]
})
])
}

Validation options

Following is the list of options for validate a URL string

{
website: schema.string({}, [
rules.url({
protocols: ['http', 'https', 'ftp'],
requireTld: true,
requireProtocol: false,
requireHost: true,
allowedHosts: [],
bannedHosts: [],
validateLength: false
})
])
}
OptionDescription
protocolAn array of allowed protocols. Defining protocols will implicitly set the requireProtocol option to true.
requireTldEnsure the tld is present in the URL. Defaults to true
requireProtocolEnsure the URL has protocol defined. Defaults to false
requireHostEnsure the URL has the host defined. Defaults to true
allowedHostsAn array of allowed hosts. URLs outside the defined hosts will fail the validation.
bannedHostsAn array of banned hosts. URLs matching the defined hosts will fail the validation.
validateLengthValidate the length of the URL to be under or equal to 2083 charcters. Defaults to true.

Normalization options

Following is the list of available options to normalize the URL value post validation.

{
website: schema.string({}, [
rules.url({
ensureProtocol: 'https',
stripWWW: true,
})
])
}
OptionDescription
ensureProtocolThe property ensures that the URL post validation has https protocol
stripWWWStrips the www from the URL