The main discrepancy imo is not where modern morality expects it. Judah mistook disguised Tamar not for a mere zonah (prostitute in the generic sense, v. 15) but for a qedeshah (a "holy girl" = a temple prostitute, v. 21-22 -- although this word is cleverly put on the lips of his Adullamite "friend"). And such qedeshoth were a big no-no in Deuteronomistic Law, for religious rather than moral reasons (Deuteronomy 23:18). However the main flaw of Judah, by the text's own assessment, is not going to a prostitute, but breaking his word and not applying the Levirate custom (v. 26, cf. Deuteronomy 25). And the text implies many other contraventions to the Deuteronomistic Law: Judah marries a Canaanite, v. 2; Tamar seems to be a Canaanite too (and so later Jewish tradition has her). By introducing such a multiple "disorder" into the ancestry of David Genesis 38, just as the book of Ruth (where a mixed marriage with a Moabite also enters into the Davidic line), may be discreetly fighting Deuteronomistic exclusivism.