Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor(editor-validator): improve error hint#294

Merged
mydearxym merged 1 commit intodevfromimprove-editor-validator
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletionslib/helper/converter/editor_to_html/validator/index.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,10 +6,13 @@ defmodule Helper.Converter.EditorToHTML.Validator do
alias Validator.Schema
alias Converter.EditorToHTML.Validator.EditorSchema

# blocks with no children items
@normal_blocks ["header", "paragraph", "quote"]
# blocks with "items" fields
@complex_blocks ["list", "table"]

# blocks with "items" fields (has many children item)
@children_blocks ["list", "table"]

# all the supported blocks
@supported_blocks @normal_blocks ++ @children_blocks

@spec is_valid(map) :: {:error, map} | {:ok, :pass}
def is_valid(data) when is_map(data) do
Expand DownExpand Up@@ -60,14 +63,18 @@ defmodule Helper.Converter.EditorToHTML.Validator do

# validate block which has mode and items
defp validate_block(%{"type" => type, "data" => data})
when type in @complex_blocks do
when type in @children_blocks do
[parent: parent_schema, item: item_schema] = EditorSchema.get(type)
validate_with(type, parent_schema, item_schema, data)
end

defp validate_block(%{"type" => type}), do: raise("undown #{type} block")
defp validate_block(%{"type" => type}) do
raise("undown #{type} block, supported blocks: #{@supported_blocks |> Enum.join(" | ")}")
end

defp validate_block(e), do: raise("undown block: #{e}")
defp validate_block(e) do
raise("undown block: #{e}, supported blocks: #{@supported_blocks |> Enum.join(" | ")}")
end

# validate with given schema
defp validate_with(block, schema, data) do
Expand Down
4 changes: 2 additions & 2 deletionstest/helper/converter/editor_to_html_test/index_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do
assert {:ok, _} = Parser.to_html(editor_string)
end

@tag :wip
@tag :wip2
test "invalid editorjs json fmt should raise error" do
editor_json = %{
"invalid_time" => 1_567_250_876_713,
Expand DownExpand Up@@ -100,7 +100,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do
{:ok, editor_string} = Jason.encode(editor_json)
{:error, error} = Parser.to_html(editor_string)

assert error =="undown block: 1"
assertString.contains?(error,"undown block: 1")
end

test "real-world editor.js data should work" do
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp