Query parameter in url and email suffix validation

Dear All,

I have the below two urls in my project:-

Url1 - https://localhost:4443/register

Url2 - https://localhost:4443/register?scheme=tesco

If Url1 is used by a user while registration then we must verify the user with his email suffix (should have @tesco.com in the email address).

If Url2 is used then user is verified with his url query parameter (scheme=tesco) and then his email suffix can be anything (no restriction with email suffix)

I am struggling to make both the cases work together. Can someone please help me here. It would be of great help for me.

Happy to help. The best place to start would be if we can see what you’ve tried so far, can you post your code? The more code the better!

Thanks Ben for the reply.

Url2 requirement, I am able to achieve as I am verifying the scheme parameter in the url from the database and allowing user to proceed with any email suffix. Code snippet below:-

def partner_intro(%{request_path: path} = conn, %{“scheme” => scheme_value} = params, _guest)

  when path == "/register" do

query = Repo.all(from u in PensionSchemeFees, select: u.scheme)

if Enum.member?(query, scheme_value) do

.>>>

Struggling to fit in the same module the url1 suffix validation condition.

I have a module for email suffix validation like below where I have the email pattern mentioned.
But the problem is if I apply this ‘validate_email_domain’ module in my changeset, then it is applied for both Url1 and Url2. I do not want any email address restrictions for Url2 case. Only want the restriction for Url1 case.

def validate_email_domain(changeset, field, opts \ []) do

pattern = ~r/|@tesco.com/

Please note the site uses Markdown,

You should enclose code with ```

You struggle with the case where there is no scheme params?

Maybe add this after your first match?!

def partner_intro(%{request_path: path} = conn, params, _guest) when path == "/register" do
  ...
end

Thanks, I can add another module as you mentioned.
But how to make the ‘validate_email_domain’ condition apply to one case and not to apply for another case.

Below is my one common changeset which has to be used for both the cases:-

def email_changeset(struct, params \\ %{}) do

   struct

   |> cast(params, [:email])

   |> validate_required(:email)

   |> validate_unique_field(:email)

   |> validate_email_format(:email)

   |> validate_email_domain(:email)

 end

Is there any way to exclude the ‘validate_email_domain’ condition in one case and include in another case.

If it is to separate case, I would not mind having separate changeset.

This is the descriptive way…

or add a parameter when domain needs to be checked, and use a condition.

Can you please give some example of ‘add a parameter when domain needs to be checked, and use a condition’.
I am very new to elixir and phoenix web development.

Any help here can you provide please

You can differentiate case by pattern matching on param scheme (or not)… then I advise You to have to different changesets for each cases.

If You prefer, add a parameter when You call email_changeset… like should_verify_email and use a condition in the pipeline.

But I would try to avoid that, and use pattern match instead.

Thanks, I have created two separate changesets now.
if possible, can you please give sample code for differentiating case by pattern matching on param scheme (or not).

I did in my first post.