Keats/validator

Regression with `regex`es on 0.17

Closed this issue · 8 comments

The regex validator had a regression with version 0.17 when using the lazy_static crate.

While on 0.16 it works perfectly well, when updating to 0.17 it no longer works, with the following error message: Unexpected literal type `string`

Reproducing

Using these crates:

  • lazy_static version 1.4.0
  • regex version 1.10.3
  • validator version 0.16.1/0.17.0

The following code will compile on 0.16 but won't in 0.17:

use validator::Validate;

lazy_static::lazy_static! {
    static ref CATEGORY_NAME: regex::Regex = regex::Regex::new(r"^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$").unwrap();
}

#[derive(Debug, Clone, PartialEq, Validate)]
pub struct Category {
    #[validate(regex = "CATEGORY_NAME")]
    pub name: Option<String>,
}

Ah yes it was not mentioned. You now use the path rather than a string of the path.
You now have to use it like so:

#[derive(Debug, Clone, PartialEq, Validate)]
pub struct Category {
    #[validate(regex(path = *crate::CATEGORY_NAME))]
    pub name: Option<String>,
}

Have the same test case, but regex is not being actually validated. Other validations work well.
v0.18 just updated.
If needed more context or example snippet, I'll send here.

Yes please add some snippet. The syntax on the attribute has changed so it might be that like for the OP

sorry, man! it seemed cause of some deps cache problem when updating Cargo.toml. A new project from scratch is ok using #[validate(regex(path = *STATIC_VALUE))] with both lazy_static and OnceCell. Just run cargo clean in my prev project and everything is working properly. 💪 Dunno why anyway 🤔

Just one more thing!
It seems that regex pattern does not appear anymore automatically in generated schema, but only if I specify
#[schemars(regex = "STATIC_VALUE")] along with validate macro, got from this docs here https://graham.cool/schemars/deriving/attributes/#regex

Just one more thing! It seems that regex pattern does not appear anymore automatically in generated schema, but only if I specify #[schemars(regex = "STATIC_VALUE")] along with validate macro, got from this docs here https://graham.cool/schemars/deriving/attributes/#regex

Maybe we can open new issue for this? Or in schemars with a ref here?

I don't know what schemars is so it should probably be on their repo

I'm getting a similar error after upgrading to 18.1. validator = { version = "0.18.1", features = [ "derive" ] }, lazy_static = 1.4.0, regex = 1.10.5

lazy_static! {
    pub static ref NO_WHITE_SPACE: Regex = Regex::new(r"^[^\s]+(\s+[^\s]+)*$").unwrap();
}
#[derive(Debug, Deserialize, Validate, Serialize)]
pub struct Login {
    #[validate(
        required,
        length(min = 1, message = "Enter your Username"),
        regex(path = *NO_WHITE_SPACE, message = "Spaces aren't allowed")
    )]
    pub username: Option<String>,
}
error: Unexpected literal type `path`
   --> /home/simanacci/.cargo/registry/src/index.crates.io-6f17d22bba15001f/validator_derive-0.18.1/src/lib.rs:228:22
    |
228 | #[darling(and_then = ValidationData::validate)]
    |                      ^^^^^^^^^^^^^^

   Compiling dummy v0.7.0
error[E0599]: no function or associated item named `from_derive_input` found for struct `ValidationData` in the current scope
   --> /home/simanacci/.cargo/registry/src/index.crates.io-6f17d22bba15001f/validator_derive-0.18.1/src/lib.rs:288:49
    |
229 | struct ValidationData {
    | --------------------- function or associated item `from_derive_input` not found for this struct
...
288 |     let validation_data = match ValidationData::from_derive_input(&input) {
    |                                                 ^^^^^^^^^^^^^^^^^ function or associated item not found in `ValidationData`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `from_derive_input`, perhaps you need to implement it:
            candidate #1: `FromDeriveInput`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `validator_derive` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...