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

MD5 string validation #557

Merged
merged 6 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -85,6 +85,7 @@ Passing anything other than a string is an error.
- **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
- **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.
- **isMobilePhone(str, locale)** - check if the string is a mobile phone number, (locale is one of `['ar-DZ', 'ar-SY', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-CA', 'en-ZA', 'en-ZM', 'es-ES', 'fi-FI', 'fr-FR', 'hu-HU', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR', 'vi-VN', 'zh-CN', 'zh-TW']`).
- **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 @@ -124,6 +124,10 @@ var _isHexColor = require('./lib/isHexColor');

var _isHexColor2 = _interopRequireDefault(_isHexColor);

var _isMD = require('./lib/isMD5');

var _isMD2 = _interopRequireDefault(_isMD);

var _isJSON = require('./lib/isJSON');

var _isJSON2 = _interopRequireDefault(_isJSON);
Expand Down Expand Up @@ -257,6 +261,7 @@ var validator = {
isMultibyte: _isMultibyte2.default, isSurrogatePair: _isSurrogatePair2.default,
isInt: _isInt2.default, isFloat: _isFloat2.default, isDecimal: _isDecimal2.default, isHexadecimal: _isHexadecimal2.default, isDivisibleBy: _isDivisibleBy2.default,
isHexColor: _isHexColor2.default,
isMD5: _isMD2.default,
isJSON: _isJSON2.default,
isNull: _isNull2.default,
isLength: _isLength2.default, isByteLength: _isByteLength2.default,
Expand Down
22 changes: 22 additions & 0 deletions lib/isMD5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

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

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

var _assertString2 = _interopRequireDefault(_assertString);

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

/* eslint-disable no-control-regex */
var md5 = /^[a-f0-9]{32}$/;
/* eslint-enable no-control-regex */

function isMD5(str) {
(0, _assertString2.default)(str);
return md5.test(str);
}
module.exports = exports['default'];
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import isDivisibleBy from './lib/isDivisibleBy';

import isHexColor from './lib/isHexColor';

import isMD5 from './lib/isMD5';

import isJSON from './lib/isJSON';
import isNull from './lib/isNull';

Expand Down Expand Up @@ -93,6 +95,7 @@ const validator = {
isMultibyte, isSurrogatePair,
isInt, isFloat, isDecimal, isHexadecimal, isDivisibleBy,
isHexColor,
isMD5,
isJSON,
isNull,
isLength, isByteLength,
Expand Down
10 changes: 10 additions & 0 deletions src/lib/isMD5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assertString from './util/assertString';

/* eslint-disable no-control-regex */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this lint suppression necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure really so I followed the existing src with regex (lib/isAscii.js) which also has lint suppression.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks safe to remove :)

const md5 = /^[a-f0-9]{32}$/;
/* eslint-enable no-control-regex */

export default function isMD5(str) {
assertString(str);
return md5.test(str);
}
18 changes: 18 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,24 @@ describe('Validators', function () {
});
});

it('should validate md5 strings', function () {
test({
validator: 'isMD5',
valid: [
'd94f3f016ae679c3008de268209132f2',
'751adbc511ccbe8edf23d486fa4581cd',
'88dae00e614d8f24cfd5a8b3f8002e93',
'0bf1c35032a71a14c2f719e5a14c1e96',
],
invalid: [
'KYT0bf1c35032a71a14c2f719e5a14c1',
'q94375dj93458w34',
'39485729348',
'%&FHKJFvk',
],
});
});

it('should validate null strings', function () {
test({
validator: 'isNull',
Expand Down
10 changes: 10 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,15 @@
return hexcolor.test(str);
}

/* eslint-disable no-control-regex */
var md5 = /^[a-f0-9]{32}$/;
/* eslint-enable no-control-regex */

function isMD5(str) {
assertString(str);
return md5.test(str);
}

function isJSON(str) {
assertString(str);
try {
Expand Down Expand Up @@ -1097,6 +1106,7 @@
isMultibyte: isMultibyte, isSurrogatePair: isSurrogatePair,
isInt: isInt, isFloat: isFloat, isDecimal: isDecimal, isHexadecimal: isHexadecimal, isDivisibleBy: isDivisibleBy,
isHexColor: isHexColor,
isMD5: isMD5,
isJSON: isJSON,
isNull: isNull,
isLength: isLength, isByteLength: isByteLength,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.