Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 25, 2024

Conversation

matteopolak
Copy link
Contributor

@matteopolak matteopolak commented Jul 14, 2024

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
Copy link
Owner

Keats commented Jul 16, 2024

Is it possible to add a test?

@matteopolak
Copy link
Contributor Author

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

  • Accidentally using ::validator::... instead of #crate_name::... (by the compile-fail)
  • Accidentally using validator::... instead of #crate_name::... (by the run-pass)

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

@matteopolak
Copy link
Contributor Author

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
Copy link
Owner

Keats commented Jul 23, 2024

It looks like ti still fails?

@matteopolak
Copy link
Contributor Author

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
Copy link
Owner

Keats commented Jul 23, 2024

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

@matteopolak
Copy link
Contributor Author

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

@Keats Keats merged commit c6c1808 into Keats:master Jul 25, 2024
0 of 4 checks passed
@matteopolak matteopolak deleted the feature/add_crate_alias branch December 4, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants