Skip to content
New issue

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

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

Already on GitHub? # to your account

Nested lists validation issues #12

Open
svsool opened this issue Feb 12, 2025 · 4 comments
Open

Nested lists validation issues #12

svsool opened this issue Feb 12, 2025 · 4 comments
Assignees
Milestone

Comments

@svsool
Copy link

svsool commented Feb 12, 2025

Hi, it looks like there might be an issue with validating nested lists.

Repro:

defmodule NestedListStruct do
  use GuardedStruct

  guardedstruct do
    sub_field(:list, list(struct()),
      structs: true,
      derive: "validate(list, not_empty)",
      enforce: true
    ) do
      field(:id, String.t(), enforce: true)

      sub_field(:sublist, list(struct()),
        structs: true,
        derive: "validate(list, not_empty)",
        enforce: true
      ) do
        field(:id, String.t(), enforce: true)
      end
    end
  end
end

assert {:ok, list} = NestedListStruct.builder(
      %{
        "list" => [
          %{"id" => "1", "sublist" => [%{"id" => "2"}]}
        ]
      }
    )

# this error will be returned about missing required fields, but it's expected to work given struct definition
{:error, [%{field: :list, errors: [%{field: :sublist, errors: %{message: "Please submit required fields.", fields: [:id], action: :required_fields}}]}]}

Also if sublist is renamed to list in both places struct and input, another issue occurs vs working as expected:

{:error, [%{field: :list, errors: [%{field: :list, errors: [%{message: "The list field must be list", field: :list, action: :list}]}]}]}

@shahryarjb
Copy link
Member

shahryarjb commented Feb 12, 2025

Hi @svsool , Amm... ; I think it is kind a bug!! let me test it more. I hope it is not going to be hard part of macro !! without structs it works, but as a nested list ! no 😢

It seems the validation is working but sending data to inner module can not know about the map keys

I suggest test conditional_field instead for now

Thank you for report my friend

@shahryarjb shahryarjb added this to the 0.0.5 milestone Feb 12, 2025
@shahryarjb shahryarjb self-assigned this Feb 12, 2025
@svsool
Copy link
Author

svsool commented Feb 13, 2025

hi, conditional field did not work either unfortunately.

@shahryarjb
Copy link
Member

Hi @svsool, sorry you have this experience, the goal at first not support very nested options.

Please give me your code of conditional!!

By the way, we had an issue of nested conditional fields that I left it #7

I will try to fix the nested list field

@shahryarjb
Copy link
Member

Hello again, dear @svsool .

First of all, thank you for the report you submitted.

Unfortunately, this part has a lot of dependencies. I spent the whole day working on it, but the changes affect other parts of the code as well.

For this reason, it is recommended to use derive and a custom function. Otherwise, unfortunately, you might need to use other libraries that support this feature.

I won’t be able to follow up on this issue for now.

I’d still be happy if someone manages to solve the problem and submits a pull request.

This issue only occurs when dealing with nested lists. The macro cannot determine which loop it is in, preventing it from executing the specific data of that loop for the next condition.

Best of luck!

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

No branches or pull requests

2 participants