reinhardt-rest 0.1.2

REST API framework aggregator for Reinhardt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Validators for ModelSerializer
//!
//! This module provides validators for enforcing database constraints
//! such as uniqueness of fields.
//!
//! # Examples
//!
//! ```no_run
//! use reinhardt_rest::serializers::validators::{UniqueValidator, UniqueTogetherValidator};
//! use reinhardt_db::orm::Model;
//! use reinhardt_db::backends::DatabaseConnection;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Debug, Clone, Serialize, Deserialize)]
//! struct User {
//!     id: Option<i64>,
//!     username: String,
//!     email: String,
//! }
//!
//! impl Model for User {
//!     type PrimaryKey = i64;
//!     type Fields = UserFields;
//!     fn table_name() -> &'static str { "users" }
//!     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
//!     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
//!     fn new_fields() -> Self::Fields { UserFields }
//! }
//! #[derive(Clone)]
//! struct UserFields;
//! impl reinhardt_db::orm::FieldSelector for UserFields {
//!     fn with_alias(self, _alias: &str) -> Self { self }
//! }
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let connection = DatabaseConnection::connect_postgres("postgres://localhost/test").await?;
//!
//! // Validate that username is unique
//! let validator = UniqueValidator::<User>::new("username");
//! validator.validate(&connection, "alice", None).await?;
//!
//! // Validate that (username, email) combination is unique
//! let mut values = std::collections::HashMap::new();
//! values.insert("username".to_string(), "alice".to_string());
//! values.insert("email".to_string(), "alice@example.com".to_string());
//!
//! let validator = UniqueTogetherValidator::<User>::new(vec!["username", "email"]);
//! validator.validate(&connection, &values, None).await?;
//! # Ok(())
//! # }
//! ```

use super::SerializerError;
use reinhardt_db::backends::DatabaseConnection;
use reinhardt_db::orm::{Filter, FilterOperator, FilterValue, Model};
use std::marker::PhantomData;
use thiserror::Error;

/// Errors that can occur during database validation
#[derive(Debug, Error, Clone, PartialEq)]
pub enum DatabaseValidatorError {
	/// A unique constraint was violated for a single field
	#[error("Unique constraint violated: {field} = '{value}' already exists in table {table}")]
	UniqueConstraintViolation {
		/// The field name that violated the constraint
		field: String,
		/// The value that caused the violation
		value: String,
		/// The table name
		table: String,
		/// Optional custom message
		message: Option<String>,
	},

	/// A unique together constraint was violated for multiple fields
	#[error(
		"Unique together constraint violated: fields ({fields:?}) with values ({values:?}) already exist in table {table}"
	)]
	UniqueTogetherViolation {
		/// The field names that violated the constraint
		fields: Vec<String>,
		/// The values that caused the violation
		values: Vec<String>,
		/// The table name
		table: String,
		/// Optional custom message
		message: Option<String>,
	},

	/// A database error occurred during validation
	#[error("Database error during validation: {message}")]
	DatabaseError {
		/// The error message from the database
		message: String,
		/// The SQL query that failed (optional, for debugging)
		query: Option<String>,
	},

	/// A required field was not found in the data
	#[error("Required field '{field}' not found in validation data")]
	FieldNotFound {
		/// The field name that was missing
		field: String,
	},
}

impl From<DatabaseValidatorError> for SerializerError {
	fn from(err: DatabaseValidatorError) -> Self {
		SerializerError::Other {
			message: err.to_string(),
		}
	}
}

impl From<DatabaseValidatorError> for reinhardt_core::exception::Error {
	fn from(err: DatabaseValidatorError) -> Self {
		match err {
			DatabaseValidatorError::UniqueConstraintViolation {
				field,
				value,
				table,
				message,
			} => {
				let msg = message.unwrap_or_else(|| {
					format!(
						"Field '{}' with value '{}' already exists in {}",
						field, value, table
					)
				});
				reinhardt_core::exception::Error::Conflict(msg)
			}
			DatabaseValidatorError::UniqueTogetherViolation {
				fields,
				values,
				table,
				message,
			} => {
				let msg = message.unwrap_or_else(|| {
					format!(
						"Combination of fields {:?} with values {:?} already exists in {}",
						fields, values, table
					)
				});
				reinhardt_core::exception::Error::Conflict(msg)
			}
			DatabaseValidatorError::FieldNotFound { field } => {
				reinhardt_core::exception::Error::Validation(format!(
					"Required field '{}' not found",
					field
				))
			}
			DatabaseValidatorError::DatabaseError { message, .. } => {
				reinhardt_core::exception::Error::Database(message)
			}
		}
	}
}

/// UniqueValidator ensures that a field value is unique in the database
///
/// This validator checks that a given field value doesn't already exist
/// in the database table, with optional support for excluding the current
/// instance during updates.
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_rest::serializers::validators::UniqueValidator;
/// # use reinhardt_db::orm::Model;
/// # use reinhardt_db::backends::DatabaseConnection;
/// # use serde::{Serialize, Deserialize};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct User {
/// #     id: Option<i64>,
/// #     username: String,
/// # }
/// #
/// # impl Model for User {
/// #     type PrimaryKey = i64;
/// #     type Fields = UserFields;
/// #     fn table_name() -> &'static str { "users" }
/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
/// #     fn new_fields() -> Self::Fields { UserFields }
/// # }
/// # #[derive(Clone)]
/// # struct UserFields;
/// # impl reinhardt_db::orm::FieldSelector for UserFields {
/// #     fn with_alias(self, _alias: &str) -> Self { self }
/// # }
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let connection = DatabaseConnection::connect_postgres("postgres://localhost/test").await?;
/// let validator = UniqueValidator::<User>::new("username");
///
/// // Check if "alice" is unique
/// validator.validate(&connection, "alice", None).await?;
///
/// // Check if "alice" is unique, excluding user with id=1
/// let user_id = 1i64;
/// validator.validate(&connection, "alice", Some(&user_id)).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct UniqueValidator<M: Model> {
	field_name: String,
	message: Option<String>,
	_phantom: PhantomData<M>,
}

impl<M: Model> UniqueValidator<M> {
	/// Create a new UniqueValidator for the specified field
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_rest::serializers::validators::UniqueValidator;
	/// # use reinhardt_db::orm::Model;
	/// # use serde::{Serialize, Deserialize};
	/// #
	/// # #[derive(Debug, Clone, Serialize, Deserialize)]
	/// # struct User { id: Option<i64>, username: String }
	/// #
	/// # impl Model for User {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = UserFields;
	/// #     fn table_name() -> &'static str { "users" }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
	/// #     fn new_fields() -> Self::Fields { UserFields }
	/// # }
	/// # #[derive(Clone)]
	/// # struct UserFields;
	/// # impl reinhardt_db::orm::FieldSelector for UserFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// let validator = UniqueValidator::<User>::new("username");
	/// // Verify the validator is created successfully
	/// let _: UniqueValidator<User> = validator;
	/// ```
	pub fn new(field_name: impl Into<String>) -> Self {
		Self {
			field_name: field_name.into(),
			message: None,
			_phantom: PhantomData,
		}
	}

	/// Set a custom error message
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_rest::serializers::validators::UniqueValidator;
	/// # use reinhardt_db::orm::Model;
	/// # use serde::{Serialize, Deserialize};
	/// #
	/// # #[derive(Debug, Clone, Serialize, Deserialize)]
	/// # struct User { id: Option<i64>, username: String }
	/// #
	/// # impl Model for User {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = UserFields;
	/// #     fn table_name() -> &'static str { "users" }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
	/// #     fn new_fields() -> Self::Fields { UserFields }
	/// # }
	/// # #[derive(Clone)]
	/// # struct UserFields;
	/// # impl reinhardt_db::orm::FieldSelector for UserFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// let validator = UniqueValidator::<User>::new("username")
	///     .with_message("Username must be unique");
	/// // Verify the validator is configured with custom message
	/// let _: UniqueValidator<User> = validator;
	/// ```
	pub fn with_message(mut self, message: impl Into<String>) -> Self {
		self.message = Some(message.into());
		self
	}

	/// Get the field name being validated
	pub fn field_name(&self) -> &str {
		&self.field_name
	}

	/// Validates that the given value is unique for this field in the database.
	pub async fn validate(
		&self,
		_connection: &DatabaseConnection,
		value: &str,
		instance_pk: Option<&M::PrimaryKey>,
	) -> Result<(), DatabaseValidatorError>
	where
		M::PrimaryKey: std::fmt::Display,
	{
		let table_name = M::table_name();

		// Build QuerySet with filter
		let mut qs = M::objects().all();
		qs = qs.filter(Filter::new(
			self.field_name.clone(),
			FilterOperator::Eq,
			FilterValue::String(value.to_string()),
		));

		// Exclude current instance if updating
		if let Some(pk) = instance_pk {
			qs = qs.filter(Filter::new(
				M::primary_key_field().to_string(),
				FilterOperator::Ne,
				FilterValue::String(pk.to_string()),
			));
		}

		// Execute count query
		let count = qs
			.count()
			.await
			.map_err(|e| DatabaseValidatorError::DatabaseError {
				message: e.to_string(),
				query: None,
			})?;

		if count > 0 {
			Err(DatabaseValidatorError::UniqueConstraintViolation {
				field: self.field_name.clone(),
				value: value.to_string(),
				table: table_name.to_string(),
				message: self.message.clone(),
			})
		} else {
			Ok(())
		}
	}
}

/// UniqueTogetherValidator ensures that a combination of fields is unique
///
/// This validator checks that a combination of field values doesn't already exist
/// in the database table, with optional support for excluding the current
/// instance during updates.
///
/// # Examples
///
/// ```no_run
/// # use reinhardt_rest::serializers::validators::UniqueTogetherValidator;
/// # use reinhardt_db::orm::Model;
/// # use reinhardt_db::backends::DatabaseConnection;
/// # use serde::{Serialize, Deserialize};
/// # use std::collections::HashMap;
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
/// # struct User {
/// #     id: Option<i64>,
/// #     username: String,
/// #     email: String,
/// # }
/// #
/// # impl Model for User {
/// #     type PrimaryKey = i64;
/// #     type Fields = UserFields;
/// #     fn table_name() -> &'static str { "users" }
/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
/// #     fn new_fields() -> Self::Fields { UserFields }
/// # }
/// # #[derive(Clone)]
/// # struct UserFields;
/// # impl reinhardt_db::orm::FieldSelector for UserFields {
/// #     fn with_alias(self, _alias: &str) -> Self { self }
/// # }
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let connection = DatabaseConnection::connect_postgres("postgres://localhost/test").await?;
/// let validator = UniqueTogetherValidator::<User>::new(vec!["username", "email"]);
///
/// let mut values = HashMap::new();
/// values.insert("username".to_string(), "alice".to_string());
/// values.insert("email".to_string(), "alice@example.com".to_string());
///
/// validator.validate(&connection, &values, None).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct UniqueTogetherValidator<M: Model> {
	field_names: Vec<String>,
	message: Option<String>,
	_phantom: PhantomData<M>,
}

impl<M: Model> UniqueTogetherValidator<M> {
	/// Create a new UniqueTogetherValidator for the specified fields
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_rest::serializers::validators::UniqueTogetherValidator;
	/// # use reinhardt_db::orm::Model;
	/// # use serde::{Serialize, Deserialize};
	/// #
	/// # #[derive(Debug, Clone, Serialize, Deserialize)]
	/// # struct User { id: Option<i64>, username: String, email: String }
	/// #
	/// # impl Model for User {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = UserFields;
	/// #     fn table_name() -> &'static str { "users" }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
	/// #     fn new_fields() -> Self::Fields { UserFields }
	/// # }
	/// # #[derive(Clone)]
	/// # struct UserFields;
	/// # impl reinhardt_db::orm::FieldSelector for UserFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// let validator = UniqueTogetherValidator::<User>::new(vec!["username", "email"]);
	/// // Verify the validator is created successfully
	/// let _: UniqueTogetherValidator<User> = validator;
	/// ```
	pub fn new(field_names: Vec<impl Into<String>>) -> Self {
		Self {
			field_names: field_names.into_iter().map(|f| f.into()).collect(),
			message: None,
			_phantom: PhantomData,
		}
	}

	/// Set a custom error message
	///
	/// # Examples
	///
	/// ```
	/// # use reinhardt_rest::serializers::validators::UniqueTogetherValidator;
	/// # use reinhardt_db::orm::Model;
	/// # use serde::{Serialize, Deserialize};
	/// #
	/// # #[derive(Debug, Clone, Serialize, Deserialize)]
	/// # struct User { id: Option<i64>, username: String, email: String }
	/// #
	/// # impl Model for User {
	/// #     type PrimaryKey = i64;
	/// #     type Fields = UserFields;
	/// #     fn table_name() -> &'static str { "users" }
	/// #     fn primary_key(&self) -> Option<Self::PrimaryKey> { self.id }
	/// #     fn set_primary_key(&mut self, value: Self::PrimaryKey) { self.id = Some(value); }
	/// #     fn new_fields() -> Self::Fields { UserFields }
	/// # }
	/// # #[derive(Clone)]
	/// # struct UserFields;
	/// # impl reinhardt_db::orm::FieldSelector for UserFields {
	/// #     fn with_alias(self, _alias: &str) -> Self { self }
	/// # }
	/// let validator = UniqueTogetherValidator::<User>::new(vec!["username", "email"])
	///     .with_message("Username and email combination must be unique");
	/// // Verify the validator is configured with custom message
	/// let _: UniqueTogetherValidator<User> = validator;
	/// ```
	pub fn with_message(mut self, message: impl Into<String>) -> Self {
		self.message = Some(message.into());
		self
	}

	/// Get the field names being validated
	pub fn field_names(&self) -> &[String] {
		&self.field_names
	}

	/// Validates that the combination of field values is unique together in the database.
	pub async fn validate(
		&self,
		_connection: &DatabaseConnection,
		values: &std::collections::HashMap<String, String>,
		instance_pk: Option<&M::PrimaryKey>,
	) -> Result<(), DatabaseValidatorError>
	where
		M::PrimaryKey: std::fmt::Display,
	{
		let table_name = M::table_name();

		// Build QuerySet with filters for all fields
		let mut qs = M::objects().all();
		let mut field_values = Vec::new();

		for field_name in &self.field_names {
			let value =
				values
					.get(field_name)
					.ok_or_else(|| DatabaseValidatorError::FieldNotFound {
						field: field_name.clone(),
					})?;
			field_values.push(value.clone());

			qs = qs.filter(Filter::new(
				field_name.clone(),
				FilterOperator::Eq,
				FilterValue::String(value.clone()),
			));
		}

		// Exclude current instance if updating
		if let Some(pk) = instance_pk {
			qs = qs.filter(Filter::new(
				M::primary_key_field().to_string(),
				FilterOperator::Ne,
				FilterValue::String(pk.to_string()),
			));
		}

		// Execute count query
		let count = qs
			.count()
			.await
			.map_err(|e| DatabaseValidatorError::DatabaseError {
				message: e.to_string(),
				query: None,
			})?;

		if count > 0 {
			Err(DatabaseValidatorError::UniqueTogetherViolation {
				fields: self.field_names.clone(),
				values: field_values,
				table: table_name.to_string(),
				message: self.message.clone(),
			})
		} else {
			Ok(())
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use reinhardt_db::orm::FieldSelector;

	#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
	struct TestUser {
		id: Option<i64>,
		username: String,
		email: String,
	}

	#[derive(Debug, Clone)]
	struct TestUserFields;

	impl FieldSelector for TestUserFields {
		fn with_alias(self, _alias: &str) -> Self {
			self
		}
	}

	impl Model for TestUser {
		type PrimaryKey = i64;
		type Fields = TestUserFields;

		fn table_name() -> &'static str {
			"test_users"
		}

		fn new_fields() -> Self::Fields {
			TestUserFields
		}

		fn primary_key(&self) -> Option<Self::PrimaryKey> {
			self.id
		}

		fn set_primary_key(&mut self, value: Self::PrimaryKey) {
			self.id = Some(value);
		}
	}

	#[test]
	fn test_unique_validator_new() {
		let validator = UniqueValidator::<TestUser>::new("username");
		assert_eq!(validator.field_name(), "username");
	}

	#[test]
	fn test_unique_validator_with_message() {
		let validator =
			UniqueValidator::<TestUser>::new("username").with_message("Custom error message");
		assert_eq!(validator.field_name(), "username");
		assert!(validator.message.is_some());
	}

	#[test]
	fn test_unique_together_validator_new() {
		let validator = UniqueTogetherValidator::<TestUser>::new(vec!["username", "email"]);
		assert_eq!(validator.field_names().len(), 2);
		assert_eq!(validator.field_names()[0], "username");
		assert_eq!(validator.field_names()[1], "email");
	}

	#[test]
	fn test_unique_together_validator_with_message() {
		let validator = UniqueTogetherValidator::<TestUser>::new(vec!["username", "email"])
			.with_message("Custom combination message");
		assert_eq!(validator.field_names().len(), 2);
		assert!(validator.message.is_some());
	}
}