Skip to content

class Athena::Validator::Constraints::URL
inherits Athena::Validator::Constraint #

Validates that a value is a valid URL string. The underlying value is converted to a string via #to_s before being validated.

Note

As with most other constraints, nil and empty strings are considered valid values, in order to allow the value to be optional. If the value is required, consider combining this constraint with AVD::Constraints::NotBlank.

class Profile
  include AVD::Validatable

  def initialize(@avatar_url : String); end

  @[Assert::URL]
  property avatar_url : String
end

Configuration#

Optional Arguments#

protocols#

Type: Array(String) Default: ["http", "https"]

The protocols considered to be valid for the URL.

relative_protocol#

Type: Bool Default: false

If true the protocol is considered optional.

require_tld#

Type: Bool Default: true

The URL spec considers URLs like https://aaa or https://foobar to be valid However, this is most likely not desirable for most use cases. As such, this argument defaults to true and can be used to require that the host part of the URL will have to include a TLD (top-level domain name). E.g. https://example.com is valid but https://example is not.

Note

This constraint does NOT validate that the provided TLD is a valid one according to the official list.

tld_message#

Type: String Default: This URL is missing a top-level domain.

The message that will be shown if #require_tld? is true and the URL does not contain at least one TLD.

Placeholders#

The following placeholders can be used in this message:

  • {{ value }} - The current (invalid) value.

message#

Type: String Default: This value is not a valid URL.

The message that will be shown if the URL is not valid.

Placeholders#

The following placeholders can be used in this message:

  • {{ value }} - The current (invalid) value.

groups#

Type: Array(String) | String | Nil Default: nil

The validation groups this constraint belongs to. AVD::Constraint::DEFAULT_GROUP is assumed if nil.

payload#

Type: Hash(String, String)? Default: nil

Any arbitrary domain-specific data that should be stored with this constraint. The payload is not used by Athena::Validator, but its processing is completely up to you.

Constants#

INVALID_URL_ERROR = "e87ceba6-a896-4906-9957-b102045272ee"#

MISSING_TLD_ERROR = "4507f4cc-90fd-4616-989b-2166fc0d1083"#

Constructors#

.new(protocols : Array(String) = ["http", "https"], relative_protocol : Bool = false, require_tld : Bool = true, tld_message : String = "This URL is missing a top-level domain.", message : String = "This value is not a valid URL.", groups : Array(String) | String | Nil = nil, payload : Hash(String, String) | Nil = nil)#

Methods#

#protocols : Array(String)#

#relative_protocol? : Bool#

#require_tld? : Bool#

#tld_message : String#

#validated_by : AVD::ConstraintValidator.class#

Returns the AVD::ConstraintValidator.class that should handle validating self.