Embed schema for base64 string

Hi guys.
I have input data with two fields:
data - base64 string
signature - signed data field.
Field data has predicted structure and I need to validate it.
I need combination of custom type (Base64) and embed schema.
Is it possible to create embed schema on base64 string?

Something like that:

  embedded_schema do
    embeds_one :data, Data, type: Base64 do
      field :id, Ecto.UUID
      field :password, :string
      field :date, :string
    field(:signature, :string)

Welcome to the forum

If you want or need UUID don’t generate it as a primary key instead use it like this https://dev.to/dreamingechoes/generate-uuid-fields-in-phoenix-with-postgresql--2a02

Also if you want to understand the reason why you shouldn’t use uuid as a primary key you can read this thread where i asked a lot of questions regarding this topic Best way to generate an UUID for the id field?

Hope it helps

Sorry, but I’m not asking about UUID.
My question is - can I use Base64 encoder for embed schema, so I can define embed schema on Base64 string

I know what you asked, I just wanted to point out if you are using uuid like you do now, then you will encounter some problems later on.

As you can see, I’m using embedded_schema, that not generates primary key.
It’s because this schema for request validation and will not used with ecto_sql.
I know that default UUID generates at compile-time, but this is not me case

So you need to validate a base64 using a custom rule changeset, because if not it’s not very clear what you are asking?

Also this links show how changesets can be used on embeded schemas, so it should offer you a starting point at least https://thoughtbot.com/blog/embedding-elixir-structs-in-ecto-models


I’d probably just use a custom type, without the embedded schema. AFAIK embeds can I can only be represented as json.

Or parse the base64 string before using the schema’s changesets and whatnot.

If you can give an example of input and desired output, that will help a lot.

I’m not quite sure what you’re asking yet – maybe that you want a struct that transparently encodes and decodes itself after a field is changed?

According to ecto’s documentation

You must declare your embeds_one/3 field with type :map at the database level.


  data: "BASE64({"id":"some-id","date":"2012-12-12","password":"some-pwd"})",
  signature: "some-signature"

So basically I need decode base64 to map and than work with it like it’s map

@PavelVesnin What is the actual database column?

@benwilson512 Seems like the schema will only be used for validation.

If you’re given a base64 encoded JSON string, you’re best off doing the decoding and json parsing yourself before passing it to Ecto:

data = string |> Base.decode64! |> Jason.decode!
# then validate
1 Like

it’s for request validation

that’s close to what I do.
I’ve created custom type Base64, that expects base64 string and decode it to map.
Then I’m using validate_change/3 for validation.
But I thought that maybe there is a more elegant way to do this.