drops icon indicating copy to clipboard operation
drops copied to clipboard

Support for aliases / mapping between input keys and output fields

Open LeartS opened this issue 1 year ago • 1 comments

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"
}

LeartS avatar Jul 25 '24 12:07 LeartS

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.

solnic avatar Aug 03 '24 06:08 solnic