Skip to content

Commit eda1b01

Browse files
committed
models/krate: Move URL and name validation out of create_or_update()
`create_or_update()` is only called in four places. Three of them are within test code, where a `create()` fn would be sufficient and where we don't necessarily need the same kind of validation. The fourth place is the publish endpoint, where this validation is definitely needed, but could potentially be combined with other validation code in the future.
1 parent e865596 commit eda1b01

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/controllers/krate/publish.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
142142
};
143143

144144
let license_file = metadata.license_file.as_deref();
145+
146+
persist.validate()?;
147+
persist.ensure_name_not_reserved(conn)?;
148+
145149
let krate = persist.create_or_update(conn, user.id)?;
146150

147151
let owners = krate.owners(conn)?;

src/models/krate.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ impl<'a> NewCrate<'a> {
105105
pub fn create_or_update(self, conn: &mut PgConnection, uploader: i32) -> AppResult<Crate> {
106106
use diesel::update;
107107

108-
self.validate()?;
109-
self.ensure_name_not_reserved(conn)?;
110-
111108
conn.transaction(|conn| {
112109
// To avoid race conditions, we try to insert
113110
// first so we know whether to add an owner
@@ -124,7 +121,7 @@ impl<'a> NewCrate<'a> {
124121
})
125122
}
126123

127-
fn validate(&self) -> AppResult<()> {
124+
pub fn validate(&self) -> AppResult<()> {
128125
fn validate_url(url: Option<&str>, field: &str) -> AppResult<()> {
129126
let url = match url {
130127
Some(s) => s,
@@ -151,7 +148,7 @@ impl<'a> NewCrate<'a> {
151148
Ok(())
152149
}
153150

154-
fn ensure_name_not_reserved(&self, conn: &mut PgConnection) -> AppResult<()> {
151+
pub fn ensure_name_not_reserved(&self, conn: &mut PgConnection) -> AppResult<()> {
155152
use crate::schema::reserved_crate_names::dsl::*;
156153
use diesel::dsl::exists;
157154
use diesel::select;

0 commit comments

Comments
 (0)