Skip to content

Commit 8e004e0

Browse files
francoatmegafedeciJardel Matias
authored
added isTime validator (#1479)
Co-authored-by: Federico Ciardi <[email protected]> Co-authored-by: Jardel Matias <[email protected]>
1 parent 44224d7 commit 8e004e0

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Validator | Description
163163
**isUppercase(str)** | check if the string is uppercase.
164164
**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`]
165165
**isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
166+
**isTime(str [, options])** | Check if the input is a valid time. e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
166167
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
167168
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>require_port - if set as true isURL will check if port is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.<br/>allow_fragments - if set as false isURL will return false if fragments are present.<br/>allow_query_components - if set as false isURL will return false if query components are present.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
168169
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import isIP from './lib/isIP';
1313
import isIPRange from './lib/isIPRange';
1414
import isFQDN from './lib/isFQDN';
1515
import isDate from './lib/isDate';
16+
import isTime from './lib/isTime';
1617

1718
import isBoolean from './lib/isBoolean';
1819
import isLocale from './lib/isLocale';
@@ -226,6 +227,7 @@ const validator = {
226227
isStrongPassword,
227228
isTaxID,
228229
isDate,
230+
isTime,
229231
isLicensePlate,
230232
isVAT,
231233
ibanLocales,

src/lib/isTime.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import merge from './util/merge';
2+
3+
const default_time_options = {
4+
hourFormat: 'hour24',
5+
mode: 'default',
6+
};
7+
8+
const formats = {
9+
hour24: {
10+
default: /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/,
11+
withSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/,
12+
},
13+
hour12: {
14+
default: /^(0?[1-9]|1[0-2]):([0-5][0-9]) (A|P)M$/,
15+
withSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]) (A|P)M$/,
16+
},
17+
};
18+
19+
export default function isTime(input, options) {
20+
options = merge(options, default_time_options);
21+
if (typeof input !== 'string') return false;
22+
return formats[options.hourFormat][options.mode].test(input);
23+
}

test/validators.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12680,6 +12680,113 @@ describe('Validators', () => {
1268012680
],
1268112681
});
1268212682
});
12683+
it('should validate time', () => {
12684+
test({
12685+
validator: 'isTime',
12686+
valid: [
12687+
'00:00',
12688+
'23:59',
12689+
'9:00',
12690+
],
12691+
invalid: [
12692+
'',
12693+
null,
12694+
undefined,
12695+
0,
12696+
'07:00 PM',
12697+
'23',
12698+
'00:60',
12699+
'00:',
12700+
'01:0 ',
12701+
'001:01',
12702+
],
12703+
});
12704+
test({
12705+
validator: 'isTime',
12706+
args: [{ hourFormat: 'hour24', mode: 'withSeconds' }],
12707+
valid: [
12708+
'23:59:59',
12709+
'00:00:00',
12710+
'9:50:01',
12711+
],
12712+
invalid: [
12713+
'',
12714+
null,
12715+
undefined,
12716+
23,
12717+
'01:00:01 PM',
12718+
'13:00:',
12719+
'00',
12720+
'26',
12721+
'00;01',
12722+
'0 :09',
12723+
'59:59:59',
12724+
'24:00:00',
12725+
'00:59:60',
12726+
'99:99:99',
12727+
'009:50:01',
12728+
],
12729+
});
12730+
test({
12731+
validator: 'isTime',
12732+
args: [{ hourFormat: 'hour12' }],
12733+
valid: [
12734+
'12:59 PM',
12735+
'12:59 AM',
12736+
'01:00 PM',
12737+
'01:00 AM',
12738+
'7:00 AM',
12739+
],
12740+
invalid: [
12741+
'',
12742+
null,
12743+
undefined,
12744+
0,
12745+
'12:59 MM',
12746+
'12:59 MA',
12747+
'12:59 PA',
12748+
'12:59 A M',
12749+
'13:00 PM',
12750+
'23',
12751+
'00:60',
12752+
'00:',
12753+
'9:00',
12754+
'01:0 ',
12755+
'001:01',
12756+
'12:59:00 PM',
12757+
'12:59:00 A M',
12758+
'12:59:00 ',
12759+
],
12760+
});
12761+
test({
12762+
validator: 'isTime',
12763+
args: [{ hourFormat: 'hour12', mode: 'withSeconds' }],
12764+
valid: [
12765+
'12:59:59 PM',
12766+
'2:34:45 AM',
12767+
'7:00:00 AM',
12768+
],
12769+
invalid: [
12770+
'',
12771+
null,
12772+
undefined,
12773+
23,
12774+
'01:00: 1 PM',
12775+
'13:00:',
12776+
'13:00:00 PM',
12777+
'00',
12778+
'26',
12779+
'00;01',
12780+
'0 :09',
12781+
'59:59:59',
12782+
'24:00:00',
12783+
'00:59:60',
12784+
'99:99:99',
12785+
'9:50:01',
12786+
'009:50:01',
12787+
],
12788+
});
12789+
});
1268312790
it('should be valid license plate', () => {
1268412791
test({
1268512792
validator: 'isLicensePlate',

0 commit comments

Comments
 (0)