Hello! Long time no ranch! Whilst longing for a similar JS community I thought I'd double check if there was actually a sub-forum here... and here it is
Anyway, I've been a bit confused lately whilst trying to test my server side validation and thought someone here might be able to enlighten me
I've been using express-validator's `isString()` method, and suddenly realised that all my client -> server communication comes in as JSON anyway, so is in effect, a string. In other words, why would I ever check whether `req.body.exampleVar` is a string?
I've considered leaving the `isString` check in place, just to be sure, but because I can't pass an integer anyway I can't test if the validation is even working!
I guess what I'm saying is that there's a hole in my knowledge here, and I'd appreciate it if anyone knows either a simple answer or of a good resource I could read on the topic
Bear Bibeault wrote:Are you talking about request parameters or the request body?
Both are always strings, but the body is interpreted by the content type mime type (in this case application/json).
For example, if the body represents a binary type (e.g. JPEG image), it'll be passed as the image data encoded as a string (base64 if I recall correctly).
JSON is a string. The mime type tells the receiver that the string in the body is to be interpreted as JSON.
The body itself - and I use `express.json()` on the server side. So should that be converting each variable (as in req.body.x, req.body.y) to it's intended type? If that's true (and it sounds like it is from your JPEG example) then it's not working as intended, but at least I now know where to look, and that I should probably still validate against types that aren't strings