Skip to content

Commit

Permalink
Merge pull request #147 from sohosai/feature/fix-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
appare45 authored Apr 5, 2024
2 parents 9256e46 + 8889dc3 commit 2608d32
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tower = { version = "0.4.13", features = ["util"] }
tower-http = { version = "0.5.1", features = ["cors", "trace"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
unicode-segmentation = "1.11.0"
uuid = { version = "1.7.0", features = ["v4", "serde"] }
url = "2.5.0"
percent-encoding = "2.3.1"
1 change: 1 addition & 0 deletions crates/sos24-domain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ getset.workspace = true
mockall.workspace = true
regex.workspace = true
thiserror.workspace = true
unicode-segmentation.workspace = true
uuid.workspace = true
url.workspace = true
percent-encoding.workspace = true
37 changes: 23 additions & 14 deletions crates/sos24-domain/src/entity/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;
use bitflags::bitflags;
use getset::Getters;
use thiserror::Error;
use unicode_segmentation::UnicodeSegmentation;

use crate::{ensure, impl_value_object};

Expand Down Expand Up @@ -280,7 +281,7 @@ impl<const MAXLEN: usize> BoundedString<MAXLEN> {
#[derive(Debug, Error)]
pub enum BoundedStringError {
#[error("Invalid character: `{0}`")]
InvalidCharacter(char),
InvalidCharacter(String),
#[error("Empty string is not allowed")]
Empty,
#[error("Too long (max: {0})")]
Expand All @@ -293,21 +294,28 @@ impl<const MAXLEN: usize> TryFrom<String> for BoundedString<MAXLEN> {
fn try_from(value: String) -> Result<Self, Self::Error> {
let mut length = 0; // 文字列長を3倍してカウントする

for c in value.chars() {
if emojis::get(&c.to_string()).is_some() {
return Err(BoundedStringError::InvalidCharacter(c));
let is_small = |c: char| match c {
'\u{0021}'..='\u{007E}' // 半角英数字・記号
| '\u{FF10}'..='\u{FF19}' // 全角数字
| '\u{FF21}'..='\u{FF3A}' // 全角英語(大文字)
| '\u{FF41}'..='\u{FF5A}' // 全角英語(小文字)
=> true,
_ => false,
};

for grapheme_cluster in value.graphemes(true) {
if emojis::get(grapheme_cluster).is_some() {
return Err(BoundedStringError::InvalidCharacter(
grapheme_cluster.to_string(),
));
}

let char_length = match c {
'\u{0021}'..='\u{007E}' // 半角英数字・記号
| '\u{FF10}'..='\u{FF19}' // 全角数字
| '\u{FF21}'..='\u{FF3A}' // 全角英語(大文字)
| '\u{FF41}'..='\u{FF5A}' // 全角英語(小文字)
=> 2,
_ => 3,
};

length += char_length;
let mut chars = grapheme_cluster.chars();
let is_small_char = chars
.next()
.map(|c| is_small(c) && chars.next().is_none())
.unwrap_or(false);
length += if is_small_char { 2 } else { 3 };
}

if length == 0 {
Expand Down Expand Up @@ -422,5 +430,6 @@ mod tests {
assert!(ProjectTitle::try_from(format!("{kana18}AAAA")).is_err());

assert!(ProjectTitle::try_from("🙂".to_string()).is_err());
assert!(ProjectTitle::try_from("企画名#️⃣appare".to_string()).is_err());
}
}

0 comments on commit 2608d32

Please sign in to comment.