Refactor id validation logic