CodeSampleX

샘플

ecto 3.14.1: Validate a plain map with Ecto.Changeset alone: an embedded_schema, no Repo, no database and no ecto_sql

검증된 샘플 — hex ecto 3.14.1: Validate a plain map with Ecto.Changeset alone: an embedded_schema, no Repo, no database and no ecto_sql. elixir 1 · linux…

sha256:ec1c423e61b01693526ccce5e694e6c68ed968878f00b8331b3d82c953abea87

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
2
빌드한 서명 키
2
선언된 환경 elixir 1 linux x64 elixir 1 elixir mix

검증 실행 환경

환경 컨트랙트 단계 실행일
elixir 1 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:PASS · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · hex@1
2026-08-14
elixir 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:PASS · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · hex@1
2026-08-18

케이스

HOW
목표
Validate a plain map with Ecto.Changeset alone: an embedded_schema, no Repo, no database and no ecto_sql
패키지
심벌
  • Ecto.Schema.embedded_schema
  • Ecto.Changeset.cast
  • Ecto.Changeset.validate_required
  • Ecto.Changeset.validate_number
  • Ecto.Changeset.validate_format
  • Ecto.Changeset.validate_length
  • Ecto.Changeset.empty_values
  • Ecto.Changeset.field_missing?
  • Ecto.Changeset.apply_action
  • Ecto.Changeset.apply_changes
  • Ecto.Changeset.traverse_errors
  • Ecto.Changeset.put_change
  • Ecto.Changeset.force_change
  • Ecto.InvalidChangesetError
환경
elixir 1
생성일
2026-08-14T14:09:57Z

컨트랙트

  1. assert Ecto.Changeset validates with :ecto alone: ecto_sql is not installed and Ecto.Adapters.SQL will not load, while the embedded_schema reports no source table and no prefix where an otherwise identical schema/2 block reports its table name
  2. assert embedded_schema's own default primary key is an autogenerated binary_id that nothing outside a Repo insert ever fills in, so it stays nil, which is why the schema turns it off
  3. assert cast/4 keeps only the permitted fields and silently drops the rest, adding no error for a real-but-unpermitted field or for a key that is not a field at all, while changeset.params still holds the raw attempt and permitting that same field would have accepted it with no error either
  4. assert permitting a field the schema does not have raises ArgumentError, so a typo in the permitted list fails loudly while an unexpected param never does
  5. assert cast substitutes the field's default for an empty or whitespace-only string, so validate_required reports can't be blank, and that the substituted value is the declared default rather than nil, which silently resets a field that has one
  6. assert a value that survives the emptiness check is stored untrimmed, so only the check trims
  7. assert validate_required runs no blank check of its own and reuses changeset.empty_values, so casting with empty_values: [] lets an empty string pass validation while a validator that inspects the value still catches it
  8. assert passing a function in :empty_values now raises ArgumentError and the two-arity :trim_values option is the replacement
  9. assert validate_required decides presence with get_field, so a field carrying a non-nil schema default can never be reported missing, on a failing changeset or a passing one
  10. assert a type failure from cast is reported as is invalid with validation: :cast, leaves the field out of changes, and suppresses the later validators for that field
  11. assert apply_action/2 returns {:ok, struct} or {:error, changeset} with :action stamped onto the error changeset, apply_action!/2 raises Ecto.InvalidChangesetError, and apply_changes/1 returns the struct even when the changeset is invalid
  12. assert validate_number stores a %{number} placeholder plus interpolation metadata while validate_format's message is already complete, so a raw error message is not displayable
  13. assert traverse_errors interpolates and groups the repeated keys of changeset.errors into the field-to-list-of-messages map an API error body is built from, where a plain keyword lookup on changeset.errors returns only the most recent of a field's several errors
  14. assert put_change writes to changes regardless of the permitted list and regardless of the validators that already ran, is a no-op when the value equals the one on the data, and force_change/3 is the override

파일

  • csx.json
  • lib/csx_ecto/signup.ex
  • mix.exs
  • mix.lock
  • test/contract_test.exs
  • test/test_helper.exs

소스 아티팩트 내려받기 (tar.gz)

오리진 시더

anonymous