Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

field name alternatives #28

Open
PhilWaldmann opened this issue Aug 22, 2024 · 3 comments
Open

field name alternatives #28

PhilWaldmann opened this issue Aug 22, 2024 · 3 comments

Comments

@PhilWaldmann
Copy link

PhilWaldmann commented Aug 22, 2024

Hi,

thanks for this awesome library.
I saw that it's possible to define an alias for a field. Is it possible to have alternative input field names for a single field?

for example:

schema = %{
  reference_no: [type: :string, alternatives: [:referenceNo, :referenceno]]
}

Tarams.cast(%{"referenceNo": "1234"}, schema)
# {:ok, %{reference_no: "1234"}}
Tarams.cast(%{"referenceno": "1234"}, schema)
# {:ok, %{reference_no: "1234"}}
Tarams.cast(%{"reference_no": "1234"}, schema)
# {:ok, %{reference_no: "1234"}}
@bluzky
Copy link
Owner

bluzky commented Nov 4, 2024

HI @PhilWaldmann, sorry for late response.
Tarams provide from: option, but it limit to one alias.

schema = %{
  reference_no: [type: :string, from: :referenceNo]
}

Tarams.cast(%{"referenceNo": "1234"}, schema)

@PhilWaldmann
Copy link
Author

Hi @bluzky thanks. Already found it in the docs.
Would you be interested in a PR to have alternative input names that map to a single field (like in my example above)? There are definitely some edge cases to consider and I haven't looked at Taram's code in detail if it's even possible without changing too much.
Just let me know if you think that it makes sense. I'm happy to contribute!

@bluzky
Copy link
Owner

bluzky commented Nov 17, 2024

Hi, I don't think multiple alias is common case, because keys in params or json responses are fixed in most cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants