Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not raise on schema/data with string keys #4

Merged
merged 1 commit into from
Jul 23, 2024

Conversation

iurimateus
Copy link
Contributor

Allow binary keys when traversing data. Unsure if this is the correct fix.

Type of Change

  • Bug fix

Checklist

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Example:

iex|29| schema = %{"str" => {:required, :string, {:transform, &String.trim/1}}}
%{"str" => {:required, {:string, {:transform, &String.trim/1}}}}
iex|30| Peri.validate(schema, %{"str" => "aaaaaaa "})
{:ok, %{"str" => "aaaaaaa"}}
iex|31| Peri.validate(schema, %{"a" => "aaaaaaa "})
..!!** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not an atom

    (erts 14.2.2) :erlang.atom_to_binary("str")
    (peri 0.2.6) lib/peri.ex:358: Peri.get_enumerable_value/2
    (peri 0.2.6) lib/peri.ex:302: anonymous fn/3 in Peri.filter_data/2
    (stdlib 5.2) maps.erl:416: :maps.fold_1/4
    (peri 0.2.6) lib/peri.ex:300: Peri.filter_data/2
    (peri 0.2.6) lib/peri.ex:275: Peri.validate/2
    iex:31: (file)
iex|32|Peri.validate(schema, %{"str" => 123})
{:error,
 [
   %Peri.Error{
     path: ["str"],
     key: "str",
     content: %{actual: "123", expected: :string},
     message: "expected type of :string received 123 value",
     errors: nil
   }
 ]}

@zoedsoupe
Copy link
Owner

thank you so much for the contribution!

@zoedsoupe zoedsoupe closed this Jul 23, 2024
@zoedsoupe zoedsoupe reopened this Jul 23, 2024
@zoedsoupe zoedsoupe merged commit 266b5a2 into zoedsoupe:main Jul 23, 2024
5 checks passed
zoedsoupe added a commit that referenced this pull request Jul 23, 2024
* 'main' of github.com:zoedsoupe/peri:
  fix: do not raise on schemas with string keys (#4)
zoedsoupe added a commit that referenced this pull request Jul 23, 2024
* 'main' of github.com:zoedsoupe/peri:
  fix: do not raise on schemas with string keys (#4)
@LeartS
Copy link

LeartS commented Jul 26, 2024

@zoedsoupe could we get a release with this fix? It's quite a critical one!

@zoedsoupe
Copy link
Owner

zoedsoupe commented Jul 26, 2024

sorry, I'm not managing GitHub releases for this library but recently I just released the 0.2.7 package version on hex that includes this fix!

iex(1)> Mix.install [{:peri, "~> 0.2"}]
Resolving Hex dependencies...
Resolution completed in 0.012s
New:
  peri 0.2.7
* Getting peri (Hex package)
==> peri
Compiling 5 files (.ex)
Generated peri app
:ok
iex(2)> schema = %{"str" => {:required, {:string, {:transform, &String.trim/1}}}}
%{"str" => {:required, {:string, {:transform, &String.trim/1}}}}
iex(3)> Peri.validate(schema, %{"a" => "aaaaaaa "})
{:error,
 [
   %Peri.Error{
     path: ["str"],
     key: "str",
     content: %{},
     message: "is required",
     errors: nil
   }
 ]}
iex(9)> Peri.validate(schema, %{"str" => "aaaaaaa "})
{:ok, %{"str" => "aaaaaaa"}}

@LeartS
Copy link

LeartS commented Jul 27, 2024

Yeah I meant the hex.pm package. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants