use super::sea_orm;
crate::db::__vld_db_error!(
VldSeaError
);
impl From<VldSeaError> for sea_orm::DbErr {
fn from(e: VldSeaError) -> Self {
sea_orm::DbErr::Custom(e.to_string())
}
}
pub fn active_model_to_json<A>(model: &A) -> serde_json::Value
where
A: sea_orm::ActiveModelTrait,
{
use sea_orm::{ActiveValue, EntityTrait, IdenStatic, Iterable};
let mut map = serde_json::Map::new();
for col in <<A as sea_orm::ActiveModelTrait>::Entity as EntityTrait>::Column::iter() {
match model.get(col) {
ActiveValue::Set(v) | ActiveValue::Unchanged(v) => {
map.insert(col.as_str().to_string(), sea_value_to_json(v));
}
ActiveValue::NotSet => {} }
}
serde_json::Value::Object(map)
}
#[allow(unreachable_patterns)]
fn sea_value_to_json(v: sea_orm::Value) -> serde_json::Value {
use sea_orm::sea_query::Value as SV;
use serde_json::Value as J;
fn float(n: f64) -> J {
serde_json::Number::from_f64(n)
.map(J::Number)
.unwrap_or(J::Null)
}
match v {
SV::Bool(Some(b)) => J::Bool(b),
SV::TinyInt(Some(n)) => (n as i64).into(),
SV::SmallInt(Some(n)) => (n as i64).into(),
SV::Int(Some(n)) => (n as i64).into(),
SV::BigInt(Some(n)) => n.into(),
SV::TinyUnsigned(Some(n)) => (n as u64).into(),
SV::SmallUnsigned(Some(n)) => (n as u64).into(),
SV::Unsigned(Some(n)) => (n as u64).into(),
SV::BigUnsigned(Some(n)) => n.into(),
SV::Float(Some(n)) => float(n.to_string().parse().unwrap_or(f64::from(n))),
SV::Double(Some(n)) => float(n),
SV::String(Some(s)) => J::String(s.to_string()),
SV::Char(Some(c)) => J::String(c.to_string()),
SV::Bytes(Some(b)) => J::Array(b.iter().map(|x| J::from(*x)).collect()),
#[cfg(feature = "with-json")]
SV::Json(Some(j)) => (*j).clone(),
#[cfg(feature = "with-chrono")]
SV::ChronoDate(Some(d)) => J::String(d.to_string()),
#[cfg(feature = "with-chrono")]
SV::ChronoTime(Some(t)) => J::String(t.to_string()),
#[cfg(feature = "with-chrono")]
SV::ChronoDateTime(Some(dt)) => J::String(dt.format("%Y-%m-%dT%H:%M:%S%.f").to_string()),
#[cfg(feature = "with-chrono")]
SV::ChronoDateTimeUtc(Some(dt)) => J::String(dt.to_rfc3339()),
#[cfg(feature = "with-chrono")]
SV::ChronoDateTimeLocal(Some(dt)) => J::String(dt.to_rfc3339()),
#[cfg(feature = "with-chrono")]
SV::ChronoDateTimeWithTimeZone(Some(dt)) => J::String(dt.to_rfc3339()),
#[cfg(feature = "with-time")]
SV::TimeDate(Some(d)) => J::String(time_fmt::date(&d)),
#[cfg(feature = "with-time")]
SV::TimeTime(Some(t)) => J::String(time_fmt::time(&t)),
#[cfg(feature = "with-time")]
SV::TimeDateTime(Some(dt)) => J::String(time_fmt::datetime(&dt)),
#[cfg(feature = "with-time")]
SV::TimeDateTimeWithTimeZone(Some(dt)) => J::String(time_fmt::offset_datetime(&dt)),
#[cfg(feature = "postgres-array")]
SV::Array(_, Some(items)) => {
J::Array((*items).into_iter().map(sea_value_to_json).collect())
}
#[cfg(feature = "with-uuid")]
SV::Uuid(Some(u)) => J::String(u.to_string()),
#[cfg(feature = "with-rust_decimal")]
SV::Decimal(Some(d)) => J::String(d.to_string()),
#[cfg(feature = "with-bigdecimal")]
SV::BigDecimal(Some(d)) => J::String(d.to_string()),
other => super::compat::other_value_to_json(other),
}
}
#[cfg(feature = "with-time")]
mod time_fmt {
use super::sea_orm::prelude::{TimeDate, TimeDateTime, TimeDateTimeWithTimeZone, TimeTime};
use std::borrow::Borrow;
pub(crate) fn date(d: &impl Borrow<TimeDate>) -> String {
let d = d.borrow();
format!("{:04}-{:02}-{:02}", d.year(), u8::from(d.month()), d.day())
}
pub(crate) fn time(t: &impl Borrow<TimeTime>) -> String {
let t = t.borrow();
let s = format!("{:02}:{:02}:{:02}", t.hour(), t.minute(), t.second());
match t.nanosecond() {
0 => s,
n if n % 1_000_000 == 0 => format!("{s}.{:03}", n / 1_000_000),
n if n % 1_000 == 0 => format!("{s}.{:06}", n / 1_000),
n => format!("{s}.{n:09}"),
}
}
pub(crate) fn datetime(dt: &impl Borrow<TimeDateTime>) -> String {
let dt = dt.borrow();
format!("{}T{}", date(&dt.date()), time(&dt.time()))
}
pub(crate) fn offset_datetime(dt: &impl Borrow<TimeDateTimeWithTimeZone>) -> String {
let dt = dt.borrow();
let off = dt.offset();
let secs = off.whole_seconds();
let (sign, secs) = if secs < 0 { ('-', -secs) } else { ('+', secs) };
format!(
"{}T{}{sign}{:02}:{:02}",
date(&dt.date()),
time(&dt.time()),
secs / 3600,
secs / 60 % 60
)
}
}
pub fn validate_active<S, A>(model: &A) -> Result<S, VldSeaError>
where
S: vld::schema::VldParse,
A: sea_orm::ActiveModelTrait,
{
let json = active_model_to_json(model);
S::vld_parse_value(&json).map_err(VldSeaError::Validation)
}
pub fn validate_model<S, T>(value: &T) -> Result<S, VldSeaError>
where
S: vld::schema::VldParse,
T: serde::Serialize,
{
__vld_validate::<S, T>(value)
}
pub fn validate_json<S>(json: &serde_json::Value) -> Result<S, VldSeaError>
where
S: vld::schema::VldParse,
{
S::vld_parse_value(json).map_err(VldSeaError::Validation)
}
pub fn before_save<S, A>(model: &A) -> Result<(), sea_orm::DbErr>
where
S: vld::schema::VldParse,
A: sea_orm::ActiveModelTrait,
{
validate_active::<S, A>(model)
.map(|_| ())
.map_err(Into::into)
}
crate::db::__vld_db_validated!(VldSeaError);
pub mod prelude {
pub use super::{
Validated, VldSeaError, active_model_to_json, before_save, validate_active, validate_json,
validate_model,
};
pub use vld::prelude::*;
}