That would certainly be an easy off-the-shelf solution... Although if the door opens a crack, that also means a reckless toddler (is there any other kind?) could slam it on their own fingers.
There are some which are just hinged metal that can lock at a right-angle, one of those would give tighter no-finger-gap tolerances, while also being structurally weaker in case of emergency.
https://www.homedepot.com/pep/Prime-Line-1-3-8-in-Door-Lock-...