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

Add isRFC3339 #816

Merged
merged 4 commits into from
May 3, 2018
Merged
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 @@ -89,6 +89,7 @@ Validator | Description
**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).<br/><br/>`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
**isISO8601(str)** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.
**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code.
**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code).
**isIn(str, values)** | check if the string is in a array of allowed values.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ var _isISO = require('./lib/isISO8601');

var _isISO2 = _interopRequireDefault(_isISO);

var _isRFC = require('./lib/isRFC3339');

var _isRFC2 = _interopRequireDefault(_isRFC);

var _isISO31661Alpha = require('./lib/isISO31661Alpha2');

var _isISO31661Alpha2 = _interopRequireDefault(_isISO31661Alpha);
Expand Down Expand Up @@ -329,6 +333,7 @@ var validator = {
isPostalCode: _isPostalCode2.default,
isCurrency: _isCurrency2.default,
isISO8601: _isISO2.default,
isRFC3339: _isRFC2.default,
isISO31661Alpha2: _isISO31661Alpha2.default,
isBase64: _isBase2.default,
isDataURI: _isDataURI2.default,
Expand Down
39 changes: 39 additions & 0 deletions lib/isRFC3339.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

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

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

var _assertString2 = _interopRequireDefault(_assertString);

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

/* Based on https://tools.ietf.org/html/rfc3339#section-5.6 */

var dateFullYear = /[0-9]{4}/;
var dateMonth = /(0[1-9]|1[0-2])/;
var dateMDay = /([12]\d|0[1-9]|3[01])/;

var timeHour = /([01][0-9]|2[0-3])/;
var timeMinute = /[0-5][0-9]/;
var timeSecond = /([0-5][0-9]|60)/;

var timeSecFrac = /(\.[0-9]+)?/;
var timeNumOffset = new RegExp('[-+]' + timeHour.source + ':' + timeMinute.source);
var timeOffset = new RegExp('([zZ]|' + timeNumOffset.source + ')');

var partialTime = new RegExp(timeHour.source + ':' + timeMinute.source + ':' + timeSecond.source + timeSecFrac.source);

var fullDate = new RegExp(dateFullYear.source + '-' + dateMonth.source + '-' + dateMDay.source);
var fullTime = new RegExp('' + partialTime.source + timeOffset.source);

var rfc3339 = new RegExp(fullDate.source + '[ tT]' + fullTime.source);

function isRFC3339(str) {
(0, _assertString2.default)(str);
return rfc3339.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 @@ -66,6 +66,7 @@ import isMobilePhone from './lib/isMobilePhone';
import isCurrency from './lib/isCurrency';

import isISO8601 from './lib/isISO8601';
import isRFC3339 from './lib/isRFC3339';
import isISO31661Alpha2 from './lib/isISO31661Alpha2';

import isBase64 from './lib/isBase64';
Expand Down Expand Up @@ -144,6 +145,7 @@ const validator = {
isPostalCode,
isCurrency,
isISO8601,
isRFC3339,
isISO31661Alpha2,
isBase64,
isDataURI,
Expand Down
27 changes: 27 additions & 0 deletions src/lib/isRFC3339.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assertString from './util/assertString';

/* Based on https://tools.ietf.org/html/rfc3339#section-5.6 */

const dateFullYear = /[0-9]{4}/;
const dateMonth = /(0[1-9]|1[0-2])/;
const dateMDay = /([12]\d|0[1-9]|3[01])/;

const timeHour = /([01][0-9]|2[0-3])/;
const timeMinute = /[0-5][0-9]/;
const timeSecond = /([0-5][0-9]|60)/;

const timeSecFrac = /(\.[0-9]+)?/;
const timeNumOffset = new RegExp(`[-+]${timeHour.source}:${timeMinute.source}`);
const timeOffset = new RegExp(`([zZ]|${timeNumOffset.source})`);

const partialTime = new RegExp(`${timeHour.source}:${timeMinute.source}:${timeSecond.source}${timeSecFrac.source}`);

const fullDate = new RegExp(`${dateFullYear.source}-${dateMonth.source}-${dateMDay.source}`);
const fullTime = new RegExp(`${partialTime.source}${timeOffset.source}`);

const rfc3339 = new RegExp(`${fullDate.source}[ tT]${fullTime.source}`);

export default function isRFC3339(str) {
assertString(str);
return rfc3339.test(str);
}
35 changes: 35 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5078,6 +5078,41 @@ describe('Validators', function () {
});
});

it('should validate RFC 3339 dates', function () {
test({
validator: 'isRFC3339',
valid: [
'2009-05-19 14:39:22-06:00',
'2009-05-19 14:39:22+06:00',
'2009-05-19 14:39:22Z',
'2009-05-19T14:39:22-06:00',
'2009-05-19T14:39:22Z',
'2010-02-18T16:23:48.3-06:00',
'2010-02-18t16:23:33+06:00',
'2010-02-18t16:23:33+06:00',
'2010-02-18t16:12:23.23334444z',
'2010-02-18T16:23:55.2283Z',
'2009-05-19 14:39:22.500Z',
'2009-05-19 14:39:55Z',
'2009-05-31 14:39:55Z',
'2009-05-31 14:53:60Z',
'2010-02-18t00:23:23.33+06:00',
'2010-02-18t00:23:32.33+00:00',
'2010-02-18t00:23:32.33+23:00',
],
invalid: [
'2010-02-18t00:23:32.33+24:00',
'2009-05-31 14:60:55Z',
'2010-02-18t24:23.33+0600',
'2009-05-00 1439,55Z',
'2009-13-19 14:39:22-06:00',
'2009-05-00 14:39:22+0600',
'2009-00-1 14:39:22Z',
'2009-05-19T14:39:22',
],
});
});

it('should validate ISO 3166-1 alpha 2 country codes', function () {
// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
test({
Expand Down
27 changes: 27 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,32 @@ function isISO8601(str) {
return iso8601.test(str);
}

/* Based on https://tools.ietf.org/html/rfc3339#section-5.6 */

var dateFullYear = /[0-9]{4}/;
var dateMonth = /(0[1-9]|1[0-2])/;
var dateMDay = /([12]\d|0[1-9]|3[01])/;

var timeHour = /([01][0-9]|2[0-3])/;
var timeMinute = /[0-5][0-9]/;
var timeSecond = /([0-5][0-9]|60)/;

var timeSecFrac = /(\.[0-9]+)?/;
var timeNumOffset = new RegExp('[-+]' + timeHour.source + ':' + timeMinute.source);
var timeOffset = new RegExp('([zZ]|' + timeNumOffset.source + ')');

var partialTime = new RegExp(timeHour.source + ':' + timeMinute.source + ':' + timeSecond.source + timeSecFrac.source);

var fullDate = new RegExp(dateFullYear.source + '-' + dateMonth.source + '-' + dateMDay.source);
var fullTime = new RegExp('' + partialTime.source + timeOffset.source);

var rfc3339 = new RegExp(fullDate.source + '[ tT]' + fullTime.source);

function isRFC3339(str) {
assertString(str);
return rfc3339.test(str);
}

// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
var validISO31661Alpha2CountriesCodes = ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW'];

Expand Down Expand Up @@ -1554,6 +1580,7 @@ var validator = {
isPostalCode: isPostalCode,
isCurrency: isCurrency,
isISO8601: isISO8601,
isRFC3339: isRFC3339,
isISO31661Alpha2: isISO31661Alpha2,
isBase64: isBase64,
isDataURI: isDataURI,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.