pub struct UrlValidator { /* private fields */ }Expand description
Validates that a string value is a well-formed HTTP or HTTPS URL.
The validator checks:
- Scheme must be
httporhttps - Host must be non-empty and must not start or end with a hyphen
- Optional port, path, query string, and fragment are allowed
§Examples
use reinhardt_forms::validators::UrlValidator;
let validator = UrlValidator::new();
assert!(validator.validate("https://example.com").is_ok());
assert!(validator.validate("http://localhost:8080/path").is_ok());
assert!(validator.validate("ftp://example.com").is_err());
assert!(validator.validate("not-a-url").is_err());Implementations§
Source§impl UrlValidator
impl UrlValidator
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new UrlValidator with default settings.
§Examples
use reinhardt_forms::validators::UrlValidator;
let validator = UrlValidator::new();
assert!(validator.validate("https://example.com").is_ok());Sourcepub fn with_message(self, message: impl Into<String>) -> Self
pub fn with_message(self, message: impl Into<String>) -> Self
Sets a custom error message returned on validation failure.
§Examples
use reinhardt_forms::validators::UrlValidator;
let validator = UrlValidator::new().with_message("Please enter a valid website URL");
assert!(validator.validate("bad").is_err());Sourcepub fn validate(&self, value: &str) -> FieldResult<()>
pub fn validate(&self, value: &str) -> FieldResult<()>
Validates the given string slice as a URL.
Returns Ok(()) when the URL is valid, or a FieldError::Validation
containing an error message when it is not.
§Examples
use reinhardt_forms::validators::UrlValidator;
let validator = UrlValidator::new();
assert!(validator.validate("https://www.example.com/path?q=1").is_ok());
assert!(validator.validate("ftp://example.com").is_err());Trait Implementations§
Source§impl Clone for UrlValidator
impl Clone for UrlValidator
Source§fn clone(&self) -> UrlValidator
fn clone(&self) -> UrlValidator
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for UrlValidator
impl Debug for UrlValidator
Auto Trait Implementations§
impl Freeze for UrlValidator
impl RefUnwindSafe for UrlValidator
impl Send for UrlValidator
impl Sync for UrlValidator
impl Unpin for UrlValidator
impl UnsafeUnpin for UrlValidator
impl UnwindSafe for UrlValidator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more