ObjectValidators: fix validate_required on embeds_many fields

This commit is contained in:
Alex Gleason 2022-11-03 11:56:20 -05:00
parent d02c5f4480
commit 4576a5c440
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 5 additions and 7 deletions

View file

@ -45,9 +45,7 @@ def changeset(struct, data) do
struct struct
|> cast(data, [:id, :type, :mediaType, :name, :blurhash]) |> cast(data, [:id, :type, :mediaType, :name, :blurhash])
|> cast_embed(:url, with: &url_changeset/2) |> cast_embed(:url, with: &url_changeset/2, required: true)
|> validate_inclusion(:type, ~w[Link Document Audio Image Video])
|> validate_required([:type, :mediaType, :url])
end end
def url_changeset(struct, data) do def url_changeset(struct, data) do
@ -90,7 +88,7 @@ defp fix_url(data) do
defp validate_data(cng) do defp validate_data(cng) do
cng cng
|> validate_inclusion(:type, ~w[Document Audio Image Video]) |> validate_inclusion(:type, ~w[Link Document Audio Image Video])
|> validate_required([:mediaType, :url, :type]) |> validate_required([:type, :mediaType])
end end
end end

View file

@ -104,14 +104,14 @@ def changeset(struct, data) do
struct struct
|> cast(data, __schema__(:fields) -- [:attachment, :tag]) |> cast(data, __schema__(:fields) -- [:attachment, :tag])
|> cast_embed(:attachment) |> cast_embed(:attachment, required: true)
|> cast_embed(:tag) |> cast_embed(:tag)
end end
defp validate_data(data_cng) do defp validate_data(data_cng) do
data_cng data_cng
|> validate_inclusion(:type, ["Audio", "Video"]) |> validate_inclusion(:type, ["Audio", "Video"])
|> validate_required([:id, :actor, :attributedTo, :type, :context, :attachment]) |> validate_required([:id, :actor, :attributedTo, :type, :context])
|> CommonValidations.validate_any_presence([:cc, :to]) |> CommonValidations.validate_any_presence([:cc, :to])
|> CommonValidations.validate_fields_match([:actor, :attributedTo]) |> CommonValidations.validate_fields_match([:actor, :attributedTo])
|> CommonValidations.validate_actor_presence() |> CommonValidations.validate_actor_presence()