Skip to main content

WellKnown

Enum WellKnown 

Source
pub enum WellKnown {
Show 19 variants Email(bool), Hostname(bool), Ip(bool), Ipv4(bool), Ipv6(bool), Uri(bool), UriRef(bool), Address(bool), Uuid(bool), Tuuid(bool), IpWithPrefixlen(bool), Ipv4WithPrefixlen(bool), Ipv6WithPrefixlen(bool), IpPrefix(bool), Ipv4Prefix(bool), Ipv6Prefix(bool), HostAndPort(bool), Ulid(bool), WellKnownRegex(i32),
}
Expand description

WellKnown rules provide advanced rules against common string patterns.

Variants§

§

Email(bool)

email specifies that the field value must be a valid email address, for example “foo@example.com”.

Conforms to the definition for a valid email address from the HTML standard. Note that this standard willfully deviates from RFC 5322, which allows many unexpected forms of email addresses and will easily match a typographical error.

If the field value isn’t a valid email address, an error message will be generated.

message MyString {
   // value must be a valid email address
   string value = 1 \[(buf.validate.field).string.email = true\];
}
§

Hostname(bool)

hostname specifies that the field value must be a valid hostname, for example “foo.example.com”.

A valid hostname follows the rules below:

  • The name consists of one or more labels, separated by a dot (“.”).
  • Each label can be 1 to 63 alphanumeric characters.
  • A label can contain hyphens (“-”), but must not start or end with a hyphen.
  • The right-most label must not be digits only.
  • The name can have a trailing dot—for example, “foo.example.com.”.
  • The name can be 253 characters at most, excluding the optional trailing dot.

If the field value isn’t a valid hostname, an error message will be generated.

message MyString {
   // value must be a valid hostname
   string value = 1 \[(buf.validate.field).string.hostname = true\];
}
§

Ip(bool)

ip specifies that the field value must be a valid IP (v4 or v6) address.

IPv4 addresses are expected in the dotted decimal format—for example, “192.168.5.21”. IPv6 addresses are expected in their text representation—for example, “::1”, or “2001:0DB8:ABCD:0012::0”.

Both formats are well-defined in the internet standard RFC 3986. Zone identifiers for IPv6 addresses (for example, “fe80::a%en1”) are supported.

If the field value isn’t a valid IP address, an error message will be generated.

message MyString {
   // value must be a valid IP address
   string value = 1 \[(buf.validate.field).string.ip = true\];
}
§

Ipv4(bool)

ipv4 specifies that the field value must be a valid IPv4 address—for example “192.168.5.21”. If the field value isn’t a valid IPv4 address, an error message will be generated.

message MyString {
   // value must be a valid IPv4 address
   string value = 1 \[(buf.validate.field).string.ipv4 = true\];
}
§

Ipv6(bool)

ipv6 specifies that the field value must be a valid IPv6 address—for example “::1”, or “d7a:115c:a1e0:ab12:4843:cd96:626b:430b”. If the field value is not a valid IPv6 address, an error message will be generated.

message MyString {
   // value must be a valid IPv6 address
   string value = 1 \[(buf.validate.field).string.ipv6 = true\];
}
§

Uri(bool)

uri specifies that the field value must be a valid URI, for example “https://example.com/foo/bar?baz=quux#frag".

URI is defined in the internet standard RFC 3986. Zone Identifiers in IPv6 address literals are supported (RFC 6874).

If the field value isn’t a valid URI, an error message will be generated.

message MyString {
   // value must be a valid URI
   string value = 1 \[(buf.validate.field).string.uri = true\];
}
§

UriRef(bool)

uri_ref specifies that the field value must be a valid URI Reference—either a URI such as “https://example.com/foo/bar?baz=quux#frag", or a Relative Reference such as “./foo/bar?query”.

URI, URI Reference, and Relative Reference are defined in the internet standard RFC 3986. Zone Identifiers in IPv6 address literals are supported (RFC 6874).

If the field value isn’t a valid URI Reference, an error message will be generated.

message MyString {
   // value must be a valid URI Reference
   string value = 1 \[(buf.validate.field).string.uri_ref = true\];
}
§

Address(bool)

address specifies that the field value must be either a valid hostname (for example, “example.com”), or a valid IP (v4 or v6) address (for example, “192.168.0.1”, or “::1”). If the field value isn’t a valid hostname or IP, an error message will be generated.

message MyString {
   // value must be a valid hostname, or ip address
   string value = 1 \[(buf.validate.field).string.address = true\];
}
§

Uuid(bool)

uuid specifies that the field value must be a valid UUID as defined by RFC 4122. If the field value isn’t a valid UUID, an error message will be generated.

message MyString {
   // value must be a valid UUID
   string value = 1 \[(buf.validate.field).string.uuid = true\];
}
§

Tuuid(bool)

tuuid (trimmed UUID) specifies that the field value must be a valid UUID as defined by RFC 4122 with all dashes omitted. If the field value isn’t a valid UUID without dashes, an error message will be generated.

message MyString {
   // value must be a valid trimmed UUID
   string value = 1 \[(buf.validate.field).string.tuuid = true\];
}
§

IpWithPrefixlen(bool)

ip_with_prefixlen specifies that the field value must be a valid IP (v4 or v6) address with prefix length—for example, “192.168.5.21/16” or “2001:0DB8:ABCD:0012::F1/64”. If the field value isn’t a valid IP with prefix length, an error message will be generated.

message MyString {
   // value must be a valid IP with prefix length
    string value = 1 \[(buf.validate.field).string.ip_with_prefixlen = true\];
}
§

Ipv4WithPrefixlen(bool)

ipv4_with_prefixlen specifies that the field value must be a valid IPv4 address with prefix length—for example, “192.168.5.21/16”. If the field value isn’t a valid IPv4 address with prefix length, an error message will be generated.

message MyString {
   // value must be a valid IPv4 address with prefix length
    string value = 1 \[(buf.validate.field).string.ipv4_with_prefixlen = true\];
}
§

Ipv6WithPrefixlen(bool)

ipv6_with_prefixlen specifies that the field value must be a valid IPv6 address with prefix length—for example, “2001:0DB8:ABCD:0012::F1/64”. If the field value is not a valid IPv6 address with prefix length, an error message will be generated.

message MyString {
   // value must be a valid IPv6 address prefix length
    string value = 1 \[(buf.validate.field).string.ipv6_with_prefixlen = true\];
}
§

IpPrefix(bool)

ip_prefix specifies that the field value must be a valid IP (v4 or v6) prefix—for example, “192.168.0.0/16” or “2001:0DB8:ABCD:0012::0/64”.

The prefix must have all zeros for the unmasked bits. For example, “2001:0DB8:ABCD:0012::0/64” designates the left-most 64 bits for the prefix, and the remaining 64 bits must be zero.

If the field value isn’t a valid IP prefix, an error message will be generated.

message MyString {
   // value must be a valid IP prefix
    string value = 1 \[(buf.validate.field).string.ip_prefix = true\];
}
§

Ipv4Prefix(bool)

ipv4_prefix specifies that the field value must be a valid IPv4 prefix, for example “192.168.0.0/16”.

The prefix must have all zeros for the unmasked bits. For example, “192.168.0.0/16” designates the left-most 16 bits for the prefix, and the remaining 16 bits must be zero.

If the field value isn’t a valid IPv4 prefix, an error message will be generated.

message MyString {
   // value must be a valid IPv4 prefix
    string value = 1 \[(buf.validate.field).string.ipv4_prefix = true\];
}
§

Ipv6Prefix(bool)

ipv6_prefix specifies that the field value must be a valid IPv6 prefix—for example, “2001:0DB8:ABCD:0012::0/64”.

The prefix must have all zeros for the unmasked bits. For example, “2001:0DB8:ABCD:0012::0/64” designates the left-most 64 bits for the prefix, and the remaining 64 bits must be zero.

If the field value is not a valid IPv6 prefix, an error message will be generated.

message MyString {
   // value must be a valid IPv6 prefix
    string value = 1 \[(buf.validate.field).string.ipv6_prefix = true\];
}
§

HostAndPort(bool)

host_and_port specifies that the field value must be valid host/port pair—for example, “example.com:8080”.

The host can be one of:

  • An IPv4 address in dotted decimal format—for example, “192.168.5.21”.
  • An IPv6 address enclosed in square brackets—for example, “[2001:0DB8:ABCD:0012::F1]”.
  • A hostname—for example, “example.com”.

The port is separated by a colon. It must be non-empty, with a decimal number in the range of 0-65535, inclusive.

§

Ulid(bool)

ulid specifies that the field value must be a valid ULID (Universally Unique Lexicographically Sortable Identifier) as defined by the ULID specification. If the field value isn’t a valid ULID, an error message will be generated.

message MyString {
   // value must be a valid ULID
   string value = 1 \[(buf.validate.field).string.ulid = true\];
}
§

WellKnownRegex(i32)

well_known_regex specifies a common well-known pattern defined as a regex. If the field value doesn’t match the well-known regex, an error message will be generated.

message MyString {
   // value must be a valid HTTP header value
   string value = 1 \[(buf.validate.field).string.well_known_regex = KNOWN_REGEX_HTTP_HEADER_VALUE\];
}
§KnownRegex

well_known_regex contains some well-known patterns.

NameNumberDescription
KNOWN_REGEX_UNSPECIFIED0
KNOWN_REGEX_HTTP_HEADER_NAME1HTTP header name as defined by RFC 7230
KNOWN_REGEX_HTTP_HEADER_VALUE2HTTP header value as defined by RFC 7230

Implementations§

Source§

impl WellKnown

Source

pub fn encode(&self, buf: &mut impl BufMut)

Encodes the message to a buffer.

Source

pub fn merge( field: &mut Option<WellKnown>, tag: u32, wire_type: WireType, buf: &mut impl Buf, ctx: DecodeContext, ) -> Result<(), DecodeError>

Decodes an instance of the message from a buffer, and merges it into self.

Source

pub fn encoded_len(&self) -> usize

Returns the encoded length of the message without a length delimiter.

Trait Implementations§

Source§

impl Clone for WellKnown

Source§

fn clone(&self) -> WellKnown

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 Copy for WellKnown

Source§

impl Debug for WellKnown

Source§

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

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

impl Eq for WellKnown

Source§

impl Hash for WellKnown

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for WellKnown

Source§

fn eq(&self, other: &WellKnown) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for WellKnown

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<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, 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.