Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : isMimeType(str) #760

Merged
merged 4 commits into from
Dec 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Validator | Description
**isLowercase(str)** | check if the string is lowercase.
**isMACAddress(str)** | check if the string is a MAC address.
**isMD5(str)** | check if the string is a MD5 hash.
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
**isMobilePhone(str, locale)** | check if the string is a mobile phone number,<br/><br/>(locale is one of `['ar-AE', 'ar-DZ','ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ var _isDataURI = require('./lib/isDataURI');

var _isDataURI2 = _interopRequireDefault(_isDataURI);

var _isMimeType = require('./lib/isMimeType');

var _isMimeType2 = _interopRequireDefault(_isMimeType);

var _isLatLong = require('./lib/isLatLong');

var _isLatLong2 = _interopRequireDefault(_isLatLong);
Expand Down Expand Up @@ -328,6 +332,7 @@ var validator = {
isISO31661Alpha2: _isISO31661Alpha2.default,
isBase64: _isBase2.default,
isDataURI: _isDataURI2.default,
isMimeType: _isMimeType2.default,
isLatLong: _isLatLong2.default,
ltrim: _ltrim2.default,
rtrim: _rtrim2.default,
Expand Down
52 changes: 52 additions & 0 deletions lib/isMimeType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isMimeType;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
Checks if the provided string matches to a correct Media type format (MIME type)

This function only checks is the string format follows the
etablished rules by the according RFC specifications.
This function supports 'charset' in textual media types
(https://tools.ietf.org/html/rfc6657).

This function does not check against all the media types listed
by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)
because of lightness purposes : it would require to include
all these MIME types in this librairy, which would weigh it
significantly. This kind of effort maybe is not worth for the use that
this function has in this entire librairy.

More informations in the RFC specifications :
- https://tools.ietf.org/html/rfc2045
- https://tools.ietf.org/html/rfc2046
- https://tools.ietf.org/html/rfc7231#section-3.1.1.1
- https://tools.ietf.org/html/rfc7231#section-3.1.1.5
*/

// Match simple MIME types
// NB :
// Subtype length must not exceed 100 characters.
// This rule does not comply to the RFC specs (what is the max length ?).
var mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len

// Handle "charset" in "text/*"
var mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len

// Handle "boundary" in "multipart/*"
var mimeTypeMultipart = /^multipart\/[a-zA-Z0-9\.\-\+]{1,100}(;\s?(boundary|charset)=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?){0,2}$/i; // eslint-disable-line max-len

function isMimeType(str) {
(0, _assertString2.default)(str);
return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str);
}
module.exports = exports['default'];
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import isISO31661Alpha2 from './lib/isISO31661Alpha2';

import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';
import isMimeType from './lib/isMimeType';

import isLatLong from './lib/isLatLong';
import isPostalCode from './lib/isPostalCode';
Expand Down Expand Up @@ -146,6 +147,7 @@ const validator = {
isISO31661Alpha2,
isBase64,
isDataURI,
isMimeType,
isLatLong,
ltrim,
rtrim,
Expand Down
40 changes: 40 additions & 0 deletions src/lib/isMimeType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assertString from './util/assertString';

/*
Checks if the provided string matches to a correct Media type format (MIME type)

This function only checks is the string format follows the
etablished rules by the according RFC specifications.
This function supports 'charset' in textual media types
(https://tools.ietf.org/html/rfc6657).

This function does not check against all the media types listed
by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)
because of lightness purposes : it would require to include
all these MIME types in this librairy, which would weigh it
significantly. This kind of effort maybe is not worth for the use that
this function has in this entire librairy.

More informations in the RFC specifications :
- https://tools.ietf.org/html/rfc2045
- https://tools.ietf.org/html/rfc2046
- https://tools.ietf.org/html/rfc7231#section-3.1.1.1
- https://tools.ietf.org/html/rfc7231#section-3.1.1.5
*/

// Match simple MIME types
// NB :
// Subtype length must not exceed 100 characters.
// This rule does not comply to the RFC specs (what is the max length ?).
const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len

// Handle "charset" in "text/*"
const mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len

// Handle "boundary" in "multipart/*"
const mimeTypeMultipart = /^multipart\/[a-zA-Z0-9\.\-\+]{1,100}(;\s?(boundary|charset)=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?){0,2}$/i; // eslint-disable-line max-len

export default function isMimeType(str) {
assertString(str);
return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str);
}
47 changes: 47 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5139,4 +5139,51 @@ describe('Validators', function () {
args: ['any'],
});
});

it('should validate MIME types', function () {
test({
validator: 'isMimeType',
valid: [
'application/json',
'application/xhtml+xml',
'audio/mp4',
'image/bmp',
'font/woff2',
'message/http',
'model/vnd.gtw',
'multipart/form-data',
'multipart/form-data; boundary=something',
'multipart/form-data; charset=utf-8; boundary=something',
'multipart/form-data; boundary=something; charset=utf-8',
'multipart/form-data; boundary=something; charset="utf-8"',
'multipart/form-data; boundary="something"; charset=utf-8',
'multipart/form-data; boundary="something"; charset="utf-8"',
'text/css',
'text/plain; charset=utf8',
'Text/HTML;Charset="utf-8"',
'text/html;charset=UTF-8',
'Text/html;charset=UTF-8',
'text/html; charset=us-ascii',
'text/html; charset=us-ascii (Plain text)',
'text/html; charset="us-ascii"',
'video/mp4',
],
invalid: [
'',
' ',
'/',
'f/b',
'application',
'application\\json',
'application/json/text',
'application/json; charset=utf-8',
'audio/mp4; charset=utf-8',
'image/bmp; charset=utf-8',
'font/woff2; charset=utf-8',
'message/http; charset=utf-8',
'model/vnd.gtw; charset=utf-8',
'video/mp4; charset=utf-8',
],
});
});
});
40 changes: 40 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,45 @@ function isDataURI(str) {
return dataURI.test(str);
}

/*
Checks if the provided string matches to a correct Media type format (MIME type)

This function only checks is the string format follows the
etablished rules by the according RFC specifications.
This function supports 'charset' in textual media types
(https://tools.ietf.org/html/rfc6657).

This function does not check against all the media types listed
by the IANA (https://www.iana.org/assignments/media-types/media-types.xhtml)
because of lightness purposes : it would require to include
all these MIME types in this librairy, which would weigh it
significantly. This kind of effort maybe is not worth for the use that
this function has in this entire librairy.

More informations in the RFC specifications :
- https://tools.ietf.org/html/rfc2045
- https://tools.ietf.org/html/rfc2046
- https://tools.ietf.org/html/rfc7231#section-3.1.1.1
- https://tools.ietf.org/html/rfc7231#section-3.1.1.5
*/

// Match simple MIME types
// NB :
// Subtype length must not exceed 100 characters.
// This rule does not comply to the RFC specs (what is the max length ?).
var mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len

// Handle "charset" in "text/*"
var mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len

// Handle "boundary" in "multipart/*"
var mimeTypeMultipart = /^multipart\/[a-zA-Z0-9\.\-\+]{1,100}(;\s?(boundary|charset)=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?){0,2}$/i; // eslint-disable-line max-len

function isMimeType(str) {
assertString(str);
return mimeTypeSimple.test(str) || mimeTypeText.test(str) || mimeTypeMultipart.test(str);
}

var lat = /^\(?[+-]?(90(\.0+)?|[1-8]?\d(\.\d+)?)$/;
var long = /^\s?[+-]?(180(\.0+)?|1[0-7]\d(\.\d+)?|\d{1,2}(\.\d+)?)\)?$/;

Expand Down Expand Up @@ -1458,6 +1497,7 @@ var validator = {
isISO31661Alpha2: isISO31661Alpha2,
isBase64: isBase64,
isDataURI: isDataURI,
isMimeType: isMimeType,
isLatLong: isLatLong,
ltrim: ltrim,
rtrim: rtrim,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.