Keats / validator

Simple validation for Rust structs
MIT License
2.07k stars 151 forks source link

feat: add `#[validator(crate = "...")]` for crate alias #339

Closed matteopolak closed 4 months ago

matteopolak commented 4 months ago

Currently it's not possible to package up the validator crate's derives without requiring users to include the dependency locally (cluttering up their deps list if they don't actually use it).

This PR adds an optional crate attribute that overrides all references to the crate with a new name.

validator_renamed = { package = "validator", version = "0.18", features = ["derive"] }
#[derive(validator_renamed::Validator)]
#[validate(crate = "validator_renamed")]
pub struct MyData {
  // ...
}

It also works with paths, e.g.

#[validate(crate = "some::other::location")]
Keats commented 4 months ago

Is it possible to add a test?

matteopolak commented 4 months ago

Added (also fixed the compile error). The tests cover:

It doesn't cover every branch, but I checked the ones for url, email, length, range, and custom (function)

matteopolak commented 4 months ago

Whoops, seems like that "private" type is now permitted on the nightly channel I was on, but not on 1.70. I shuffled it around so it could be pub to work around the issue, and tested it on 1.70 (test + build).

Keats commented 4 months ago

It looks like ti still fails?

matteopolak commented 4 months ago

The fail appears to be unrelated to this commit and more related to the version of nightly being used having changed the order of the note diagnostics:

EXPECTED:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
error: Invalid argument reference

         = note: The lifetime `'a` is not supported.
         = help: Please use the validator lifetime `'v_a`

 --> tests/compile-fail/custom/different_lifetime.rs:8:22
  |
8 | #[validate(context = "Arg<'a>")]
  |                      ^^^^^^^^^
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

ACTUAL OUTPUT:
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
error: Invalid argument reference
 --> tests/compile-fail/custom/different_lifetime.rs:8:22
  |
8 | #[validate(context = "Arg<'a>")]
  |                      ^^^^^^^^^
  |
  = note: The lifetime `'a` is not supported.
  = help: Please use the validator lifetime `'v_a`
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

Looks like it has placed them at the end instead of at the start for a lot of the expect-fail tests. It might be a good idea to pin to the last-working nightly, or maybe there's a way to specify different output for a different toolchain?

Keats commented 4 months ago

I think I should remove nightly entirely from the CI, it's always failing on there for one reason or another

matteopolak commented 4 months ago

Sounds like a good idea. Let me know if you need anything from me to get this merged!