computation and analysis – Why does D ignore the conditions specified on a custom function?

I define a function with a condition like that.

foo[expr_] /; FreeQ[expr, bar] := expr

Such as foo[12] Return 12, and foo[12 bar] returns not evaluated as foo[12 bar].

Why is it when I take the derivative D[foo[12 bar], x] or D[foo[12 bar], bar] it acts as if the condition is not present, returning 0 and 12 respectively?

The expected and desired behavior is that the derivative cannot be evaluated. I consider that the result that Mathematica gives is false. My function does not have this output when this condition is not met, and therefore it is not a valid derivative.

I found this specifying in addition that catch everything foo[_] := 0 means that D takes the condition into account and uses this definition instead when appropriate, but without it, the condition is ignored as shown above. Move condition to foo[expr_] /; FreeQ[expr, bar] := expr has no effect on behavior.

I suppose this line of documentation may be relevant: "D returns generic results which may not take discontinuities, cusps or other special points into consideration" Is my condition treated as a special point? I don't want that, I just want it to behave like other functions that cannot be evaluated for an entry.