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

Access data fields on transformation functions #14

Merged
merged 2 commits into from
Nov 26, 2024

Conversation

zoedsoupe
Copy link
Owner

@zoedsoupe zoedsoupe commented Nov 26, 2024

Description
Allows the access of other data fields while applying the transformation (aka mapper) function
on a :transform field, example:

import Peri

s = %{
  id: {:required, :string},
  name:
    {:string,
     {:transform,
      fn
        name, data -> (data[:id] && name <> "-#{data[:id]}") || name
      end}}
}

Peri.validate(s, %{id: 1, name: "maria"})
# {:ok, %{id, name: "maria-1"}}

It should also work on nested schemas (check test file) and handle errors.

Related Issues
closes #13

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

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.

Additional Context
N/A

- minor fix of `:transform` directive not accepting
MFAs on schema definition validation
- passing root data to the `mapper` function
as second argument
@zoedsoupe
Copy link
Owner Author

improving some documentation stuff

@zoedsoupe zoedsoupe merged commit 6520de5 into main Nov 26, 2024
4 checks passed
zoedsoupe added a commit that referenced this pull request Nov 26, 2024
@zoedsoupe zoedsoupe deleted the feat/transform-improvement branch November 26, 2024 16:41
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.

[FEATURE] Access to other fields during transform
1 participant