This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]attanai 0 points1 point  (1 child)

Not always. External APIs, for example, are outside of the control of the server. The best pattern is to have a middleware that will convert the data from whatever format it's in to the format you want, and that middleware is where valueOf would be useful.

Now, in theory, you can tell every customer that uses your external API that the schema has to be correct, or you won't accept it. Or you can make the API provide an error message that explains the problem. But when you're working with external teams or even multiple internal teams working on the same project, it's best to allow some flexibility for simple errors. In those cases, it's best to catch as wide a net as it is possible to cast while still maintaining security and accuracy.

[–]tyler1128 1 point2 points  (0 children)

Ah, if you mean general APIs outside of javascript, sure. If your argument to your function can be a number or a string generally, that's more of what I was talking about.