Set regex literal in attribute

Open
#149 3 comments 2 reactions 0 assignees View on GitHub

@rakshith-ravi is already working on this.

Since Apr 20, 2022.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
backend

Research direction

Start with the regex attribute handling and generated impl described in the issue. Compare the proposed lazy_static and re-export approaches, then define how string literals should be supported and what dependency behavior is acceptable; the work is done when the shown literal syntax is supported without requiring consumers to define a Regex variable.

Written by the indexing model from the issue text.

Description

Currently, the regex attribute accepts a path to a Regex variable, which would typically be created by something like lazy_static or OnceCell. It would be more convenient for consumers of this library if you could pass in a string literal to be used as a regex, e.g.

#[derive(Validate)]
pub struct Struct {
    #[validate(regex(pattern = r"^[Hh]ello\b"))]
    s: String,
}

I see this approach was considered in #1 but dropped as it was easier to just take a path to an existing variable.

  • use lazy_static inside the generated impl but that requires the end user adding lazy_static manually to their dependencies (afaik)

It could also be achieved by re-exporting lazy_static from the validator crate - potentially with a #[doc(hidden)] as it's just an implementation detail, not public API. In the future hopefully we'll have rust-lang/rust#74465 stable so we can stop using lazy_static entirely

Dominant language
Rust
Stars
2.5k
Forks
190
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Keats/validator

All issues in Keats/validator

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.