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. e.g. [`1 week`, `2 days`, `1h`, `30m`, `15 s`].<br/><br/>It is designed to match the format used by the [ms](https://github.com/vercel/ms) package.
**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 @@ -245,6 +246,7 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isDuration,
};

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

const BaseDurationUnits = [
'Years',
'Year',
'Yrs',
'Yr',
'Y',
'Weeks',
'Week',
'W',
'Days',
'Day',
'D',
'Hours',
'Hour',
'Hrs',
'Hr',
'H',
'Minutes',
'Minute',
'Mins',
'Min',
'M',
'Seconds',
'Second',
'Secs',
'Sec',
's',
'Milliseconds',
'Millisecond',
'Msecs',
'Msec',
'Ms',
];

const AllDurationUnits = new Set(BaseDurationUnits.reduce((acc, unit) => acc.concat([
unit, unit.toUpperCase(), unit.toLowerCase()]), []));

/**
* Checks if the string is a valid duration.
* It is designed to match the format used by the [ms](https://github.com/vercel/ms) package.
* The duration can be "1 week","2 days","1h", "30m", "15 s", etc.
*/
export default function isDuration(value) {
assertString(value);

// Match number (integer or decimal) optionally followed by a unit
// Supports: "1", "1.5", ".5", "-1", "-1.5", "-.5", "1h", "1.5 hours", etc.
// Using indexed capture groups for Node.js <10 compatibility
const match = value.match(/^(-?(?:\d+(?:\.\d+)?|\.\d+))(?:\s?([a-zA-Z]+))?$/);

if (!match) {
return false;
}

const unit = match[2];

return unit === undefined || AllDurationUnits.has(unit);
}
44 changes: 44 additions & 0 deletions test/validators/isDuration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe } from 'mocha';
import test from '../testFunctions';

describe('isDuration', () => {
it('should validate duration strings', () => {
test({
validator: 'isDuration',
valid: [
'1 week',
'2 days',
'1h',
'30m',
'15 s',
'100ms',
'1.5h',
'2.5 weeks',
'-1d',
'-200',
],
invalid: [
'',
'abc',
'1 invalid',
'week 1',
'1.2.3h',
'+1h', // plus sign is not allowed as in `ms` package
'+200', // plus sign is not allowed as in `ms` package
],
});
});

it('should accept various unit formats', () => {
test({
validator: 'isDuration',
valid: [
'1 Year',
'2 WEEKS',
'3 days',
'4H',
'5m',
],
});
});
});
Loading