Skip to content

Commit

Permalink
use JsonArrayMap validation
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Nov 9, 2023
1 parent d7f34ea commit e1ebb56
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 0 additions & 10 deletions lib/beacon/content/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ defmodule Beacon.Content.Page do
:format
])
|> validate_string([:path])
|> validate_raw_schema(attrs["raw_schema"])
|> remove_all_newlines([:description])
|> remove_empty_meta_attributes(:meta_tags)
|> Content.PageField.apply_changesets(page.site, extra_attrs)
Expand Down Expand Up @@ -173,13 +172,4 @@ defmodule Beacon.Content.Page do
|> Enum.reject(fn {_key, value} -> is_nil(value) || String.trim(value) == "" end)
|> Map.new()
end

defp validate_raw_schema(changeset, raw_schema) do
raw_schema = if raw_schema in ["", nil], do: "[]", else: raw_schema

case Jason.decode(raw_schema) do
{:ok, raw_schema} -> put_change(changeset, :raw_schema, raw_schema)
{:error, _} -> add_error(changeset, :raw_schema, "invalid schema")
end
end
end
18 changes: 18 additions & 0 deletions test/beacon/content_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ defmodule Beacon.ContentTest do
raw_schema: [%{"foo" => "bar"}]
})
end

test "validate raw_schema" do
layout = layout_fixture(site: :raw_schema_test)

assert {:error,
%{
errors: [
raw_schema: {"expected a list of map or a map, got: [nil]", [type: Beacon.Types.JsonArrayMap, validation: :cast]}
]
}} =
Content.create_page(%{
site: "my_site",
path: "/",
template: "<p>page</p>",
layout_id: layout.id,
raw_schema: [nil]
})
end
end

describe "snippets" do
Expand Down

0 comments on commit e1ebb56

Please sign in to comment.