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

[WIP] Ecto integration #18

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft

[WIP] Ecto integration #18

wants to merge 14 commits into from

Conversation

zoedsoupe
Copy link
Owner

@zoedsoupe zoedsoupe commented Feb 6, 2025

Description

Provide seamlessly integration with Ecto, providing a new function to_changeset!/2 that receives a peri schema and the data to be validated, acting like a common changeset/2 function.

This function will translate all possible peri type definitions to ecto ones, using ecto schemaless changesets and custom ecto types implemented on Peri.Ecto.Type.* modules, that can be used alone on your other Ecto schemas, for example:

  • Peri.Ecto.Type.Atom
  • Peri.Ecto.Type.PID
  • Peri.Ecto.Type.Tuple
  • Peri.Ecto.Type.Either
  • Peri.Ecto.Type.OneOf

Also, notice that Ecto doesn't support raw data structures validations, so to_changeset!/2 only accepts a map peri schema, will raise otherwise.

It also supports nested schemas, respecting above rules, and also nested composite types, like tuple of maps, or either maps or oneof maps and so on (wip right now).

Related Issues

This PR will close #3

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

Just to track the already supported types implemented on this PR:

  • :any
  • :atom
  • :integer and all custom validations :eq, :neq, :gt, :gte, :lt, :lte, :range
  • :float and all custom validations :eq, :neq, :gt, :gte, :lt, :lte, :range
  • :map
  • :boolean
  • :string and all those custom validations like :eq, :min, :max, :regex
  • :date
  • :time
  • :datetime
  • :naive_datetime
  • :duration
  • :pid via custom ecto type
  • {type, {:default, val}} for setting default values
  • {:enum, choices} via Ecto.Enum
  • {type, {:transform, mapper}} via Ecto.Changeset.update_change/3
  • {:required, type} via Ecto.Changeset.validate_required/2
  • {:list, type} via Ecto {:array, type}
  • {:tuple, types} via custom Ecto type
  • {:either, {fat, snd}} via custom Ecto type
  • {:oneof, types} via custom Ecto type
  • nested schemas
  • nested array schemas
  • nested schemas on :oneof
  • nested schemas on :either
  • nested schemas on :tuple
  • {:cond, condition, true, false}
  • {:dependent, field, condition, type}
  • {:dependent, condition}
  • {:dependent, {mod, fun}}
  • {:custom, callback}
  • {:custom, {mod, fun}}
  • {:custom, {mod, fun, args}}

@zoedsoupe zoedsoupe marked this pull request as draft February 6, 2025 15:39
@zoedsoupe zoedsoupe self-assigned this Feb 6, 2025
…o feat/ecto-integration

* 'feat/ecto-integration' of github.com:zoedsoupe/peri:
  feat: add to_changeset!/2 benchs and results
  fix: handle list composite types
…ration

* 'main' of github.com:zoedsoupe/peri:
  fix: make :either behave consistently with :oneof for nested (#21)
  feat: new schema types for map and literal (#22)
  Update README.md
  Update README.md
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] Integration with Ecto
1 participant