Skip to content

Commit

Permalink
test: add diesel example
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed May 26, 2024
1 parent 3cb62ba commit 6f9a4c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ actix-web = { version = "4.4.0" }
rocket = "=0.5.0-rc.3"

validator = { version = "0.18", features = ["derive"] }
url = "2.5.0"
url = "2.5.0"

diesel = { version = "2.1.6", features = ["postgres"] }
49 changes: 46 additions & 3 deletions examples/string_custom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use diesel::{Connection, ExpressionMethods, PgConnection, Queryable, Selectable};
use valitron::{
available::{Email, Trim},
register::string::Validator,
Expand All @@ -11,21 +12,24 @@ pub fn main() {
gender: "male".into(),
password: "Abc123".into(),
age: 12,
weight: 101.2,
};

let data = Input::new(data).unwrap();

assert_eq!(data.name, "Jone");

PgConnection::establish("aaa").unwrap();
}

#[derive(Queryable, Selectable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
struct Input {
name: String,
email: String,
gender: String,
password: String,
age: u8,
weight: f32,
age: i32,
//weight: f32,
}

impl Input {
Expand Down Expand Up @@ -55,3 +59,42 @@ impl StringRule for MyRequired<'_> {
format!("{} is not be empty", self.0)
}
}

pub fn establish_connection() -> PgConnection {
PgConnection::establish("DATABASE_URL").unwrap()
}


diesel::table! {
inputs (email) {
name -> Varchar,
email -> Varchar,
gender -> Varchar,
password -> Varchar,
age -> Integer
}
}

#[derive(Clone)]
struct UniqueEmail;

impl StringRule for UniqueEmail {
type Message = String;
const NAME: &'static str = "unique_email";
fn call(&mut self, data: &mut String) -> bool {

use diesel::prelude::*;
use self::inputs::dsl::*;
//use self::models::*;

let conn = &mut establish_connection();

let results = inputs.filter(email.eq(data.to_owned())).select(Input::as_select()).load(conn).unwrap();

results.len() == 0
}

fn message(&self) -> Self::Message {
format!("email is existing")
}
}

0 comments on commit 6f9a4c4

Please sign in to comment.