DEV Community

Discussion on: How To Write A Custom Elixir Schema Validation

mackshkatz profile image
Max Katz

Great write-up! One other way I would maybe handle the need to check for those fields' existence is by having another function clause for mathematical_validation/1:

defp mathematical_validation(changeset = %Ecto.Changeset{errors: errors}) when length(errors) == 0, do: changeset

defp mathematical_validation(changeset) do

This cleans up the actual logic for this function, of course this wouldn't work I guess if you didn't have the validate_required check before it in the pipeline, so it all kinda depends on the requirement. Mostly I just wanted to say hiiii, long time no talk :)

noelworden profile image
Noel Worden Author

Yeah, I think I started with something like this, but the feedback in the PR was that if other, unrelated validations were inserted in the wrong place in the pipeline then the errors field might show a false positive, leading to this custom function not running at all.

But, it obviously wasn't too bad of an idea if thats what you thought too!

Sorry for the late response, kind of dropped the ball.