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

Dynamic key support in GuardedStruct #11

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

Dynamic key support in GuardedStruct #11

svsool opened this issue Feb 12, 2025 · 3 comments
Assignees

Comments

@svsool
Copy link

svsool commented Feb 12, 2025

Hi,

Are dynamic keys supported in GuardedStruct?

For example with such input:

   { 
      knownField1: { 
         knownField2: { 
            [dynamicKey1 ... dynamicKey100]: KnownStruct 
         },
      knownField3: ...,
      ...
   }

Would be nice to avoid using custom validator in such cases.

Thanks!

@shahryarjb shahryarjb transferred this issue from mishka-group/mishka_developer_tools Feb 12, 2025
@shahryarjb
Copy link
Member

Hi dear @svsool ,
Would you mind giving me a full scenario, please 🙏?
For example a complete map and how do you want to validate the Daynamic part?
It helpes me to recognize what solutions can be help full here.
By the way we separated the repo and please update your dep.
Thank you in advance

@svsool
Copy link
Author

svsool commented Feb 12, 2025

Hi @shahryarjb,

For example:

defmodule ShardStruct do
  use GuardedStruct

  guardedstruct do
    field(:node, String.t(), derive: "sanitize(trim) validate(ipv4)")
    
    # ... other fields here ...
  end
end

# map with dynamic keys
defmodule ShardsMapStruct do
  use GuardedStruct

  guardedstruct do
    # dynamic key ⬇️ <--- this feature basically would be great to have
    field(~r/\d+$/, struct(), struct: ShardStruct, derive: "validate(map, not_empty)")
  end
end

defmodule ShardingPlanStruct do
  use GuardedStruct

  guardedstruct do
    field(:shards_map, struct: ShardsMapStruct, enforce: true)

    # ... other plan fields here created_at, status etc ...
  end
end

In Ajv it's called patternProperties.

@shahryarjb
Copy link
Member

@svsool
It could be a great opportunity, but honestly, I don't think I'll be heading in that direction anytime soon. Right now, I'm really caught up with the Mishka Chelekom phoenix component library, and handling everything alone is quite tough.

I’m sorry to say this, but I might consider it in the future, or I’d be really happy if someone submits a pull request.

The macro's goal was to avoid being too dynamic and to have strict mode check the keys and build a schema.

But this feature also has its own use case and would be great to have if added.

Thanks for taking the time to explain.
Don’t forget to update to the latest version of this library!

{:guarded_struct, "~> 0.0.4"}

@shahryarjb shahryarjb self-assigned this Feb 12, 2025
@svsool svsool changed the title Question: Dynamic key support in GuardedStruct Dynamic key support in GuardedStruct Feb 12, 2025
# 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