Skip to content

Commit

Permalink
Fixes #74 (Date and DateTime accepting any map)
Browse files Browse the repository at this point in the history
- Implements fix
- Adds regression test
- Updates changelog/version
  • Loading branch information
Qqwy committed Oct 26, 2021
1 parent b7c27cb commit af4deb0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -204,6 +204,9 @@ Details:
- [ ] Per-module or even per-spec settings to turn on/off, configure formatter, etc.

### Changelog
- 0.10.2 -
- Fixes:
- Fixes issue where FixedMaps would accept maps any maps (even those missing the required keys) sometimes. (c.f. #74)
- 0.10.1 -
- Fixes:
- Swaps `Murmur` out for Erlang's builtin `:erlang.phash2/2` to generate data for function-types, allowing the removal of the optional dependency on the `:murmur` library.
Expand Down
6 changes: 3 additions & 3 deletions lib/type_check/builtin/fixed_map.ex
Expand Up @@ -27,8 +27,8 @@ defmodule TypeCheck.Builtin.FixedMap do

def to_check(s, param) do
res = quote generated: true, location: :keep do
with :ok <- unquote(map_check(param, s)),
:ok <- unquote(build_keys_presence_ast(s, param)),
with {:ok, _, _} <- unquote(map_check(param, s)),
{:ok, _, _} <- unquote(build_keys_presence_ast(s, param)),
{:ok, bindings3, altered_param} <- unquote(build_keypairs_checks_ast(s.keypairs, param, s)) do
{:ok, bindings3, altered_param}
end
Expand Down Expand Up @@ -61,7 +61,7 @@ defmodule TypeCheck.Builtin.FixedMap do

case unquote(required_keys) -- actual_keys do
[] ->
:ok
{:ok, [], unquote(param)}

missing_keys ->
{:error,
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -6,7 +6,7 @@ defmodule TypeCheck.MixProject do
def project do
[
app: :type_check,
version: "0.10.1",
version: "0.10.2",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
25 changes: 25 additions & 0 deletions test/type_check/builtin/fixed_map_test.exs
@@ -0,0 +1,25 @@
defmodule TypeCheck.Builtin.FixedMapTest do
use ExUnit.Case, async: true
use TypeCheck

defmodule UnrelatedStruct do
defstruct []
end

# Regression test for issue #74
test "Date.t() only accepts valid dates" do
fun = &TypeCheck.conforms(&1, Date.t())


assert {:ok, _} = fun.(Date.utc_today())

assert {:error, _} = fun.(DateTime.utc_now())
assert {:error, _} = fun.(%{})
assert {:error, _} = fun.(%UnrelatedStruct{})

assert {:error, _} = fun.([])
assert {:error, _} = fun.(6)
assert {:error, _} = fun.("some string")
end

end

0 comments on commit af4deb0

Please sign in to comment.