“…Joseph, being a righteous man and unwilling to expose her to public disgrace, planned to dismiss her quietly.”
Being righteous does not mean sinless.  There was only one who was sinless and that was Jesus.  So what does it mean to be “righteous.”  In the Hebrew language “righteous” means to “do, or to be what anything was created to do or be.”  If you flip the light switch and the light goes on, “the switch is righteous.”  If you jump  in your car and it starts right up, your car is “righteous.”  If you turn on the TV and you get a picture and sound the TV is righteous.  For Joseph to be righteous means that he lived according to the teachings of the Word of God; he was just, forgiving, merciful, and compassionate.  He did the will of God and not the will of men.  We are not called by God to be sinless.  We are called to be God’s children.  That means we carry God’s DNA in us, which means we are to be righteous; follow God’s Word, be just, forgiving, merciful, and compassionate.  If someone wrote about you, would he be able to say, “You are righteous”?