Fix regex attribute when it uses dereferencing

This commit is contained in:
Graham Esau 2024-08-27 19:26:02 +01:00
parent ae2b5f16c0
commit d7ff81de96
2 changed files with 12 additions and 4 deletions

View file

@ -3,8 +3,16 @@ use schemars::JsonSchema;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use util::*; use util::*;
struct FakeRegex(&'static str);
impl std::fmt::Display for FakeRegex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
// In real code, this would typically be a Regex, potentially created in a `lazy_static!`. // In real code, this would typically be a Regex, potentially created in a `lazy_static!`.
static STARTS_WITH_HELLO: &str = r"^[Hh]ello\b"; static STARTS_WITH_HELLO: &FakeRegex = &FakeRegex(r"^[Hh]ello\b");
const MIN: u32 = 1; const MIN: u32 = 1;
const MAX: u32 = 1000; const MAX: u32 = 1000;
@ -16,7 +24,7 @@ pub struct Struct {
min_max: f32, min_max: f32,
#[validate(range(min = "MIN", max = "MAX"))] #[validate(range(min = "MIN", max = "MAX"))]
min_max2: f32, min_max2: f32,
#[validate(regex(path = &*STARTS_WITH_HELLO))] #[validate(regex(path = *STARTS_WITH_HELLO))]
regex_str1: String, regex_str1: String,
#[validate(regex(path = "STARTS_WITH_HELLO", code = "foo"))] #[validate(regex(path = "STARTS_WITH_HELLO", code = "foo"))]
regex_str2: String, regex_str2: String,
@ -63,7 +71,7 @@ pub struct Struct2 {
#[schemars(range(min = "MIN", max = "MAX"))] #[schemars(range(min = "MIN", max = "MAX"))]
min_max2: f32, min_max2: f32,
#[validate(regex(path = overridden))] #[validate(regex(path = overridden))]
#[schemars(regex(pattern = &*STARTS_WITH_HELLO))] #[schemars(regex(pattern = *STARTS_WITH_HELLO))]
regex_str1: String, regex_str1: String,
#[schemars(regex(pattern = r"^\d+$"))] #[schemars(regex(pattern = r"^\d+$"))]
regex_str2: String, regex_str2: String,

View file

@ -69,7 +69,7 @@ impl ValidationAttrs {
if let Some(regex) = &self.regex { if let Some(regex) = &self.regex {
mutators.push(quote! { mutators.push(quote! {
schemars::_private::insert_validation_property(#mut_ref_schema, "string", "pattern", #regex.to_string()); schemars::_private::insert_validation_property(#mut_ref_schema, "string", "pattern", (#regex).to_string());
}); });
} }