Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7a1c83f
add empty parameter to gen
kanishka-linux Jan 21, 2019
f961ecb
handle case when $ref is absent
kanishka-linux Jan 21, 2019
c9c698a
use generator instead of gen in verify
kanishka-linux Jan 21, 2019
5114855
pass context to gen_init and handle generator resizing
kanishka-linux Jan 21, 2019
73f588d
pass context to gen_init
kanishka-linux Jan 21, 2019
60c9a4a
remove gen_enum from gen_init
kanishka-linux Jan 22, 2019
4cc7990
rename gen_init to gen_lazy
kanishka-linux Jan 22, 2019
179a5ad
add separate Context module for holding root schema, child schema and…
kanishka-linux Jan 23, 2019
565f36a
pass context as struct instead of map to gen_lazy
kanishka-linux Jan 23, 2019
468e47d
use single empty parameter in gen
kanishka-linux Jan 23, 2019
160eeec
use Context with pattern matching in gen
kanishka-linux Jan 23, 2019
437fabc
use Context with pattern matching in gen
kanishka-linux Jan 23, 2019
fb21170
pass only context to expand_ref
kanishka-linux Jan 23, 2019
006254e
pass only context parameter to expand_ref
kanishka-linux Jan 23, 2019
2ba7580
mix format
kanishka-linux Jan 23, 2019
0379a9e
modify arguments to gen_lazy and remove for loop
kanishka-linux Jan 23, 2019
ccb46b1
fix unused variable warning
kanishka-linux Jan 23, 2019
c87f878
add separate tests for $ref
kanishka-linux Jan 23, 2019
736234f
handle nested ref in gen_lazy
kanishka-linux Jan 23, 2019
5f579d4
handle $ref when not nil
kanishka-linux Jan 23, 2019
42e52dc
replace Poison with Jason
kanishka-linux Jan 23, 2019
0b10c59
remove merge conflict
kanishka-linux Jan 24, 2019
040af63
add refExtra.json
kanishka-linux Jan 24, 2019
87ae1f5
read test schema from refExtra.json
kanishka-linux Jan 24, 2019
2c25999
add odgn_json_pointer as dependency
kanishka-linux Jan 25, 2019
8d8ca2e
use JSONPointer to resolve json pointer
kanishka-linux Jan 25, 2019
f8a2fee
use URI.parse instead of manually splitting
kanishka-linux Jan 25, 2019
80ce5d7
add default_path and cache to context
kanishka-linux Jan 25, 2019
98ebb9f
add test for http $ref
kanishka-linux Jan 25, 2019
2f260d4
modify expand_ref to include local file schema and caching of http li…
kanishka-linux Jan 25, 2019
7231c94
rename default_path to root_dir
kanishka-linux Jan 26, 2019
52729b2
rename uri_parse to parsed_uri
kanishka-linux Jan 26, 2019
3d7f020
format json file
kanishka-linux Jan 26, 2019
375fda5
add support for patternProperties
kanishka-linux Jan 29, 2019
229533b
add test for patternProperties
kanishka-linux Jan 29, 2019
1d52d84
add one more test
kanishka-linux Jan 29, 2019
d5bc5f6
modify check_pattern_properties
kanishka-linux Jan 29, 2019
86f1c92
modify test
kanishka-linux Jan 29, 2019
98863b9
Merging jake master branch
kanishka-linux Jan 29, 2019
ab37258
mix format
kanishka-linux Jan 29, 2019
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
78 changes: 78 additions & 0 deletions lib/jake/object.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
defmodule Jake.Object do
alias Jake.StreamUtil
alias Jake.Context
alias Jake.MapUtil

def gen(%Context{child: spec} = context) do
properties = Map.get(spec, "properties", %{})

if map_size(properties) == 0 and spec["patternProperties"] do
gen_pattern_properties(context)
else
new_child = Map.put(spec, "properties", properties)
gen_regular_object(%{context | child: new_child})
end
end

defp gen_pattern_properties(
%Context{child: %{"patternProperties" => patternProperties} = _spec} = context
) do
nlist =
for {k, v} <- patternProperties,
do: build_and_verify_patterns(k, v, patternProperties, context)

merge_patterns(nlist)
end

defp gen_regular_object(%Context{child: %{"properties" => properties} = spec} = context) do
nproperties = check_pattern_properties(spec, properties, spec["patternProperties"])

properties =
if is_list(nproperties) and length(nproperties) > 0 do
Enum.reduce(nproperties, %{}, fn x, acc -> MapUtil.deep_merge(x, acc) end)
else
properties
end

spec = Map.put(spec, "properties", properties)
context = %{context | child: spec}
required = Map.get(spec, "required", [])
all_properties = Map.keys(properties)
optional = Enum.filter(all_properties, &(!Enum.member?(required, &1)))
Expand All @@ -26,6 +58,52 @@ defmodule Jake.Object do
|> StreamUtil.merge(StreamData.fixed_map(as_map(properties, required, context)))
end

defp check_pattern_properties(_spec, properties, pprop) do
if pprop do
pprop_list = Map.to_list(pprop)

Map.to_list(properties)
|> Enum.map(fn {k, v} ->
Enum.map(pprop_list, fn {key, value} ->
if Regex.match?(~r/#{key}/, k) do
Map.put(properties, k, Map.merge(v, value))
end
end)
end)
|> List.flatten()
|> Enum.uniq()
|> List.delete(nil)
else
properties
end
end

defp merge_patterns(nlist) do
merge_maps = fn list -> Enum.reduce(list, %{}, fn x, acc -> Map.merge(acc, x) end) end

StreamData.bind(StreamData.fixed_list(nlist), fn list ->
StreamData.constant(merge_maps.(list))
end)
end

defp build_and_verify_patterns(key, value, pprop, context) do
pprop_schema = %{"patternProperties" => pprop}
# IO.inspect(pprop_schema)
nkey = Randex.stream(~r/#{key}/, mod: Randex.Generator.StreamData)
nval = %{context | child: value} |> Jake.gen_lazy()

StreamData.bind(nkey, fn k ->
StreamData.bind_filter(
nval,
fn v ->
result = ExJsonSchema.Validator.valid?(pprop_schema, %{k => v})
if result, do: {:cont, StreamData.constant(%{k => v})}, else: :skip
end,
100
)
end)
end

defp additional(properties, _all, min.._max, _context) when min < 0 or not is_map(properties) do
StreamData.constant(%{})
end
Expand Down
3 changes: 2 additions & 1 deletion test/jake_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ defmodule JakeTest do
"draft4/additionalItems.json",
"draft4/maxProperties.json",
"draft4/minProperties.json",
"draft4/additionalProperties.json"
"draft4/additionalProperties.json",
"draft4/patternProperties.json"
] do
Path.wildcard("test_suite/tests/#{path}")
|> Enum.map(fn path -> File.read!(path) |> Jason.decode!() end)
Expand Down
11 changes: 11 additions & 0 deletions test_suite/tests/draft4/patternProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,16 @@
"valid": false
}
]
},
{
"description": "patternProperties with properties",
"schema": {
"type": "object",
"properties": {"foo": {"maximum": 100}, "bar": {"type":"integer"}},
"patternProperties": {
"f.*o": {"type": "integer"},
"ba*r": {"minimum": 20}
}
}
}
]