Skip to main content

EmailOptions

Struct EmailOptions 

Source
pub struct EmailOptions { /* private fields */ }
Expand description

Configuration for email validation options.

This allows customization of email format validation behavior beyond the default JSON Schema spec requirements. For example, you can configure stricter validation that rejects addresses like “missing@domain” which are technically valid per the spec but may not be desirable in real-world usage.

§Example

use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(
        EmailOptions::default()
            .with_required_tld()
            .without_display_text()
    )
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Stricter validation rejects addresses without TLD
assert!(!validator.is_valid(&json!("user@localhost")));
// And rejects display text
assert!(!validator.is_valid(&json!("Name <user@example.com>")));
// But accepts valid emails with TLD
assert!(validator.is_valid(&json!("user@example.com")));

Implementations§

Source§

impl EmailOptions

Source

pub const fn with_minimum_sub_domains(self, min: usize) -> Self

Set the minimum number of domain segments that must exist to parse successfully.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().with_minimum_sub_domains(3))
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Requires 3+ domain segments
assert!(!validator.is_valid(&json!("user@example.com")));
assert!(validator.is_valid(&json!("user@sub.example.com")));
Source

pub const fn with_no_minimum_sub_domains(self) -> Self

Set the minimum number of domain segments to zero, allowing single-segment domains like “localhost”.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().with_no_minimum_sub_domains())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Allows single-segment domains
assert!(validator.is_valid(&json!("user@localhost")));
assert!(validator.is_valid(&json!("user@example.com")));
Source

pub const fn with_required_tld(self) -> Self

Require a domain name with a top-level domain (TLD).

This sets the minimum number of domain segments to two, effectively requiring addresses like “user@example.com” instead of “user@localhost”.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().with_required_tld())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Requires TLD
assert!(!validator.is_valid(&json!("user@localhost")));
assert!(validator.is_valid(&json!("user@example.com")));
Source

pub const fn with_domain_literal(self) -> Self

Allow domain literals (e.g., email@[127.0.0.1] or email@[IPv6:2001:db8::1]).

This is enabled by default.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().with_domain_literal())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Domain literals are allowed
assert!(validator.is_valid(&json!("email@[127.0.0.1]")));
assert!(validator.is_valid(&json!("user@example.com")));
Source

pub const fn without_domain_literal(self) -> Self

Disallow domain literals, requiring regular domain names only.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().without_domain_literal())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Domain literals are rejected
assert!(!validator.is_valid(&json!("email@[127.0.0.1]")));
assert!(validator.is_valid(&json!("user@example.com")));
Source

pub const fn with_display_text(self) -> Self

Allow display text (e.g., Simon <simon@example.com>).

This is enabled by default.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().with_display_text())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Display text is allowed
assert!(validator.is_valid(&json!("Simon <simon@example.com>")));
assert!(validator.is_valid(&json!("simon@example.com")));
Source

pub const fn without_display_text(self) -> Self

Disallow display text, requiring plain email addresses only.

§Example
use jsonschema::EmailOptions;
use serde_json::json;

let schema = json!({"format": "email", "type": "string"});
let validator = jsonschema::options()
    .with_email_options(EmailOptions::default().without_display_text())
    .should_validate_formats(true)
    .build(&schema)
    .expect("A valid schema");

// Display text is rejected
assert!(!validator.is_valid(&json!("Simon <simon@example.com>")));
assert!(validator.is_valid(&json!("simon@example.com")));

Trait Implementations§

Source§

impl Clone for EmailOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EmailOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EmailOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Options> for EmailOptions

Source§

fn from(options: EmailAddressOptions) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Inspect for T
where T: Debug,

Source§

fn inspect(&self) -> String

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more