Skip to content
Open
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 @@ -105,6 +105,7 @@ Validator | Description
**isDate(str [, options])** | check if the string is a valid date. e.g. [`2002-07-15`, new Date()].<br/><br/> `options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`.<br/><br/>`format` is a string and defaults to `YYYY/MM/DD`.<br/><br/>`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject strings different from `format`.<br/><br/> `delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`.
**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.<br/>**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'.
**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another.
**isDuration(str)** | check if the string is a valid duration.
**isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number].
**isEmail(str [, options])** | check if the string is an email.<br/><br/>`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails.
**isEmpty(str [, options])** | check if the string has a length of zero.<br/><br/>`options` is an object which defaults to `{ ignore_whitespace: false }`.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import isIPRange from './lib/isIPRange';
import isFQDN from './lib/isFQDN';
import isDate from './lib/isDate';
import isTime from './lib/isTime';
import isDuration from './lib/isDuration';

import isBoolean from './lib/isBoolean';
import isLocale from './lib/isLocale';
Expand Down Expand Up @@ -242,6 +243,7 @@ const validator = {
isTaxID,
isDate,
isTime,
isDuration,
isLicensePlate,
isVAT,
ibanLocales,
Expand Down
27 changes: 27 additions & 0 deletions src/lib/isDuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assertString from './util/assertString';

const durationRegex = /^(-?(?:\d+)?\.?\d+)(?:\s?([a-zA-Z]+))?$/;

const durationUnits = [
'y', 'yr', 'yrs', 'year', 'years',
'mo', 'month', 'months',
'w', 'week', 'weeks',
'd', 'day', 'days',
'h', 'hr', 'hrs', 'hour', 'hours',
'm', 'min', 'mins', 'minute', 'minutes',
's', 'sec', 'secs', 'second', 'seconds',
'ms', 'msec', 'msecs', 'millisecond', 'milliseconds',
];

export default function isDuration(str) {
assertString(str);
const match = str.match(durationRegex);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '00'.
if (!match) {
return false;
}
const unit = match[2];
if (unit === undefined) {
return true;
}
return durationUnits.indexOf(unit.toLowerCase()) !== -1;
}
38 changes: 38 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7089,6 +7089,44 @@ describe('Validators', () => {
});
});

it('should validate duration strings', () => {
test({
validator: 'isDuration',
valid: [
'123',
'123Yrs',
'123 Yrs',
'45min',
'45 MIN',
'100Ms',
'10 Days',
'7weeks',
'200 SEC',
'1d',
'-1.5 hours',
'0.5s',
'.5s',
'12.5 Hours',
'2h',
'1m',
'5s',
'1y',
'100ms',
],
invalid: [
'abc',
'123bananas',
'123 Yards',
' 123 Hrs',
'123Hrs ',
'123 Hrs',
'123-Hrs',
'',
'0x10Ms',
],
});
});

it('should validate ISINs', () => {
test({
validator: 'isISIN',
Expand Down
Loading