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

Support for aliases / mapping between input keys and output fields #56

Open
LeartS opened this issue Jul 25, 2024 · 1 comment
Open

Comments

@LeartS
Copy link

LeartS commented Jul 25, 2024

It would be nice to be able to map input keys to different output keys, similar to the key: option of Parameter.

This would allow to easily convert between different field names across boundaries at data ingestion point.

For example, something like this (I'm using two different imaginary options in the example)

defmodule UserPayload do
  use Drops.Contract

  schema do
    %{
      required(:user_id, source_key: "userId") => string(),
      required(:surname, aliases: ["lastName", "familyName"]) => string()
    }
  end
end

Which would allow me to conform the input map (e.g. from Jason.decode)

{
  "userId": "user-123",
  "lastName": "Doe"
}

Into the output map:

%{
  user_id: "user-123",
  surname: "Doe"
}
@solnic
Copy link
Owner

solnic commented Aug 3, 2024

I'm not a fan of mixing schema definitions with data transformation to be honest. I do want to provide tools for converting data though. We can have Drops.Transformer or something that can perform key renaming and other types of common transformations. We could still provide a streamlined API for this, it's just that I don't want it to be mixed inside of schema DSL.

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