pub struct Valid<T>(/* private fields */);Expand description
Validated value
Valid<T> wraps a value T that has been validated using Validate trait.
Valid<T> provides only immutable access to T. For instance, if you want to change content of T, you
need to deconstruct it, do necessary modifications, and then validate it again.
§Transitive “valideness” through AsRef
Valid<T> assumes that if T implements AsRef<K> and K can be validated (i.e. K implements Validate),
then K has been validated when T was validated. Thus, if you have value of type Valid<T>, you can obtain
&Valid<K> via AsRef trait.
Example of transitive valideness is demostrated below:
use key_share::{Validate, Valid};
pub type CoreKeyShare = Valid<DirtyCoreKeyShare>;
pub type KeyInfo = Valid<DirtyKeyInfo>;
pub struct DirtyCoreKeyShare {
i: u16,
key_info: DirtyKeyInfo,
x: SecretScalar,
}
pub struct DirtyKeyInfo { /* ... */ }
// Key info can be validated separately
impl Validate for DirtyKeyInfo {
type Error = InvalidKeyShare;
fn is_valid(&self) -> Result<(), Self::Error> {
// ...
}
}
// CoreKeyShare can be validated as well
impl Validate for DirtyCoreKeyShare {
type Error = InvalidKeyShare;
fn is_valid(&self) -> Result<(), Self::Error> {
// Since `key_info` is part of key share, it **must be** validated when
// the key share is validated
self.key_info.is_valid();
// ...
}
}
impl AsRef<DirtyKeyInfo> for DirtyCoreKeyShare {
fn as_ref(&self) -> &DirtyKeyInfo {
&self.key_info
}
}
let key_share: CoreKeyShare = DirtyCoreKeyShare { i, key_info, x }.validate()?;
// Since `key_share` is validated, and it contains `key_info`, we can obtain a `&KeyInfo`.
// `Valid<T>` trusts that `<DirtyCoreKeyShare as Validate>::is_valid` has validated `key_info`.
let key_info: &KeyInfo = key_share.as_ref();This mechanism allow to improve performance by not validating what’s already been validated. However, incorrect
implementation of Validate trait may lead to obtaining Valid<K> that’s actually invalid. It may, in return,
lead to runtime panic and/or compromised security of the application. Make sure that all implementations of
Validate trait are correct and aligned with AsRef implementations.
Implementations§
Source§impl<T> Valid<T>where
T: Validate,
impl<T> Valid<T>where
T: Validate,
Sourcepub fn validate(
value: T,
) -> Result<Self, ValidateError<T, <T as Validate>::Error>>
pub fn validate( value: T, ) -> Result<Self, ValidateError<T, <T as Validate>::Error>>
Validates the value
If value is valid, returns Ok(validated_value) wrapped into type guard Valid<T>, otherwise returns
Err(err) containing the error and the invalid value.
Sourcepub fn validate_ref(
value: &T,
) -> Result<&Self, ValidateError<&T, <T as Validate>::Error>>
pub fn validate_ref( value: &T, ) -> Result<&Self, ValidateError<&T, <T as Validate>::Error>>
Validates a reference to value &T returning &Valid<T> if it’s valid
Sourcepub fn from_parts<Parts>(
parts: Parts,
) -> Result<Self, ValidateError<Parts, <T as Validate>::Error>>where
T: ValidateFromParts<Parts>,
pub fn from_parts<Parts>(
parts: Parts,
) -> Result<Self, ValidateError<Parts, <T as Validate>::Error>>where
T: ValidateFromParts<Parts>,
Constructs and validates value from parts
Refer to ValidateFromParts trait documentation
Source§impl<T> Valid<T>
impl<T> Valid<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Returns wraped validated value
Trait Implementations§
Source§fn as_ref(&self) -> &CoreKeyShare<E>
fn as_ref(&self) -> &CoreKeyShare<E>
Source§impl<'de, T> Deserialize<'de> for Valid<T>
Available on crate feature serde only.
impl<'de, T> Deserialize<'de> for Valid<T>
serde only.