Skip to content

Refactored out detectFileMIMEType, matchesMIMEType logic for reuse. Added standalone isMIMEType validator for flexibility#1544

Open
dapzthelegend wants to merge 3 commits intogo-playground:masterfrom
dapzthelegend:feature/mimetype-validation
Open

Refactored out detectFileMIMEType, matchesMIMEType logic for reuse. Added standalone isMIMEType validator for flexibility#1544
dapzthelegend wants to merge 3 commits intogo-playground:masterfrom
dapzthelegend:feature/mimetype-validation

Conversation

@dapzthelegend
Copy link

Summary

Adds a new built-in mimetype validator for file paths.

It supports both exact MIME matches and wildcard subtype matches:

validate.Var("photo.png", "mimetype=image/png")
validate.Var("photo.png", "mimetype=image/*")

Changes

  • add mimetype as a built-in validation tag
  • extract shared file MIME detection logic
  • refactor isImage to reuse the shared MIME detection helper
  • keep isImage's existing explicit image MIME allowlist unchanged
  • add docs and tests for mimetype
  • add default translations and translation tests for mimetype

Notes

mimetype accepts:

  • type/subtype
  • type/*

Validation fails if the field is not a string, the file does not exist, the path is a directory, the MIME type cannot be detected, or the validator param is invalid.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@dapzthelegend dapzthelegend requested a review from a team as a code owner March 15, 2026 13:35
@dapzthelegend
Copy link
Author

@the-fanan @zemzale

PR #1 to refactor out the mime detection logic and create a flexible standalone isMIMEType Validator

To keep existing behavior I've retained the type dict for isImage

zemzale
zemzale previously approved these changes Mar 18, 2026
Copy link
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small tests thing, but the code looks good.

Would like someone else opinion about the validator itself, before merging.

tag string
expected bool
createFile func()
destroyFile func()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: You don't need this. Since you do t.Tempdir all the files are going to be deleted when the testrun ends.

The directory is automatically removed when the test and all its subtests complete

source

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed this and updated it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants