Changeset validations for data which is not inside changeset.changes

Hi guys,

I’m working on a blog and each post can have a “draft” or “published” status. In my changeset, I’ve set a changeset validation for min number of characters. I want this validation to check number of characters only if you want to publish a post.

For example, if you’re just writing a draft, it can be empty or have any number of characters but if you want to publish it, it needs to have at least 5000 characters.

I have a draft changeset but I also have a publish changeset with all the validations.

The problem is, if you save a post as draft and then you get back and want to publish it but you don’t change the post, it will be published even if it has less then 5000 characters.

The thing is, if the post is not changed, it will not be part of changeset.changes and it will skip validation so my question is; how do I force this validation on a data that’s part of Post struct and not part of changeset_changes?

You can use Ecto.Changeset.force_change.

1 Like