file_validators
file_validators copied to clipboard
JSON parser error when a string value is passed
I am trying to use file_validators
to validate the content type of avatars in my rails api. The client selects a file, submits the form, gets the content type validated with
validates :avatar, file_content_type: { allow: /^image\/.*/ }
However, the client currently has a bug where it sends a text string instead of an uploaded file. I would expect this to NOT be of an image content type and therefore fail the validation. But it seems file_validators
in https://github.com/musaffa/file_validators/blob/e9c12faeb17209b8b8a49cb4932ab1e0bd1b91db/lib/file_validators/validators/file_content_type_validator.rb#L43 tries to parse the string to json. That raises an exception because the string is not valid json
JSON::ParserError: expected false at line 1, column 2 [parse.c:152]
Maybe the correct behaviour would be for the gem to rescue a json parser error? Nevertheless Thanks for a great gem! :)
Thanks for reporting this. I would gladly accept a pull request if you wrap code with json parse exception handler. You will need to do the same on file_size_validator
as well.