pub struct ZInteger<T>where
T: IntType,{ /* private fields */ }Expand description
Schema for a sized integer (i8..u64, isize, usize). Created via
vld::i8() … vld::u64(); ZInt
(vld::number().int()) is ZInteger<i64>.
Integers are read exactly (no f64 round-trip; u64 values above
i64::MAX included). Floats with a fraction fail with not_int; values
outside the type’s range fail with too_small / too_big (origin
number) naming the type’s bound.
JSON Schema: {"type": "integer", "format": "int8" … "uint64"} with
minimum / maximum from the type range intersected with the user bounds.
§Example
use vld::prelude::*;
let port: u16 = vld::u16().min(1).parse("8080").unwrap();
assert_eq!(port, 8080);
assert!(vld::u16().parse("70000").is_err()); // above u16::MAX
assert!(vld::u8().parse("-1").is_err());
assert!(vld::i32().parse("1.5").is_err());
assert_eq!(vld::u64().parse("18446744073709551615").unwrap(), u64::MAX);Implementations§
Source§impl<T> ZInteger<T>where
T: IntType,
impl<T> ZInteger<T>where
T: IntType,
Sourcepub fn type_error(self, msg: impl Into<String>) -> ZInteger<T>
pub fn type_error(self, msg: impl Into<String>) -> ZInteger<T>
Set a custom error message for type mismatch (non-number input).
Sourcepub fn int_error(self, msg: impl Into<String>) -> ZInteger<T>
pub fn int_error(self, msg: impl Into<String>) -> ZInteger<T>
Set a custom error message for when the number is not an integer.
§Example
use vld::prelude::*;
let schema = vld::number().int().int_error("Whole numbers only!");
let err = schema.parse("3.5").unwrap_err();
assert!(err.issues[0].message.contains("Whole numbers only!"));Sourcepub fn with_messages<F>(self, f: F) -> ZInteger<T>
pub fn with_messages<F>(self, f: F) -> ZInteger<T>
Override error messages in bulk by check key.
Same keys as ZNumber::with_messages, plus "not_int" for the
integer check itself.
Sourcepub fn non_negative(self) -> ZInteger<T>
pub fn non_negative(self) -> ZInteger<T>
Must be non-negative (>= 0).
Sourcepub fn non_positive(self) -> ZInteger<T>
pub fn non_positive(self) -> ZInteger<T>
Must be non-positive (<= 0).
Sourcepub fn multiple_of(self, val: T) -> ZInteger<T>
pub fn multiple_of(self, val: T) -> ZInteger<T>
Must be a multiple of the given value (0 never matches).
Trait Implementations§
Source§impl<T> VldSchema for ZInteger<T>where
T: IntType,
impl<T> VldSchema for ZInteger<T>where
T: IntType,
Source§fn parse_value(&self, value: &Value) -> Result<T, VldError>
fn parse_value(&self, value: &Value) -> Result<T, VldError>
serde_json::Value.Source§fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
fn parse<I>(&self, input: &I) -> Result<Self::Output, VldError>
serde_json::Value, etc.)Source§fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError>where
T: Serialize,
fn validate<T>(&self, value: &T) -> Result<Self::Output, VldError>where
T: Serialize,
Source§fn is_valid<T>(&self, value: &T) -> boolwhere
T: Serialize,
fn is_valid<T>(&self, value: &T) -> boolwhere
T: Serialize,
Source§fn optional(self) -> ZOptional<Self>
fn optional(self) -> ZOptional<Self>
None.Source§fn with_default(self, value: Self::Output) -> ZDefault<Self>
fn with_default(self, value: Self::Output) -> ZDefault<Self>
Source§fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
fn refine<F>(self, check: F, message: &str) -> ZRefine<Self, F>
Source§fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
fn transform<F, U>(self, f: F) -> ZTransform<Self, F, U>
Source§fn catch(self, fallback: Self::Output) -> ZCatch<Self>
fn catch(self, fallback: Self::Output) -> ZCatch<Self>
Source§fn pipe<S>(self, next: S) -> ZPipe<Self, S>
fn pipe<S>(self, next: S) -> ZPipe<Self, S>
Source§fn describe(self, description: &str) -> ZDescribe<Self>
fn describe(self, description: &str) -> ZDescribe<Self>
Source§fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
fn super_refine<F>(self, check: F) -> ZSuperRefine<Self, F>
Source§fn or<B>(self, other: B) -> ZUnion2<Self, B>where
B: VldSchema,
fn or<B>(self, other: B) -> ZUnion2<Self, B>where
B: VldSchema,
Either<Self::Output, B::Output>.Source§fn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
fn and<B>(self, other: B) -> ZIntersection<Self, B>where
B: VldSchema,
Auto Trait Implementations§
impl<T> !RefUnwindSafe for ZInteger<T>
impl<T> !UnwindSafe for ZInteger<T>
impl<T> Forget for ZInteger<T>
impl<T> Freeze for ZInteger<T>
impl<T> Send for ZInteger<T>
impl<T> Sync for ZInteger<T>
impl<T> Unpin for ZInteger<T>
impl<T> UnsafeUnpin for ZInteger<T>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more