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

IsDate String Validation #1170

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -157,6 +157,7 @@ Sanitizer | Description
**trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input.
**whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`.
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
**isDate** | Check whether the string is a date

### XSS Sanitization

Expand Down
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ var _isDecimal = _interopRequireDefault(require("./lib/isDecimal"));

var _isHexadecimal = _interopRequireDefault(require("./lib/isHexadecimal"));

var _isOctal = _interopRequireDefault(require("./lib/isOctal"));

var _isDivisibleBy = _interopRequireDefault(require("./lib/isDivisibleBy"));

var _isHexColor = _interopRequireDefault(require("./lib/isHexColor"));
Expand Down Expand Up @@ -155,7 +157,11 @@ var _normalizeEmail = _interopRequireDefault(require("./lib/normalizeEmail"));

var _isSlug = _interopRequireDefault(require("./lib/isSlug"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
var _isDate = _interopRequireDefault(require("./lib/isDate"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

Expand Down Expand Up @@ -196,6 +202,7 @@ var validator = {
isFloatLocales: _isFloat.locales,
isDecimal: _isDecimal.default,
isHexadecimal: _isHexadecimal.default,
isOctal: _isOctal.default,
isDivisibleBy: _isDivisibleBy.default,
isHexColor: _isHexColor.default,
isISRC: _isISRC.default,
Expand Down Expand Up @@ -242,7 +249,8 @@ var validator = {
isWhitelisted: _isWhitelisted.default,
normalizeEmail: _normalizeEmail.default,
toString: toString,
isSlug: _isSlug.default
isSlug: _isSlug.default,
isDate: _isDate.default
};
var _default = validator;
exports.default = _default;
Expand Down
18 changes: 18 additions & 0 deletions lib/isDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

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

var _assertString = _interopRequireDefault(require("./util/assertString"));

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

function isDate(str) {
(0, _assertString.default)(str);
return (new Date(str) !== 'Invalid Date') && !isNaN(new Date(str));
}

module.exports = exports.default;
module.exports.default = exports.default;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import isWhitelisted from './lib/isWhitelisted';
import normalizeEmail from './lib/normalizeEmail';

import isSlug from './lib/isSlug';
import isDate from './lib/isDate';

const version = '11.1.0';

Expand Down Expand Up @@ -186,6 +187,7 @@ const validator = {
normalizeEmail,
toString,
isSlug,
isDate,
};

export default validator;
6 changes: 6 additions & 0 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import assertString from './util/assertString';

export default function isDate(str) {
assertString(str);
return (new Date(str) !== 'Invalid Date') && !isNaN(new Date(str));
}
15 changes: 15 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7079,4 +7079,19 @@ describe('Validators', () => {
],
});
});

it('should validate that the string is a date or not', () => {
test({
validator: 'isDate',
args: ['any'],
valid: ['10-11-2009', '10/11/2009', '2009-11-10T07:00:00+0000'],
invalid: [
'gdgfdg',
'1000033',
'ccxzcz',
'foo',
'',
],
});
});
});
8 changes: 7 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,11 @@ function isSlug(str) {
return charsetRegex.test(str);
}

function isDate(str) {
assertString(str);
return (new Date(str) !== 'Invalid Date') && !isNaN(new Date(str));
}

var version = '11.1.0';
var validator = {
version: version,
Expand Down Expand Up @@ -2118,7 +2123,8 @@ var validator = {
isWhitelisted: isWhitelisted,
normalizeEmail: normalizeEmail,
toString: toString,
isSlug: isSlug
isSlug: isSlug,
isDate: isDate
};

return validator;
Expand Down