diff --git a/CHANGELOG.md b/CHANGELOG.md index bfd6a65..93be64f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ## Next * Fixes `TryFrom<&str>` for a string-constrained newtype over a string-like native type +* A string constraint's patterns are compiled with the ECMA-262 `u` flag, as + JSON Schema specifies, so `\p{..}` and `\u{..}` escapes work; a pattern with + an identity escape the `u` flag forbids now fails in the consumer's build * A string-constrained newtype whose inner type is neither `String` nor `FromStr` is rejected at `finalize` rather than in the consumer's build * Schemas for tuple structs with a direct standard `Vec` remainder now describe diff --git a/typespace/src/build/structs.rs b/typespace/src/build/structs.rs index 69d7eb7..8d14f20 100644 --- a/typespace/src/build/structs.rs +++ b/typespace/src/build/structs.rs @@ -2587,7 +2587,7 @@ impl NewtypeStruct { quote! { static PATTERN: ::std::sync::LazyLock<#regress::Regex> = ::std::sync::LazyLock::new(|| { - #regress::Regex::new(#p).unwrap() + #regress::Regex::with_flags(#p, "u").unwrap() }); if PATTERN.find(value).is_none() { return Err(#err.into()); diff --git a/typespace/tests/output/string_constraint_over_string_like_native.rs b/typespace/tests/output/string_constraint_over_string_like_native.rs index ea0ba98..a3f49ae 100644 --- a/typespace/tests/output/string_constraint_over_string_like_native.rs +++ b/typespace/tests/output/string_constraint_over_string_like_native.rs @@ -26,7 +26,7 @@ impl ::std::convert::TryFrom<&str> for AnchoredUrl { value: &str, ) -> ::std::result::Result { static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| - { ::regress::Regex::new("^https://").unwrap() }); + { ::regress::Regex::with_flags("^https://", "u").unwrap() }); if PATTERN.find(value).is_none() { return Err("doesn't match pattern \"^https://\"".into()); } diff --git a/typespace/tests/output/string_constraint_patterns_have_unicode_semantics.rs b/typespace/tests/output/string_constraint_patterns_have_unicode_semantics.rs new file mode 100644 index 0000000..461857b --- /dev/null +++ b/typespace/tests/output/string_constraint_patterns_have_unicode_semantics.rs @@ -0,0 +1,80 @@ +#[derive(::serde::Serialize)] +#[serde(transparent)] +pub struct Word(::std::string::String); +impl ::std::ops::Deref for Word { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl ::std::convert::From for ::std::string::String { + fn from(value: Word) -> Self { + value.0 + } +} +impl ::std::convert::TryFrom<&str> for Word { + type Error = self::error::ConversionError; + fn try_from( + value: &str, + ) -> ::std::result::Result { + static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| + { ::regress::Regex::with_flags("^\\p{Letter}+\\u{1F354}?$", "u").unwrap() }); + if PATTERN.find(value).is_none() { + return Err("doesn't match pattern \"^\\p{Letter}+\\u{1F354}?$\"".into()); + } + Ok(Self(value.to_string())) + } +} +impl ::std::convert::TryFrom<::std::string::String> for Word { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value.as_str()) + } +} +impl<'de> ::serde::Deserialize<'de> for Word { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + ::std::convert::TryFrom::try_from( + ::std::string::String::deserialize(deserializer)?, + ) + .map_err(|e: self::error::ConversionError| { + ::custom(e.to_string()) + }) + } +} +/// Error types. +pub mod error { + /// Error from a `TryFrom` or `FromStr` implementation. + pub struct ConversionError(::std::borrow::Cow<'static, str>); + impl ::std::error::Error for ConversionError {} + impl ::std::fmt::Display for ConversionError { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Display::fmt(&self.0, f) + } + } + impl ::std::fmt::Debug for ConversionError { + fn fmt( + &self, + f: &mut ::std::fmt::Formatter<'_>, + ) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Debug::fmt(&self.0, f) + } + } + impl From<&'static str> for ConversionError { + fn from(value: &'static str) -> Self { + Self(value.into()) + } + } + impl From for ConversionError { + fn from(value: String) -> Self { + Self(value.into()) + } + } +} diff --git a/typespace/tests/output/test_crate_path_overrides.rs b/typespace/tests/output/test_crate_path_overrides.rs index 2c9da33..ee7475d 100644 --- a/typespace/tests/output/test_crate_path_overrides.rs +++ b/typespace/tests/output/test_crate_path_overrides.rs @@ -18,7 +18,7 @@ impl ::std::convert::TryFrom<&str> for Code { value: &str, ) -> ::std::result::Result { static PATTERN: ::std::sync::LazyLock = ::std::sync::LazyLock::new(|| - { super::regex_engine::Regex::new("^x").unwrap() }); + { super::regex_engine::Regex::with_flags("^x", "u").unwrap() }); if PATTERN.find(value).is_none() { return Err("doesn't match pattern \"^x\"".into()); } diff --git a/typespace/tests/output/test_render_constrained_newtype_string.rs b/typespace/tests/output/test_render_constrained_newtype_string.rs index 2059db4..e9dcd5e 100644 --- a/typespace/tests/output/test_render_constrained_newtype_string.rs +++ b/typespace/tests/output/test_render_constrained_newtype_string.rs @@ -49,14 +49,14 @@ impl ::std::convert::TryFrom<&str> for ConstrainedString { } { static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| - { ::regress::Regex::new("^a").unwrap() }); + { ::regress::Regex::with_flags("^a", "u").unwrap() }); if PATTERN.find(value).is_none() { return Err("doesn't match pattern \"^a\"".into()); } } { static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| - { ::regress::Regex::new("k$").unwrap() }); + { ::regress::Regex::with_flags("k$", "u").unwrap() }); if PATTERN.find(value).is_none() { return Err("doesn't match pattern \"k$\"".into()); } diff --git a/typespace/tests/render.rs b/typespace/tests/render.rs index 56e57e2..95630f8 100644 --- a/typespace/tests/render.rs +++ b/typespace/tests/render.rs @@ -6801,6 +6801,52 @@ fn array_constraints_need_a_sequence_inner_type() { ); } +// JSON Schema patterns carry ECMA-262 unicode semantics (the `u` flag), +// so a unicode property escape matches characters, not bytes, and a +// `\u{..}` escape names a code point. +#[test] +fn string_constraint_patterns_have_unicode_semantics() { + let mut builder = TypespaceBuilder::new( + Settings::minimal() + .with_required_trait(TypespaceTrait::Serialize) + .with_required_trait(TypespaceTrait::Deserialize), + ); + builder.insert("string".to_string(), Type::String).unwrap(); + builder + .insert( + "Word".to_string(), + Type::NewtypeStruct( + NewtypeStruct::new("string".to_string()) + .name("Word") + .constraints(NewtypeConstraints::String { + min: None, + max: None, + patterns: vec![r"^\p{Letter}+\u{1F354}?$".to_string()], + }), + ), + ) + .unwrap(); + + let ts = builder.finalize(no_cycles).unwrap(); + let out = common::codespace(&ts).into_stream(); + + #[check_and_include( + "tests/output/string_constraint_patterns_have_unicode_semantics.rs", + out + )] + fn inner() { + use import::*; + + assert!(Word::try_from("h\u{e9}llo").is_ok()); + assert!(Word::try_from("h\u{e9}llo\u{1F354}").is_ok()); + assert!(Word::try_from("123").is_err()); + assert!( + Word::try_from("\u{1F354}").is_err(), + "the letters are required" + ); + } +} + // A string constraint checks a `&str` and then constructs the inner value // from it, which takes `String` or `FromStr`. A native that declares // neither is rejected at finalize rather than in the consumer's build.