Skip to content

Commit

Permalink
Validate layout/page templates on context actions
Browse files Browse the repository at this point in the history
Instead of validating it on every form change
  • Loading branch information
leandrocp committed Sep 15, 2023
1 parent 4cdb3b9 commit 86c0fb1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/beacon/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,10 @@ defmodule Beacon.Content do

## Utils

defp do_validate_template(changeset, field, _format, nil = _template, _metadata) do
Changeset.add_error(changeset, field, "can't be blank", compilation_error: nil)
end

defp do_validate_template(changeset, field, :heex = _format, template, metadata) when is_binary(template) do
Changeset.validate_change(changeset, field, fn ^field, template ->
case Beacon.Template.HEEx.compile(template, metadata) do
Expand Down
2 changes: 1 addition & 1 deletion lib/beacon/content/layout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ defmodule Beacon.Content.Layout do
def changeset(%__MODULE__{} = layout, attrs) do
layout
|> cast(attrs, [:site, :title, :template, :meta_tags, :resource_links])
|> validate_required([:site, :title, :template])
|> validate_required([:site, :title])
end
end
2 changes: 0 additions & 2 deletions lib/beacon/content/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ defmodule Beacon.Content.Page do
|> unique_constraint([:path, :site])
|> validate_required([
:site,
:template,
:layout_id,
:format
])
Expand Down Expand Up @@ -119,7 +118,6 @@ defmodule Beacon.Content.Page do
|> unique_constraint([:path, :site])
|> validate_required([
:site,
:template,
:layout_id,
:format
])
Expand Down

0 comments on commit 86c0fb1

Please sign in to comment.