Remix.run Logo
▲ cogman10 6 hours ago

> Contracts are explicitly not input validation. They are there to encode what you expect

The line between what's "input validation" and what's "What you expect" is very muddy. Input validation is a statement of "this is input I expect".

Is it input validation to say a string must be at least some length characters? What if that string is a password? What if it's an MD5 sum?

It SHOULDN'T be used for input validation but it will be. And almost certainly the person that does use it for input validation will argue that they were just laying out expectations.

▲spider-mario 2 hours ago | parent | next [-]

> The line between what's "input validation" and what's "What you expect" is very muddy. Input validation is a statement of "this is input I expect".

> Is it input validation to say a string must be at least some length characters? What if that string is a password? What if it's an MD5 sum?

Think of it this way: pre-conditions are what the function assumes to be true when it’s called. If it would not be justified to make that assumption at the point where the function is called, it is the responsibility of the caller to perform a runtime check to ensure that it applies. So you might well have a precondition at the level of the function and an explicit check of the exact same condition on the caller side to ensure that it’s respected. If the caller fails to ensure that the condition is fulfilled, and the precondition thereby trips, that’s a programming error.

▲OneDeuxTriSeiGo 5 hours ago | parent | prev [-]

> Is it input validation to say a string must be at least some length characters

Yes. Yes it is. Input validation should happen at the public API and then contracts after that.

Contracts are very explicitly for things you don't want to ever see happen. They are "something went wrong".

So your API would accept a function that handles input validation and maybe has its own contract for things it explicitly doesn't handle but then internally you use contracts to enforce that the string is already input validated and you don't have to enforce edge cases.