↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Let's Code: Contact Form in Phoenix -- The Schema

The post walks through the schema used to model a contact form message in a Phoenix 1.4 app. It starts from a typespec, then uses an embedded Ecto schema so the message can be validated and shaped without being stored in the database.

Reading notes
#

  • It models a support message with email, subject, and body fields inside a Support context.
  • The module begins with @moduledoc false because it is meant to stay internal to the application.
  • The initial @type t documents the structure, but the module still needs a schema to compile.
  • embedded_schema is used because the message is not intended to be persisted anywhere.
  • The schema defines email, subject, and body as string fields.
  • The author stubs tests first, with cases for valid fields, missing required fields, and invalid email format.
  • The first changeset step uses cast to allow only email, subject, and body from the input map.
  • validate_required makes all three fields mandatory.
  • validate_format checks that the email matches a simple pattern.
  • The final result is a Message module with an embedded schema and a changeset function that casts, requires, and validates the fields.