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

Add support for generating schema fields from model fields #81

Open
ellmetha opened this issue Mar 7, 2023 · 0 comments
Open

Add support for generating schema fields from model fields #81

ellmetha opened this issue Mar 7, 2023 · 0 comments
Assignees

Comments

@ellmetha
Copy link
Member

ellmetha commented Mar 7, 2023

Description

Presently, schema fields must be defined explicitly as part of schema classes. This means that in order to validate the data required to create Article records, we could rely on the following schema:

class ArticleSchema < Marten::Schema
  field :title, :string, max_size: 255
  field :content, :string
end

This works fine, but this is not ideal since we are essentially "repeating" some of the properties of the fields of the model for which we want to validate data (eg. title and content fields in this example).

In order to allow for DRY-er codebases, we could allow schema fields to easily include fields generated from model fields. This could work by leveraging a new #from_model class method that would require a list of model fields for which schema fields should be generated:

class ArticleSchema < Marten::Schema
  from_model Article, fields: [:title, :body]
end

This change will involve ensuring that model fields can generate corresponding schema fields if this is applicable for their use case. Some model fields could decide to not implement a corresponding schema fields, and in this case, a dedicated exception should be raised.

@ellmetha ellmetha added this to the v0.3.0 milestone Mar 7, 2023
@ellmetha ellmetha self-assigned this Mar 7, 2023
@ellmetha ellmetha removed this from the v0.3.0 milestone Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant