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
//! Validator configuration for ModelSerializer
//!
//! This module provides configuration structures for managing validators
//! in ModelSerializer instances.

use super::ValidatorError;
use super::validators::{DatabaseValidatorError, UniqueTogetherValidator, UniqueValidator};
use reinhardt_db::backends::DatabaseConnection;
use reinhardt_db::orm::Model;
use serde::Serialize;
use std::marker::PhantomData;
use std::sync::Arc;

/// Object-level synchronous validator that operates directly on a model
/// instance.
///
/// Implementors typically check cross-field invariants that do not require
/// database access (e.g., `start_date < end_date`, `password == password_confirm`).
/// Database-backed checks belong in [`UniqueValidator`] / [`UniqueTogetherValidator`]
/// and run via [`ValidatorConfig::validate_async`].
///
/// `Debug` is required as a supertrait so that [`ValidatorConfig`] retains its
/// derived `Debug` impl.
pub trait ModelLevelValidator<M>: Send + Sync + std::fmt::Debug {
	/// Validate `instance`, returning `Err` to halt validation with a reason.
	fn validate(&self, instance: &M) -> Result<(), ValidatorError>;
}

/// Configuration for field validators
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct ValidatorConfig<M: Model> {
	unique_validators: Vec<UniqueValidator<M>>,
	unique_together_validators: Vec<UniqueTogetherValidator<M>>,
	sync_model_validators: Vec<Arc<dyn ModelLevelValidator<M>>>,
	_phantom: PhantomData<M>,
}

impl<M: Model> ValidatorConfig<M> {
	/// Create a new empty validator configuration
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::serializers::validator_config::ValidatorConfig;
	/// 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 config = ValidatorConfig::<User>::new();
	/// ```
	pub fn new() -> Self {
		Self {
			unique_validators: Vec::new(),
			unique_together_validators: Vec::new(),
			sync_model_validators: Vec::new(),
			_phantom: PhantomData,
		}
	}

	/// Add a unique field validator
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::serializers::validator_config::ValidatorConfig;
	/// 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 mut config = ValidatorConfig::<User>::new();
	/// config.add_unique_validator(UniqueValidator::new("username"));
	/// ```
	pub fn add_unique_validator(&mut self, validator: UniqueValidator<M>) {
		self.unique_validators.push(validator);
	}

	/// Add a unique together validator
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::serializers::validator_config::ValidatorConfig;
	/// 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 mut config = ValidatorConfig::<User>::new();
	/// config.add_unique_together_validator(
	///     UniqueTogetherValidator::new(vec!["username", "email"])
	/// );
	/// ```
	pub fn add_unique_together_validator(&mut self, validator: UniqueTogetherValidator<M>) {
		self.unique_together_validators.push(validator);
	}

	/// Add an object-level synchronous validator.
	///
	/// Synchronous validators run inside [`Self::validate`] and at the start of
	/// [`Self::validate_async`]. They never touch the database; for unique-style
	/// checks use [`Self::add_unique_validator`] instead.
	pub fn add_sync_model_validator(&mut self, validator: Arc<dyn ModelLevelValidator<M>>) {
		self.sync_model_validators.push(validator);
	}

	/// Get all unique validators
	pub fn unique_validators(&self) -> &[UniqueValidator<M>] {
		&self.unique_validators
	}

	/// Get all unique together validators
	pub fn unique_together_validators(&self) -> &[UniqueTogetherValidator<M>] {
		&self.unique_together_validators
	}

	/// Get all object-level synchronous validators
	pub fn sync_model_validators(&self) -> &[Arc<dyn ModelLevelValidator<M>>] {
		&self.sync_model_validators
	}

	/// Check if any validators are configured
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::serializers::validator_config::ValidatorConfig;
	/// 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 mut config = ValidatorConfig::<User>::new();
	/// assert!(!config.has_validators());
	///
	/// config.add_unique_validator(UniqueValidator::new("username"));
	/// assert!(config.has_validators());
	/// ```
	pub fn has_validators(&self) -> bool {
		!self.unique_validators.is_empty()
			|| !self.unique_together_validators.is_empty()
			|| !self.sync_model_validators.is_empty()
	}

	/// Run only synchronous validators against `instance`.
	///
	/// Returns the first failure as an `Err`. No-op (returns `Ok(())`) when no
	/// synchronous validators are registered, preserving backward compatibility
	/// for callers that rely on [`Self::has_validators`] being false.
	pub fn validate(&self, instance: &M) -> Result<(), ValidatorError> {
		for validator in &self.sync_model_validators {
			validator.validate(instance)?;
		}
		Ok(())
	}

	/// Validate model instance asynchronously against configured validators
	///
	/// Performs database-backed validation checks (uniqueness constraints).
	/// Converts the model instance to JSON for field extraction.
	///
	/// # Arguments
	///
	/// * `connection` - Database connection for validation queries
	/// * `instance` - Model instance to validate
	/// * `instance_pk` - Optional primary key (for update operations, excludes current record)
	///
	/// # Errors
	///
	/// Returns `DatabaseValidatorError` if:
	/// - Serialization fails
	/// - Field not found in serialized data
	/// - Unique constraint violated
	/// - Unique together constraint violated
	/// - Database query fails
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_rest::serializers::validator_config::ValidatorConfig;
	/// use reinhardt_db::connection::DatabaseConnection;
	///
	/// let config = ValidatorConfig::new();
	/// let user = User { id: None, username: "alice".into() };
	/// config.validate_async(&connection, &user, None).await?;
	/// ```
	pub async fn validate_async(
		&self,
		connection: &DatabaseConnection,
		instance: &M,
		instance_pk: Option<&M::PrimaryKey>,
	) -> Result<(), DatabaseValidatorError>
	where
		M: Serialize,
		M::PrimaryKey: std::fmt::Display,
	{
		// Convert model instance to JSON for field extraction
		let value =
			serde_json::to_value(instance).map_err(|e| DatabaseValidatorError::DatabaseError {
				message: format!("Failed to serialize model: {}", e),
				query: None,
			})?;

		let obj = value
			.as_object()
			.ok_or_else(|| DatabaseValidatorError::DatabaseError {
				message: "Model must serialize to an object".to_string(),
				query: None,
			})?;

		// Validate unique constraints
		for validator in &self.unique_validators {
			let field_value = obj
				.get(validator.field_name())
				.and_then(|v| v.as_str())
				.ok_or_else(|| DatabaseValidatorError::FieldNotFound {
					field: validator.field_name().to_string(),
				})?;

			validator
				.validate(connection, field_value, instance_pk)
				.await?;
		}

		// Validate unique together constraints
		for validator in &self.unique_together_validators {
			let mut values = std::collections::HashMap::new();
			for field in validator.field_names() {
				let value = obj.get(field).and_then(|v| v.as_str()).ok_or_else(|| {
					DatabaseValidatorError::FieldNotFound {
						field: field.clone(),
					}
				})?;
				values.insert(field.clone(), value.to_string());
			}

			validator.validate(connection, &values, instance_pk).await?;
		}

		Ok(())
	}
}

impl<M: Model> Default for ValidatorConfig<M> {
	fn default() -> Self {
		Self::new()
	}
}

// Manually re-assert the `UnwindSafe` / `RefUnwindSafe` auto traits that the
// new `Vec<Arc<dyn ModelLevelValidator<M>>>` field would otherwise strip.
// Trait objects do not propagate these markers, so the previously
// auto-derived impls disappeared, triggering cargo-semver-checks
// `auto_trait_impl_removed` under the RC phase's no-breaking-change policy.
// The trait objects are only reached via `&self` accessors and `Arc::clone`,
// so panic-safety guarantees match the pre-PR public contract.
impl<M: Model> std::panic::UnwindSafe for ValidatorConfig<M> {}
impl<M: Model> std::panic::RefUnwindSafe for ValidatorConfig<M> {}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::serializers::validators::{UniqueTogetherValidator, UniqueValidator};
	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_validator_config_new() {
		let config = ValidatorConfig::<TestUser>::new();
		assert_eq!(config.unique_validators().len(), 0);
		assert_eq!(config.unique_together_validators().len(), 0);
		assert!(!config.has_validators());
	}

	#[test]
	fn test_add_unique_validator() {
		let mut config = ValidatorConfig::<TestUser>::new();
		config.add_unique_validator(UniqueValidator::new("username"));

		assert_eq!(config.unique_validators().len(), 1);
		assert!(config.has_validators());
	}

	#[test]
	fn test_add_unique_together_validator() {
		let mut config = ValidatorConfig::<TestUser>::new();
		config
			.add_unique_together_validator(UniqueTogetherValidator::new(vec!["username", "email"]));

		assert_eq!(config.unique_together_validators().len(), 1);
		assert!(config.has_validators());
	}

	#[test]
	fn test_multiple_validators() {
		let mut config = ValidatorConfig::<TestUser>::new();
		config.add_unique_validator(UniqueValidator::new("username"));
		config.add_unique_validator(UniqueValidator::new("email"));
		config
			.add_unique_together_validator(UniqueTogetherValidator::new(vec!["username", "email"]));

		assert_eq!(config.unique_validators().len(), 2);
		assert_eq!(config.unique_together_validators().len(), 1);
		assert!(config.has_validators());
	}
}