Skip to content

Add support for generating schema fields from model fields #81

@ellmetha

Description

@ellmetha

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 #model class method that would require a model and a list of model fields for which schema fields should be generated:

class ArticleSchema < Marten::Schema
  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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions