validation of characters length is finished

This commit is contained in:
Priec
2025-08-05 18:27:16 +02:00
parent abd8cba7a5
commit 9c36e76eaa
6 changed files with 1004 additions and 16 deletions

View File

@@ -326,6 +326,20 @@ impl<D: DataProvider> FormEditor<D> {
pub fn validation_summary(&self) -> crate::validation::ValidationSummary {
self.ui_state.validation.summary()
}
/// Check if field switching is allowed from current field
#[cfg(feature = "validation")]
pub fn can_switch_fields(&self) -> bool {
let current_text = self.current_text();
self.ui_state.validation.allows_field_switch(self.ui_state.current_field, current_text)
}
/// Get reason why field switching is blocked (if any)
#[cfg(feature = "validation")]
pub fn field_switch_block_reason(&self) -> Option<String> {
let current_text = self.current_text();
self.ui_state.validation.field_switch_block_reason(self.ui_state.current_field, current_text)
}
// ===================================================================
// ASYNC OPERATIONS: Only autocomplete needs async
@@ -409,10 +423,22 @@ impl<D: DataProvider> FormEditor<D> {
// ===================================================================
/// Move to previous field (vim k / up arrow)
pub fn move_up(&mut self) {
pub fn move_up(&mut self) -> Result<()> {
let field_count = self.data_provider.field_count();
if field_count == 0 {
return;
return Ok(());
}
// Check if field switching is allowed (minimum character enforcement)
#[cfg(feature = "validation")]
{
let current_text = self.current_text();
if !self.ui_state.validation.allows_field_switch(self.ui_state.current_field, current_text) {
if let Some(reason) = self.ui_state.validation.field_switch_block_reason(self.ui_state.current_field, current_text) {
tracing::debug!("Field switch blocked: {}", reason);
return Err(anyhow::anyhow!("Cannot switch fields: {}", reason));
}
}
}
// Validate current field before moving
@@ -430,13 +456,26 @@ impl<D: DataProvider> FormEditor<D> {
self.ui_state.move_to_field(new_field, field_count);
self.clamp_cursor_to_current_field();
Ok(())
}
/// Move to next field (vim j / down arrow)
pub fn move_down(&mut self) {
pub fn move_down(&mut self) -> Result<()> {
let field_count = self.data_provider.field_count();
if field_count == 0 {
return;
return Ok(());
}
// Check if field switching is allowed (minimum character enforcement)
#[cfg(feature = "validation")]
{
let current_text = self.current_text();
if !self.ui_state.validation.allows_field_switch(self.ui_state.current_field, current_text) {
if let Some(reason) = self.ui_state.validation.field_switch_block_reason(self.ui_state.current_field, current_text) {
tracing::debug!("Field switch blocked: {}", reason);
return Err(anyhow::anyhow!("Cannot switch fields: {}", reason));
}
}
}
// Validate current field before moving
@@ -454,6 +493,7 @@ impl<D: DataProvider> FormEditor<D> {
self.ui_state.move_to_field(new_field, field_count);
self.clamp_cursor_to_current_field();
Ok(())
}
/// Move to first field (vim gg)
@@ -480,13 +520,13 @@ impl<D: DataProvider> FormEditor<D> {
}
/// Move to previous field (alternative to move_up)
pub fn prev_field(&mut self) {
self.move_up();
pub fn prev_field(&mut self) -> Result<()> {
self.move_up()
}
/// Move to next field (alternative to move_down)
pub fn next_field(&mut self) {
self.move_down();
pub fn next_field(&mut self) -> Result<()> {
self.move_down()
}
/// Move to start of current field (vim 0)
@@ -649,15 +689,16 @@ impl<D: DataProvider> FormEditor<D> {
}
/// Exit edit mode to read-only mode (vim Escape)
pub fn exit_edit_mode(&mut self) {
pub fn exit_edit_mode(&mut self) -> Result<()> {
// Validate current field content when exiting edit mode
#[cfg(feature = "validation")]
{
let current_text = self.current_text().to_string(); // Convert to String to avoid borrow conflicts
let _validation_result = self.ui_state.validation.validate_field_content(
self.ui_state.current_field,
&current_text,
);
let current_text = self.current_text();
if !self.ui_state.validation.allows_field_switch(self.ui_state.current_field, current_text) {
if let Some(reason) = self.ui_state.validation.field_switch_block_reason(self.ui_state.current_field, current_text) {
return Err(anyhow::anyhow!("Cannot exit edit mode: {}", reason));
}
}
}
// Adjust cursor position when transitioning from edit to normal mode
@@ -674,6 +715,8 @@ impl<D: DataProvider> FormEditor<D> {
self.set_mode(AppMode::ReadOnly);
// Deactivate autocomplete when exiting edit mode
self.ui_state.deactivate_autocomplete();
Ok(())
}
/// Enter edit mode from read-only mode (vim i/a/o)

View File

@@ -1,3 +1,4 @@
// src/validation/config.rs
//! Validation configuration types and builders
use crate::validation::CharacterLimits;
@@ -158,6 +159,32 @@ impl ValidationConfig {
// || self.custom_formatting.is_some()
// || self.external_validation.is_some()
}
pub fn allows_field_switch(&self, text: &str) -> bool {
// Character limits validation
if let Some(ref limits) = self.character_limits {
if !limits.allows_field_switch(text) {
return false;
}
}
// Future: Add other validation types here
true
}
/// Get reason why field switching is blocked (if any)
pub fn field_switch_block_reason(&self, text: &str) -> Option<String> {
// Character limits validation
if let Some(ref limits) = self.character_limits {
if let Some(reason) = limits.field_switch_block_reason(text) {
return Some(reason);
}
}
// Future: Add other validation types here
None
}
}
#[cfg(test)]

View File

@@ -1,3 +1,4 @@
// src/validation/limits.rs
//! Character limits validation implementation
use crate::validation::ValidationResult;
@@ -250,6 +251,29 @@ impl CharacterLimits {
},
}
}
pub fn allows_field_switch(&self, text: &str) -> bool {
if let Some(min) = self.min_length {
let count = self.count(text);
// Allow switching if field is empty OR meets minimum requirement
count == 0 || count >= min
} else {
true // No minimum requirement, always allow switching
}
}
/// Get reason why field switching is not allowed (if any)
pub fn field_switch_block_reason(&self, text: &str) -> Option<String> {
if let Some(min) = self.min_length {
let count = self.count(text);
if count > 0 && count < min {
return Some(format!(
"Field must be empty or have at least {} characters (currently: {})",
min, count
));
}
}
None
}
}
impl Default for CharacterLimits {
@@ -362,4 +386,39 @@ mod tests {
assert_eq!(limits.status_text("12345678"), Some("8/10 (approaching limit)".to_string()));
assert_eq!(limits.status_text("1234567890x"), Some("11/10 (exceeded)".to_string()));
}
#[test]
fn test_field_switch_blocking() {
let limits = CharacterLimits::new_range(3, 10);
// Empty field: should allow switching
assert!(limits.allows_field_switch(""));
assert!(limits.field_switch_block_reason("").is_none());
// Field with content below minimum: should block switching
assert!(!limits.allows_field_switch("hi"));
assert!(limits.field_switch_block_reason("hi").is_some());
assert!(limits.field_switch_block_reason("hi").unwrap().contains("at least 3 characters"));
// Field meeting minimum: should allow switching
assert!(limits.allows_field_switch("hello"));
assert!(limits.field_switch_block_reason("hello").is_none());
// Field exceeding maximum: should still allow switching (validation shows error but doesn't block)
assert!(limits.allows_field_switch("this is way too long"));
assert!(limits.field_switch_block_reason("this is way too long").is_none());
}
#[test]
fn test_field_switch_no_minimum() {
let limits = CharacterLimits::new(10); // Only max, no minimum
// Should always allow switching when there's no minimum
assert!(limits.allows_field_switch(""));
assert!(limits.allows_field_switch("a"));
assert!(limits.allows_field_switch("hello"));
assert!(limits.field_switch_block_reason("").is_none());
assert!(limits.field_switch_block_reason("a").is_none());
}
}

View File

@@ -1,3 +1,4 @@
// src/validation/state.rs
//! Validation state management
use crate::validation::{ValidationConfig, ValidationResult};
@@ -174,7 +175,31 @@ impl ValidationState {
self.field_configs.len()
}
/// Get validation summary
/// Check if field switching is allowed for a specific field
pub fn allows_field_switch(&self, field_index: usize, text: &str) -> bool {
if !self.enabled {
return true;
}
if let Some(config) = self.field_configs.get(&field_index) {
config.allows_field_switch(text)
} else {
true // No validation configured, allow switching
}
}
/// Get reason why field switching is blocked (if any)
pub fn field_switch_block_reason(&self, field_index: usize, text: &str) -> Option<String> {
if !self.enabled {
return None;
}
if let Some(config) = self.field_configs.get(&field_index) {
config.field_switch_block_reason(text)
} else {
None // No validation configured
}
}
pub fn summary(&self) -> ValidationSummary {
let total_validated = self.validated_fields.len();
let errors = self.fields_with_errors().count();