Regex Validator - how to do it right

I wondering how to do correct validate if returned uuid is in correct format.

I have created custom validator using Regex matches function.

What do you think about that:

package pl.gemiusz;

import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;

import java.util.regex.Pattern;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.http;

public class Case0020RegexValidatorSimulation extends Simulation {

    HttpProtocolBuilder httpProtocol =
            http
                    .baseUrl("https://postman-echo.com");

    Pattern uuidRegexCompiledPattern = Pattern.compile("^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$");
    ScenarioBuilder scn =
            scenario("GeMi_RegexValidatorSimulation")
                    .exec(
                            http("GeMi_RegexValidator_get_randomUuid")
                                    .get("/get?foo=#{randomUuid()}")
                                    .check(jmesPath("args.foo").validate(
                                            "UuidRegexValidator",
                                            (actual, session) -> {
                                                if (actual == null) {
                                                    throw new NullPointerException("Value is missing");
                                                } else if (!uuidRegexCompiledPattern.matcher(actual).matches()) {
                                                    throw new IllegalArgumentException("Value " + actual + " not match Regex compiled pattern " + uuidRegexCompiledPattern.pattern());
                                                }
                                                return actual;
                                            }).saveAs("get_randomUuid"))

                    )
                    .exec(session -> {
                        System.out.println("GeMi_RegexValidator_get_randomUuid: " + session.get("get_randomUuid"));
                        return session;
                    });

    {
        setUp(scn.injectOpen(atOnceUsers(1)).protocols(httpProtocol));
    }
}

I’m thinking of making a feature request for Regex Validator, but I’m not sure if anyone besides me will need it :slight_smile:
As I know not compiled regex patterns are slow so for this feature all will need to be compiled, I’m right? :slight_smile:

As I know not compiled regex patterns are slow so for this feature all will need to be compiled, I’m right?

Absolutely. You really want to compile the Pattern outside the function.

I’m thinking of making a feature request for Regex Validator, but I’m not sure if anyone besides me will need it :slight_smile:

I agree we shouldn’t make it a feature.
But I do think this could make a great example in the documentation.
Then, I don’t think UUID validation is a good use case for demonstrating regex based validation.

For UUID validation, I would write:

.validate(
  "UuidValidator",
  (actual, session) -> {
    // let it throw if actual is not a proper UUID
    UUID.fromString(actual)
    return actual;
  }
)

I was thinking about other user cases and nothing is so perfect :smiley: to add as a clear user case :confused:
Any propositions ?
I was thinking about e-mail, zip code, telephone number and checked other most used regex patterns and not found perfect as this :smiley:

credit card number 4x 4digits that may be separated or not by space, dot, or dash.

  • e-mail: do you really want to explain that:
/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|
\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|
\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
  • zip code: from which country? Some may accept letter, some only 4 digits, some with weird formats
  • telephone number: from which country (bis)? how many digits? spaces? parenthesis? dash? dots? and so on

Maybe something simple as just the presence/absence of a String inside a JSON value?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.