Upon clicking save, how do I stop the node from inserting?
I would probably just use Unique field, as all of the legwork is done.

This module performs additional validation when a node is created or
updated by a user to require that a node's title, author, language,
and CCK fields are unique within a given context.

If you really want it in code though, I'd take a look through the source of this module and see how it adds the extra validation.
I know you said you'd prefer it in code, but I've left this here just in case it benefits anyone with the same question as you :-)