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

Can we get path for functions & regex instead of strings? #73

Open
Keats opened this issue May 1, 2019 · 4 comments
Open

Can we get path for functions & regex instead of strings? #73

Keats opened this issue May 1, 2019 · 4 comments

Comments

@Keats
Copy link
Owner

Keats commented May 1, 2019

Seeing https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#custom-attributes-accept-arbitrary-token-streams maybe we can?

The next branch is using attr_literals already

@Keats Keats changed the title Can we get path for custom & regex? Can we get path for custom & regex instead of strings? May 1, 2019
@Keats Keats changed the title Can we get path for custom & regex instead of strings? Can we get path for functions & regex instead of strings? May 1, 2019
@flappyBug
Copy link

flappyBug commented Jul 6, 2019

As for the link you given, I think it's much better change length and range syntax to below:

// for length from 5 to 10 (10 is excluded)
#[validate(length(5..10))]   

// for length from 5 to 10 (10 is included)
#[validate(length(5..=10))]

// for length equal to 5
#[validate(length(5))]

@Keats
Copy link
Owner Author

Keats commented Jul 6, 2019

Yes the issue wasn't clear :(

So right now for regex and functions, we take a path as a string, eg

#[validate(regex = "RE2")]

It would be neater if the user could write #[validate(regex = crate::users::USERNAME_REGEX)] for example so their IDE can help.

As for the length validator, that's interesting, I've created #76 to track it

@flappyBug
Copy link

I did some investigates on this. Here are what I found.

  1. The range notation (e.g. 5..=10) can't parsed as meta. That's to say: If we want to support this syntax, we can't use Attribute::parse_meta or Attribute::interpret_meta to get the structure. I think we need to interpret token streams manually.

  2. The syn crate don't support parse path as meta for now. But there is an issue tracking this. But we also could interpret the token streams manually.

  3. I just saw that every validator has an optional code and message field. If we want to adapt the range syntax, instead of #[validate(length(5..=10, code="foo", message="bar"))], there is another choice: #[validate(length(5..10), code="foo", message="bar")], e.g. move the message and code to outer scope. What do you think?

@Keats
Copy link
Owner Author

Keats commented Jul 7, 2019

  1. Probably not worth it then imo, unless someone really wants it
  2. Nice, going to be a change for a future version then.
  3. That doesn't work I think since you can have multiple validators on a given field and each can have their own code/message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants