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.

Commit23085c8

Browse files
committed
refactor(editor): re-org / rename the schema validator
1 parent89c2e2a commit23085c8

File tree

5 files changed

+47
-57
lines changed

5 files changed

+47
-57
lines changed

‎lib/helper/converter/editor_to_html/validator/schema.ex‎renamed to ‎lib/helper/converter/editor_to_html/validator/editor_schema.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmoduleHelper.Converter.EditorToHTML.Validator.Schemado
1+
defmoduleHelper.Converter.EditorToHTML.Validator.EditorSchemado
22
@moduledocfalse
33

44
# header

‎lib/helper/converter/editor_to_html/validator/index.ex‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
defmoduleHelper.Converter.EditorToHTML.Validatordo
22
@moduledocfalse
33

4-
aliasHelper.{Utils,ValidateBySchema}
4+
aliasHelper.{Converter,Validator}
55

6-
aliasHelper.Converter.EditorToHTML.Validator.Schema
6+
aliasValidator.Schema
7+
aliasConverter.EditorToHTML.Validator.EditorSchema
78

89
# blocks with no children items
910
@simple_blocks["header","paragraph"]
@@ -22,14 +23,14 @@ defmodule Helper.Converter.EditorToHTML.Validator do
2223
einRuntimeError->
2324
format_parse_error(e)
2425

25-
e->
26+
_->
2627
format_parse_error()
2728
end
2829
end
2930
end
3031

3132
defpvalidate_editor_fmt(data)do
32-
validate_with("editor",Schema.get("editor"),data)
33+
validate_with("editor",EditorSchema.get("editor"),data)
3334
end
3435

3536
defpvalidate_blocks([]),do:{:ok,:pass}
@@ -45,19 +46,19 @@ defmodule Helper.Converter.EditorToHTML.Validator do
4546

4647
# validate block which have no nested items
4748
defpvalidate_block(%{"type"=>type,"data"=>data})whentypein@simple_blocksdo
48-
validate_with(type,Schema.get(type),data)
49+
validate_with(type,EditorSchema.get(type),data)
4950
end
5051

5152
# validate block which has mode and items
52-
defpvalidate_block(%{"type"=>type,"data"=>%{"mode"=>mode,"items"=>items}=data})
53+
defpvalidate_block(%{"type"=>type,"data"=>%{"mode"=>_,"items"=>_}=data})
5354
whentypein@complex_blocksdo
54-
[parent:parent_schema,item:item_schema]=Schema.get(type)
55+
[parent:parent_schema,item:item_schema]=EditorSchema.get(type)
5556
validate_with(type,parent_schema,item_schema,data)
5657
end
5758

5859
defpvalidate_block(%{"type"=>"code"})do
5960
# schema = %{text: [:string]}
60-
# caseValidateBySchema.cast(schema, data) do
61+
# caseSchema.cast(schema, data) do
6162
# {:error, errors} ->
6263
# format_parse_error("paragraph", errors)
6364

@@ -72,7 +73,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
7273

7374
# validate with given schema
7475
defpvalidate_with(block,schema,data)do
75-
caseValidateBySchema.cast(schema,data)do
76+
caseSchema.cast(schema,data)do
7677
{:error,errors}->
7778
format_parse_error(block,errors)
7879

@@ -82,7 +83,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
8283
end
8384

8485
defpvalidate_with(block,parent_schema,item_schema,data)do
85-
caseValidateBySchema.cast(parent_schema,data)do
86+
caseSchema.cast(parent_schema,data)do
8687
{:error,errors}->
8788
format_parse_error(block,errors)
8889

@@ -93,7 +94,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
9394
%{"mode"=>mode,"items"=>items}=data
9495

9596
Enum.each(items,fnitem->
96-
caseValidateBySchema.cast(item_schema,item)do
97+
caseSchema.cast(item_schema,item)do
9798
{:error,errors}->
9899
{:error,message}=format_parse_error("#{block}(#{mode})",errors)
99100
raise%MatchError{term:{:error,message}}
@@ -106,16 +107,6 @@ defmodule Helper.Converter.EditorToHTML.Validator do
106107
{:ok,:pass}
107108
end
108109

109-
# check if the given map has the right key-value fmt of the editorjs structure
110-
defpis_valid_editorjs_fmt(map)whenis_map(map)do
111-
Map.has_key?(map,"time")and
112-
Map.has_key?(map,"version")and
113-
Map.has_key?(map,"blocks")and
114-
is_list(map["blocks"])and
115-
is_binary(map["version"])and
116-
is_integer(map["time"])
117-
end
118-
119110
defpformat_parse_error(type,error_list)whenis_list(error_list)do
120111
{:error,
121112
Enum.map(error_list,fnerror->

‎lib/helper/validate_by_schema.ex‎renamed to ‎lib/helper/validator/schema.ex‎

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
defmoduleHelper.ValidateBySchema.Matchersdo
2-
@moduledoc"""
3-
matchers for basic type, support required option
4-
"""
5-
6-
defmacro__using__(types)do
7-
# can not use Enum.each here, see https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654/4
8-
fortype<-typesdo
9-
guard_name=iftype==:string,do:"is_binary",else:"is_#{to_string(type)}"
10-
11-
quotedo
12-
defpmatch(field,nil,[unquote(type),required:false]),do:done(field,nil)
13-
14-
defpmatch(field,value,[unquote(type),required:false])
15-
whenunquote(:"#{guard_name}")(value)do
16-
done(field,value)
17-
end
18-
19-
defpmatch(field,value,[unquote(type)])whenunquote(:"#{guard_name}")(value),
20-
do:done(field,value)
21-
22-
defpmatch(field,value,[unquote(type),required:false]),
23-
do:error(field,value,unquote(type))
24-
25-
defpmatch(field,value,[unquote(type)]),do:error(field,value,unquote(type))
26-
end
27-
end
28-
end
29-
end
30-
31-
defmoduleHelper.ValidateBySchemado
1+
defmoduleHelper.Validator.Schemado
322
@moduledoc"""
333
validate json data by given schema, mostly used in editorjs validator
344
355
currently support boolean / string / number / enum
366
"""
377

8+
useHelper.Validator.Schema.Matchers,[:string,:number,:list,:boolean]
9+
3810
@doc"""
3911
cast data by given schema
4012
@@ -49,10 +21,8 @@ defmodule Helper.ValidateBySchema do
4921
}
5022
5123
data = %{checked: true, label: "done"}
52-
ValidateBySchema.cast(schema, data)
24+
Schema.cast(schema, data)
5325
"""
54-
useHelper.ValidateBySchema.Matchers,[:string,:number,:list,:boolean]
55-
5626
defcast(schema,data)do
5727
errors_info=cast_errors(schema,data)
5828

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmoduleHelper.Validator.Schema.Matchersdo
2+
@moduledoc"""
3+
matchers for basic type, support required option
4+
"""
5+
6+
defmacro__using__(types)do
7+
# can not use Enum.each here, see https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654/4
8+
fortype<-typesdo
9+
guard_name=iftype==:string,do:"is_binary",else:"is_#{to_string(type)}"
10+
11+
quotedo
12+
defpmatch(field,nil,[unquote(type),required:false]),do:done(field,nil)
13+
14+
defpmatch(field,value,[unquote(type),required:false])
15+
whenunquote(:"#{guard_name}")(value)do
16+
done(field,value)
17+
end
18+
19+
defpmatch(field,value,[unquote(type)])whenunquote(:"#{guard_name}")(value),
20+
do:done(field,value)
21+
22+
defpmatch(field,value,[unquote(type),required:false]),
23+
do:error(field,value,unquote(type))
24+
25+
defpmatch(field,value,[unquote(type)]),do:error(field,value,unquote(type))
26+
end
27+
end
28+
end
29+
end

‎test/helper/converter/editor_to_html_test/header_test.exs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Header do
124124
}\">footer title content</div>\n</div>\n<div>"
125125
end
126126

127-
@tag:wip
127+
@tag:wip2
128128
test"wrong header format data should have invalid hint"do
129129
json=
130130
Map.merge(@editor_json,%{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp