Allow regex(path = ...) value to be a non-string expression (#328)

This commit is contained in:
Graham Esau 2024-08-24 18:27:27 +01:00 committed by GitHub
parent dc1245bbd8
commit 66f17fff0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 64 additions and 69 deletions

View file

@ -190,12 +190,6 @@ impl Attrs {
_ if ignore_errors => {}
Meta::List(m) if m.path.is_ident("inner") && attr_type == "schemars" => {
// This will be processed with the validation attributes.
// It's allowed only for the schemars attribute because the
// validator crate doesn't support it yet.
}
_ => {
if !is_known_serde_or_validation_keyword(&meta_item) {
let path = meta_item

View file

@ -1,13 +1,14 @@
use super::{expr_as_lit_str, get_meta_items, parse_lit_into_path, parse_lit_str};
use super::{expr_as_lit_str, get_meta_items, parse_lit_str};
use proc_macro2::TokenStream;
use quote::ToTokens;
use serde_derive_internals::Ctxt;
use syn::{
parse::Parser, punctuated::Punctuated, Expr, ExprPath, Lit, Meta, MetaList, MetaNameValue, Path,
parse::Parser, punctuated::Punctuated, Expr, ExprLit, ExprPath, Lit, Meta, MetaList,
MetaNameValue, Path,
};
pub(crate) static VALIDATION_KEYWORDS: &[&str] = &[
"range", "regex", "contains", "email", "phone", "url", "length", "required",
"range", "regex", "contains", "email", "phone", "url", "length", "required", "inner",
];
#[derive(Debug, Clone, Copy, PartialEq)]
@ -214,8 +215,7 @@ impl ValidationAttrs {
(Some(_), _) => duplicate_error(&nv.path),
(None, Some(_)) => mutual_exclusive_error(&nv.path, "contains"),
(None, None) => {
self.regex =
parse_lit_into_expr_path(errors, attr_type, "regex", &nv.value).ok()
self.regex = parse_regex_expr(errors, nv.value);
}
}
}
@ -230,10 +230,7 @@ impl ValidationAttrs {
Meta::NameValue(MetaNameValue { path, value, .. })
if path.is_ident("path") =>
{
self.regex = parse_lit_into_expr_path(
errors, attr_type, "path", &value,
)
.ok()
self.regex = parse_regex_expr(errors, value);
}
Meta::NameValue(MetaNameValue { path, value, .. })
if path.is_ident("pattern") =>
@ -242,7 +239,7 @@ impl ValidationAttrs {
expr_as_lit_str(errors, attr_type, "pattern", &value)
.ok()
.map(|litstr| {
Expr::Lit(syn::ExprLit {
Expr::Lit(ExprLit {
attrs: Vec::new(),
lit: Lit::Str(litstr.clone()),
})
@ -316,7 +313,18 @@ impl ValidationAttrs {
}
},
_ => {}
_ if ignore_errors => {}
_ => {
if let Some(ident) = meta_item.path().get_ident() {
if VALIDATION_KEYWORDS.iter().any(|k| ident == k) {
errors.error_spanned_by(
&meta_item,
format!("could not parse `{ident}` item in schemars attribute"),
);
}
}
}
}
}
self
@ -405,21 +413,6 @@ impl ValidationAttrs {
}
}
fn parse_lit_into_expr_path(
cx: &Ctxt,
attr_type: &'static str,
meta_item_name: &'static str,
lit: &Expr,
) -> Result<Expr, ()> {
parse_lit_into_path(cx, attr_type, meta_item_name, lit).map(|path| {
Expr::Path(ExprPath {
attrs: Vec::new(),
qself: None,
path,
})
})
}
fn str_or_num_to_expr(cx: &Ctxt, meta_item_name: &str, expr: Expr) -> Option<Expr> {
// this odd double-parsing is to make `-10` parsed as an Lit instead of an Expr::Unary
let lit: Lit = match syn::parse2(expr.to_token_stream()) {
@ -445,3 +438,13 @@ fn str_or_num_to_expr(cx: &Ctxt, meta_item_name: &str, expr: Expr) -> Option<Exp
}
}
}
fn parse_regex_expr(cx: &Ctxt, value: Expr) -> Option<Expr> {
match value {
Expr::Lit(ExprLit {
lit: Lit::Str(litstr),
..
}) => parse_lit_str(&litstr).map_err(|e| cx.syn_error(e)).ok(),
v => Some(v),
}
}