From 5578dad60d8c9dc6a51991eb8335d3d4097c1d94 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Tue, 18 Oct 2022 14:09:27 +0000 Subject: [PATCH 01/34] refactor: allow for splitting tests to different files --- package.json | 2 +- test/testUtils.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ test/validators.js | 52 +--------------------------------------- 3 files changed, 61 insertions(+), 52 deletions(-) create mode 100644 test/testUtils.js diff --git a/package.json b/package.json index 7d505205e..13768746f 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" }, "engines": { "node": ">= 0.10" diff --git a/test/testUtils.js b/test/testUtils.js new file mode 100644 index 000000000..a08277fe2 --- /dev/null +++ b/test/testUtils.js @@ -0,0 +1,59 @@ +import assert from 'assert'; +import { format } from 'util'; +import validator from '../src/index'; + +function repeat(str, count) { + let result = ''; + for (; count; count--) { + result += str; + } + return result; +} + +function test(options) { + let args = options.args || []; + args.unshift(null); + if (options.error) { + options.error.forEach((error) => { + args[0] = error; + try { + assert.throws(() => validator[options.validator](...args)); + } catch (err) { + let warning = format( + 'validator.%s(%s) passed but should error', + options.validator, args.join(', ') + ); + throw new Error(warning); + } + }); + } + if (options.valid) { + options.valid.forEach((valid) => { + args[0] = valid; + if (validator[options.validator](...args) !== true) { + let warning = format( + 'validator.%s(%s) failed but should have passed', + options.validator, args.join(', ') + ); + throw new Error(warning); + } + }); + } + if (options.invalid) { + options.invalid.forEach((invalid) => { + args[0] = invalid; + if (validator[options.validator](...args) !== false) { + let warning = format( + 'validator.%s(%s) passed but should have failed', + options.validator, args.join(', ') + ); + throw new Error(warning); + } + }); + } +} + +export default { + repeat, + test, +}; diff --git a/test/validators.js b/test/validators.js index c0e36bec7..e78f7accf 100644 --- a/test/validators.js +++ b/test/validators.js @@ -3,60 +3,10 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; +import { repeat, test } from './testUtils'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); -function test(options) { - let args = options.args || []; - args.unshift(null); - if (options.error) { - options.error.forEach((error) => { - args[0] = error; - try { - assert.throws(() => validator[options.validator](...args)); - } catch (err) { - let warning = format( - 'validator.%s(%s) passed but should error', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.valid) { - options.valid.forEach((valid) => { - args[0] = valid; - if (validator[options.validator](...args) !== true) { - let warning = format( - 'validator.%s(%s) failed but should have passed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.invalid) { - options.invalid.forEach((invalid) => { - args[0] = invalid; - if (validator[options.validator](...args) !== false) { - let warning = format( - 'validator.%s(%s) passed but should have failed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } -} - -function repeat(str, count) { - let result = ''; - for (; count; count--) { - result += str; - } - return result; -} - describe('Validators', () => { it('should validate email addresses', () => { test({ From afda0bdb168df2572e5d4947fc4a250a9fbb4ad6 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Tue, 18 Oct 2022 14:10:14 +0000 Subject: [PATCH 02/34] feat(isAfter): allow usage of options object --- README.md | 2 +- src/lib/isAfter.js | 16 +++++++++- test/validators.js | 25 ---------------- test/validators/isAfter.js | 61 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 test/validators/isAfter.js diff --git a/README.md b/README.md index 7c8887ed3..e8aba5802 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Validator | Description --------------------------------------- | -------------------------------------- **contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false
`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. **equals(str, comparison)** | check if the string matches the comparison. -**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now). +**isAfter(str [, options])** | check if the string is a date that's after the specified date.

`options` is an object that defaults to `{ date: new Date() }`.
Options:
`date`: Date to compare to. Defaults to `new Date()` (now). **isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

Locale 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

Locale 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', 'bn', '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', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 47bfb537f..f51f875b5 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -1,8 +1,22 @@ import assertString from './util/assertString'; import toDate from './toDate'; -export default function isAfter(str, date = String(new Date())) { +export default function isAfter(str, options) { assertString(str); + + let date; + + if (typeof (options) === 'object') { + date = options.date; + } else { // backwards compatibility: isAfter(str [, date]) + // eslint-disable-next-line prefer-rest-params + date = arguments[1]; + } + + if (!date) { + date = String(new Date()); + } + const comparison = toDate(date); const original = toDate(str); return !!(original && comparison && original > comparison); diff --git a/test/validators.js b/test/validators.js index e78f7accf..31777cc97 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4993,31 +4993,6 @@ describe('Validators', () => { }); }); - it('should validate dates against a start date', () => { - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2011-08-04', new Date(2011, 8, 10).toString()], - invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], - }); - test({ - validator: 'isAfter', - valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], - invalid: ['2010-07-02', new Date(0).toString()], - }); - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2015-09-17'], - invalid: ['invalid date'], - }); - test({ - validator: 'isAfter', - args: ['invalid date'], - invalid: ['invalid date', '2015-09-17'], - }); - }); - it('should validate dates against an end date', () => { test({ validator: 'isBefore', diff --git a/test/validators/isAfter.js b/test/validators/isAfter.js new file mode 100644 index 000000000..485391841 --- /dev/null +++ b/test/validators/isAfter.js @@ -0,0 +1,61 @@ +import { test } from '../testUtils'; + +describe('isAfter', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: [{ date: '2011-08-03' }], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: [{ date: '2011-08-03' }], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: [{ date: 'invalid date' }], + invalid: ['invalid date', '2015-09-17'], + }); + }); + + describe('(legacy syntax)', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: ['invalid date'], + invalid: ['invalid date', '2015-09-17'], + }); + }); + }); +}); From cad192e1905e1ba270e1de3a893940b5d66a9250 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Tue, 18 Oct 2022 14:15:24 +0000 Subject: [PATCH 03/34] style: make options italic --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e8aba5802..06bc47f2c 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Validator | Description --------------------------------------- | -------------------------------------- **contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false
`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. **equals(str, comparison)** | check if the string matches the comparison. -**isAfter(str [, options])** | check if the string is a date that's after the specified date.

`options` is an object that defaults to `{ date: new Date() }`.
Options:
`date`: Date to compare to. Defaults to `new Date()` (now). +**isAfter(str [, options])** | check if the string is a date that's after the specified date.

`options` is an object that defaults to `{ date: new Date() }`.
_Options:_
`date`: Date to compare to. Defaults to `new Date()` (now). **isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

Locale 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

Locale 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', 'bn', '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', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. From 5ba1a4353d41d1f374e54b935e4165af1863386d Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Wed, 19 Oct 2022 09:04:50 +0000 Subject: [PATCH 04/34] refactor: rename test file extension to .test.js --- test/{client-side.js => client-side.test.js} | 0 test/{exports.js => exports.test.js} | 0 test/{sanitizers.js => sanitizers.test.js} | 0 test/{testUtils.js => test-functions.js} | 0 test/{util.js => util.test.js} | 0 test/{validators.js => validators.test.js} | 2 +- test/validators/{isAfter.js => isAfter.test.js} | 2 +- 7 files changed, 2 insertions(+), 2 deletions(-) rename test/{client-side.js => client-side.test.js} (100%) rename test/{exports.js => exports.test.js} (100%) rename test/{sanitizers.js => sanitizers.test.js} (100%) rename test/{testUtils.js => test-functions.js} (100%) rename test/{util.js => util.test.js} (100%) rename test/{validators.js => validators.test.js} (99%) rename test/validators/{isAfter.js => isAfter.test.js} (97%) diff --git a/test/client-side.js b/test/client-side.test.js similarity index 100% rename from test/client-side.js rename to test/client-side.test.js diff --git a/test/exports.js b/test/exports.test.js similarity index 100% rename from test/exports.js rename to test/exports.test.js diff --git a/test/sanitizers.js b/test/sanitizers.test.js similarity index 100% rename from test/sanitizers.js rename to test/sanitizers.test.js diff --git a/test/testUtils.js b/test/test-functions.js similarity index 100% rename from test/testUtils.js rename to test/test-functions.js diff --git a/test/util.js b/test/util.test.js similarity index 100% rename from test/util.js rename to test/util.test.js diff --git a/test/validators.js b/test/validators.test.js similarity index 99% rename from test/validators.js rename to test/validators.test.js index 31777cc97..a2cb2bbe5 100644 --- a/test/validators.js +++ b/test/validators.test.js @@ -3,7 +3,7 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; -import { repeat, test } from './testUtils'; +import { repeat, test } from './test-functions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); diff --git a/test/validators/isAfter.js b/test/validators/isAfter.test.js similarity index 97% rename from test/validators/isAfter.js rename to test/validators/isAfter.test.js index 485391841..eb7bb3611 100644 --- a/test/validators/isAfter.js +++ b/test/validators/isAfter.test.js @@ -1,4 +1,4 @@ -import { test } from '../testUtils'; +import { test } from '../test-functions'; describe('isAfter', () => { it('should validate dates against a start date', () => { From 71102d458dbd8ff5277f5e987436b5cba885035a Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Wed, 19 Oct 2022 10:13:23 +0000 Subject: [PATCH 05/34] refactor: rename test-functions to testFunctions --- test/{test-functions.js => testFunctions.js} | 0 test/validators.test.js | 2 +- test/validators/isAfter.test.js | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename test/{test-functions.js => testFunctions.js} (100%) diff --git a/test/test-functions.js b/test/testFunctions.js similarity index 100% rename from test/test-functions.js rename to test/testFunctions.js diff --git a/test/validators.test.js b/test/validators.test.js index a2cb2bbe5..35200c38d 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -3,7 +3,7 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; -import { repeat, test } from './test-functions'; +import { repeat, test } from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js index eb7bb3611..e3b49c9da 100644 --- a/test/validators/isAfter.test.js +++ b/test/validators/isAfter.test.js @@ -1,4 +1,4 @@ -import { test } from '../test-functions'; +import { test } from '../testFunctions'; describe('isAfter', () => { it('should validate dates against a start date', () => { From f32d228ad57ee6fd5962a3d7d461f12e8d6ce357 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Sun, 23 Oct 2022 15:43:52 +0000 Subject: [PATCH 06/34] refactor: implement suggestion from #2019 review --- src/lib/isAfter.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index f51f875b5..89d6b5344 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -4,18 +4,9 @@ import toDate from './toDate'; export default function isAfter(str, options) { assertString(str); - let date; - - if (typeof (options) === 'object') { - date = options.date; - } else { // backwards compatibility: isAfter(str [, date]) - // eslint-disable-next-line prefer-rest-params - date = arguments[1]; - } - - if (!date) { - date = String(new Date()); - } + // accessing 'arguments' for backwards compatibility: isAfter(str [, date]) + // eslint-disable-next-line prefer-rest-params + const date = (typeof options === 'object' ? options.date : arguments[1]) || String(new Date()); const comparison = toDate(date); const original = toDate(str); From 7799d9b6b9193be20789a90578fc0dd155a7c7bd Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:40:06 +0000 Subject: [PATCH 07/34] refactor: remove custom repeat to use native function --- test/testFunctions.js | 9 --------- test/validators.test.js | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/test/testFunctions.js b/test/testFunctions.js index a08277fe2..35ecbdc36 100644 --- a/test/testFunctions.js +++ b/test/testFunctions.js @@ -2,14 +2,6 @@ import assert from 'assert'; import { format } from 'util'; import validator from '../src/index'; -function repeat(str, count) { - let result = ''; - for (; count; count--) { - result += str; - } - return result; -} - function test(options) { let args = options.args || []; args.unshift(null); @@ -54,6 +46,5 @@ function test(options) { } export default { - repeat, test, }; diff --git a/test/validators.test.js b/test/validators.test.js index 35200c38d..539bbe2ff 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -3,7 +3,7 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; -import { repeat, test } from './testFunctions'; +import { test } from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); @@ -24,9 +24,9 @@ describe('Validators', () => { '"foobar"@example.com', '" foo m端ller "@example.com', '"foo\\@bar"@example.com', - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', 'test@1337.com', @@ -40,10 +40,10 @@ describe('Validators', () => { 'foo@bar.co.uk.', 'z@co.c', 'gmailgmailgmailgmailgmail@gmail.com', - `${repeat('a', 64)}@${repeat('a', 251)}.com`, - `${repeat('a', 65)}@${repeat('a', 250)}.com`, - `${repeat('a', 64)}@${repeat('a', 64)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(251)}.com`, + `${'a'.repeat(65)}@${'a'.repeat(250)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(64)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`, 'test1@invalid.co m', 'test2@invalid.co m', 'test3@invalid.co m', @@ -78,10 +78,10 @@ describe('Validators', () => { 'foobar@gmail.com', 'foo.bar@gmail.com', 'foo.bar@googlemail.com', - `${repeat('a', 30)}@gmail.com`, + `${'a'.repeat(30)}@gmail.com`, ], invalid: [ - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', '.foobar@gmail.com', From 5375f17e2931fe23b112776f5e193728cbe5e27f Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:40:35 +0000 Subject: [PATCH 08/34] refactor: implement suggestion new Date --- src/lib/isAfter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 89d6b5344..24d18fa25 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -6,7 +6,7 @@ export default function isAfter(str, options) { // accessing 'arguments' for backwards compatibility: isAfter(str [, date]) // eslint-disable-next-line prefer-rest-params - const date = (typeof options === 'object' ? options.date : arguments[1]) || String(new Date()); + const date = (typeof options === 'object' ? options.date : arguments[1]) || Date().toString(); const comparison = toDate(date); const original = toDate(str); From ae7a8a68d41fca80836fb7f500a5a88f5fd22ae5 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 15:48:01 +0200 Subject: [PATCH 09/34] Refactor isBefore with options API --- src/lib/isBefore.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 8314f0e14..ecbc658f2 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,9 +1,12 @@ import assertString from './util/assertString'; import toDate from './toDate'; -export default function isBefore(str, date = String(new Date())) { +export default function isBefore(str, options = {}) { assertString(str); - const comparison = toDate(date); + + const { comparisonDate = Date().toString() } = options; + const comparison = toDate(comparisonDate); const original = toDate(str); + return !!(original && comparison && original < comparison); } From f5b30686370048b7c546bf154adde9f9048f977c Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 15:48:17 +0200 Subject: [PATCH 10/34] Refactor isBefore tests --- test/validators.test.js | 35 ----------------------------- test/validators/isBefore.test.js | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 test/validators/isBefore.test.js diff --git a/test/validators.test.js b/test/validators.test.js index 539bbe2ff..d16a9d08a 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4993,41 +4993,6 @@ describe('Validators', () => { }); }); - it('should validate dates against an end date', () => { - test({ - validator: 'isBefore', - args: ['08/04/2011'], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - args: [new Date(2011, 7, 4).toString()], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - valid: [ - '2000-08-04', - new Date(0).toString(), - new Date(Date.now() - 86400000).toString(), - ], - invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], - }); - test({ - validator: 'isBefore', - args: ['2011-08-03'], - valid: ['1999-12-31'], - invalid: ['invalid date'], - }); - test({ - validator: 'isBefore', - args: ['invalid date'], - invalid: ['invalid date', '1999-12-31'], - }); - }); - it('should validate IBAN', () => { test({ validator: 'isIBAN', diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js new file mode 100644 index 000000000..08510e84b --- /dev/null +++ b/test/validators/isBefore.test.js @@ -0,0 +1,38 @@ +import { test } from '../testFunctions'; + +describe('isBefore', () => { + it('should validate dates against an end date', () => { + test({ + validator: 'isBefore', + args: [{ comparisonDate: '08/04/2011' }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: new Date(2011, 7, 4).toString() }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + valid: [ + '2000-08-04', + new Date(0).toString(), + new Date(Date.now() - 86400000).toString(), + ], + invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: 'invalid date' }], + invalid: ['invalid date', '1999-12-31'], + }); + }); +}); From 4601b832b41c992b9f9b1400ed2c1b9b280e8311 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 15:59:11 +0200 Subject: [PATCH 11/34] Refactor to simplify logic --- src/lib/isBefore.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index ecbc658f2..a3cce64f7 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,12 +1,11 @@ import assertString from './util/assertString'; -import toDate from './toDate'; -export default function isBefore(str, options = {}) { - assertString(str); +export default function isBefore(date, options = {}) { + assertString(date); const { comparisonDate = Date().toString() } = options; - const comparison = toDate(comparisonDate); - const original = toDate(str); + const comparison = new Date(comparisonDate); + const original = new Date(date); - return !!(original && comparison && original < comparison); + return !!(original < comparison); } From 50318a38ae9cd3f32f40fc7258840d0d4b94a36c Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 16:03:20 +0200 Subject: [PATCH 12/34] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 06bc47f2c..52fdc87c1 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ Validator | Description **isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html). **isBase58(str)** | check if a string is base58 encoded. **isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) -**isBefore(str [, date])** | check if the string is a date that's before the specified date. +**isBefore(str [, options])** | check if the string is a date that's before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.
_Options:_
`comparisonDate`: Date to compare to. Defaults to the current date `Date().toString()` (now). **isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. **isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. From d7efe2794e5d28512a7c8aa651825cba0b8c0019 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 16:17:16 +0200 Subject: [PATCH 13/34] Refactor logic --- src/lib/isBefore.js | 5 +++-- src/lib/toDate.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index a3cce64f7..dae038f73 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,11 +1,12 @@ import assertString from './util/assertString'; +import toDate from './toDate'; export default function isBefore(date, options = {}) { assertString(date); const { comparisonDate = Date().toString() } = options; - const comparison = new Date(comparisonDate); - const original = new Date(date); + const comparison = toDate(comparisonDate); + const original = toDate(date); return !!(original < comparison); } diff --git a/src/lib/toDate.js b/src/lib/toDate.js index 7cf80510e..11abc90c3 100644 --- a/src/lib/toDate.js +++ b/src/lib/toDate.js @@ -2,6 +2,6 @@ import assertString from './util/assertString'; export default function toDate(date) { assertString(date); - date = Date.parse(date); - return !isNaN(date) ? new Date(date) : null; + + return new Date(date); } From d8caed718a8bcb3d6bcd2801a4b9e753c83e99af Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 18:54:17 +0200 Subject: [PATCH 14/34] Improve README formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 52fdc87c1..d657ad9b7 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ Validator | Description **isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html). **isBase58(str)** | check if a string is base58 encoded. **isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) -**isBefore(str [, options])** | check if the string is a date that's before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.
_Options:_
`comparisonDate`: Date to compare to. Defaults to the current date `Date().toString()` (now). +**isBefore(str [, options])** | check if the string is a date that's before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.

**Options:**
`comparisonDate`: Date to compare to. Defaults to the current date `Date().toString()` (now). **isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. **isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. From 65a9d7192f0b8a45447300ec3614ec6d81f52266 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 19:44:50 +0200 Subject: [PATCH 15/34] Fix backwards-compat --- src/lib/isBefore.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index dae038f73..556e22e52 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,10 +1,12 @@ import assertString from './util/assertString'; import toDate from './toDate'; -export default function isBefore(date, options = {}) { +export default function isBefore(date, options) { assertString(date); - const { comparisonDate = Date().toString() } = options; + // accessing 'arguments' for backwards compatibility: isBefore(date [, date]) + // eslint-disable-next-line prefer-rest-params + const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); const comparison = toDate(comparisonDate); const original = toDate(date); From ded66ebcbaedc222482fb82e14d51e090bf3d637 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 19:45:25 +0200 Subject: [PATCH 16/34] Remove redundant string assertion --- src/lib/isBefore.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 556e22e52..571a976ae 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,9 +1,6 @@ -import assertString from './util/assertString'; import toDate from './toDate'; export default function isBefore(date, options) { - assertString(date); - // accessing 'arguments' for backwards compatibility: isBefore(date [, date]) // eslint-disable-next-line prefer-rest-params const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); From 62c2e22258dc05fcc1bcc30c3e817f81fb93bbd8 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 19:48:40 +0200 Subject: [PATCH 17/34] Fix comment --- src/lib/isBefore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 571a976ae..efb8e8ae5 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,7 +1,7 @@ import toDate from './toDate'; export default function isBefore(date, options) { - // accessing 'arguments' for backwards compatibility: isBefore(date [, date]) + // accessing 'arguments' for backwards compatibility: isBefore(date [, comparisonDate]) // eslint-disable-next-line prefer-rest-params const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); const comparison = toDate(comparisonDate); From e907cde8e6c8e2a8346da693f130b25f3a882fc6 Mon Sep 17 00:00:00 2001 From: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 25 Oct 2022 19:51:45 +0200 Subject: [PATCH 18/34] Reinstate legacy tests --- test/validators/isBefore.test.js | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js index 08510e84b..129476c36 100644 --- a/test/validators/isBefore.test.js +++ b/test/validators/isBefore.test.js @@ -35,4 +35,41 @@ describe('isBefore', () => { invalid: ['invalid date', '1999-12-31'], }); }); + + describe('(legacy syntax)', () => { + it('should validate dates against an end date', () => { + test({ + validator: 'isBefore', + args: ['08/04/2011'], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [new Date(2011, 7, 4).toString()], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + valid: [ + '2000-08-04', + new Date(0).toString(), + new Date(Date.now() - 86400000).toString(), + ], + invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], + }); + test({ + validator: 'isBefore', + args: ['2011-08-03'], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: ['invalid date'], + invalid: ['invalid date', '1999-12-31'], + }); + }); + }); }); From 44eda79825ac0d504f2f2190c6b20e97c1cac706 Mon Sep 17 00:00:00 2001 From: -l <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:26:23 +0100 Subject: [PATCH 19/34] Change arg name according to code review --- src/lib/isBefore.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index efb8e8ae5..02d53d9d7 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,11 +1,11 @@ import toDate from './toDate'; -export default function isBefore(date, options) { - // accessing 'arguments' for backwards compatibility: isBefore(date [, comparisonDate]) +export default function isBefore(str, options) { + // accessing 'arguments' for backwards compatibility: isBefore(str [, comparisonDate]) // eslint-disable-next-line prefer-rest-params const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); const comparison = toDate(comparisonDate); - const original = toDate(date); + const original = toDate(str); return !!(original < comparison); } From de17c53da033955a3b5a6f2b67ffb5d6fa2c2afe Mon Sep 17 00:00:00 2001 From: -l <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:28:09 +0100 Subject: [PATCH 20/34] Add line break according to code review --- src/lib/isBefore.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 02d53d9d7..b91468650 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -4,6 +4,7 @@ export default function isBefore(str, options) { // accessing 'arguments' for backwards compatibility: isBefore(str [, comparisonDate]) // eslint-disable-next-line prefer-rest-params const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); + const comparison = toDate(comparisonDate); const original = toDate(str); From c145be306874364e60e5a2f8c5c6008d18b1ee22 Mon Sep 17 00:00:00 2001 From: -l <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:31:13 +0100 Subject: [PATCH 21/34] Revert change of simplifying toDate --- src/lib/toDate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/toDate.js b/src/lib/toDate.js index 11abc90c3..7cf80510e 100644 --- a/src/lib/toDate.js +++ b/src/lib/toDate.js @@ -2,6 +2,6 @@ import assertString from './util/assertString'; export default function toDate(date) { assertString(date); - - return new Date(date); + date = Date.parse(date); + return !isNaN(date) ? new Date(date) : null; } From 3cfab417a1cfd78fa2d76fa6bfcc73675a75bfb1 Mon Sep 17 00:00:00 2001 From: -l <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:45:48 +0100 Subject: [PATCH 22/34] Fix whitespace issues --- src/lib/isBefore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index b91468650..415b87d98 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -4,7 +4,7 @@ export default function isBefore(str, options) { // accessing 'arguments' for backwards compatibility: isBefore(str [, comparisonDate]) // eslint-disable-next-line prefer-rest-params const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); - + const comparison = toDate(comparisonDate); const original = toDate(str); From 15182ac2c35e8f8a9934925b60f55dcaaa52c99a Mon Sep 17 00:00:00 2001 From: -l <12937991+pixelbucket-dev@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:04:39 +0100 Subject: [PATCH 23/34] Fix test --- src/lib/isBefore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 415b87d98..478921bf2 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -8,5 +8,5 @@ export default function isBefore(str, options) { const comparison = toDate(comparisonDate); const original = toDate(str); - return !!(original < comparison); + return !!(original && comparison && original < comparison); } From ab6b5effe7ccae02cfc1ac2b69369aea6520f8d2 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 18:05:28 +0100 Subject: [PATCH 24/34] Fix tests --- test/validators/isBefore.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js index 129476c36..b3e55ddda 100644 --- a/test/validators/isBefore.test.js +++ b/test/validators/isBefore.test.js @@ -1,4 +1,4 @@ -import { test } from '../testFunctions'; +import test from '../testFunctions'; describe('isBefore', () => { it('should validate dates against an end date', () => { From 0419de3aa3b8c0fbf63537d230968522b67d5573 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 18:54:28 +0100 Subject: [PATCH 25/34] Format file for consistency with isBefore --- src/lib/isAfter.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index e116e77ce..30998e8d8 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -7,5 +7,6 @@ export default function isAfter(date, options) { const comparison = toDate(comparisonDate); const original = toDate(date); + return !!(original && comparison && original > comparison); } From 25f88c44b0fc7a429cca7d1399411fd47f539e28 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 18:59:48 +0100 Subject: [PATCH 26/34] Remove redundant file --- test/client-side.test.js | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 test/client-side.test.js diff --git a/test/client-side.test.js b/test/client-side.test.js deleted file mode 100644 index f0b5ca72c..000000000 --- a/test/client-side.test.js +++ /dev/null @@ -1,29 +0,0 @@ -import assert from 'assert'; -import validator from '../validator'; -import min from '../validator.min'; - -describe('Minified version', () => { - it('should export the same things as the server-side version', () => { - for (let key in validator) { - if ({}.hasOwnProperty.call(validator, key)) { - assert.strictEqual( - typeof validator[key], - typeof min[key], `Minified version did not export ${key}` - ); - } - } - }); - - it('should be up to date', () => { - assert.strictEqual(min.version, validator.version, 'Minified version mismatch. Run `make min`'); - }); - - it('should validate strings', () => { - assert.strictEqual(min.isEmail('foo@bar.com'), true); - assert.strictEqual(min.isEmail('foo'), false); - }); - - it('should sanitize strings', () => { - assert.strictEqual(min.toBoolean('1'), true); - }); -}); From 6a5ec271e5227b15f649bb9d891fefeada6a289c Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 19:03:23 +0100 Subject: [PATCH 27/34] Remove old tests --- test/validators.test.js | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/test/validators.test.js b/test/validators.test.js index a1079b34f..9bbdc5791 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -5103,41 +5103,6 @@ describe('Validators', () => { }); }); - it('should validate dates against an end date', () => { - test({ - validator: 'isBefore', - args: ['08/04/2011'], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - args: [new Date(2011, 7, 4).toString()], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - valid: [ - '2000-08-04', - new Date(0).toString(), - new Date(Date.now() - 86400000).toString(), - ], - invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], - }); - test({ - validator: 'isBefore', - args: ['2011-08-03'], - valid: ['1999-12-31'], - invalid: ['invalid date'], - }); - test({ - validator: 'isBefore', - args: ['invalid date'], - invalid: ['invalid date', '1999-12-31'], - }); - }); - it('should validate IBAN', () => { test({ validator: 'isIBAN', From 587b3ef10fa8e4517cbaee1d68873802555ec8c5 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:26:28 +0100 Subject: [PATCH 28/34] Add tests for undefined args --- test/validators/isBefore.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js index b3e55ddda..0e13b89b8 100644 --- a/test/validators/isBefore.test.js +++ b/test/validators/isBefore.test.js @@ -34,6 +34,21 @@ describe('isBefore', () => { args: [{ comparisonDate: 'invalid date' }], invalid: ['invalid date', '1999-12-31'], }); + test({ + validator: 'isBefore', + args: [], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [undefined], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: undefined }], // will fall back to the current date + valid: ['1999-06-07'], + }); }); describe('(legacy syntax)', () => { From 05d07eab5d92bb3df0e7315269b6eff747e8051d Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:26:59 +0100 Subject: [PATCH 29/34] Remove arguments: linter error --- src/lib/isBefore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 478921bf2..b444b4356 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -3,7 +3,7 @@ import toDate from './toDate'; export default function isBefore(str, options) { // accessing 'arguments' for backwards compatibility: isBefore(str [, comparisonDate]) // eslint-disable-next-line prefer-rest-params - const comparisonDate = (typeof options === 'object' ? options.comparisonDate : arguments[1]) || Date().toString(); + const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); const comparison = toDate(comparisonDate); const original = toDate(str); From daa1abd11355a12729727adea3cbc1e24d69b60d Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:27:43 +0100 Subject: [PATCH 30/34] Improve comment --- src/lib/isBefore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index b444b4356..18d8ba5b4 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,8 +1,8 @@ import toDate from './toDate'; export default function isBefore(str, options) { - // accessing 'arguments' for backwards compatibility: isBefore(str [, comparisonDate]) - // eslint-disable-next-line prefer-rest-params + // For backwards compatibility: + // isBefore(str [, date]), i.e. `options` could be used as argument for the legacy `date` const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); const comparison = toDate(comparisonDate); From 23478fd460d5de2e1a17b22ba9d3b9f750365760 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:28:37 +0100 Subject: [PATCH 31/34] Use recommended variable name --- src/lib/isBefore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/isBefore.js b/src/lib/isBefore.js index 18d8ba5b4..977d0ad1a 100644 --- a/src/lib/isBefore.js +++ b/src/lib/isBefore.js @@ -1,12 +1,12 @@ import toDate from './toDate'; -export default function isBefore(str, options) { +export default function isBefore(date, options) { // For backwards compatibility: // isBefore(str [, date]), i.e. `options` could be used as argument for the legacy `date` const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); const comparison = toDate(comparisonDate); - const original = toDate(str); + const original = toDate(date); return !!(original && comparison && original < comparison); } From 84151bc541cea3462cc3d5f380481c480ede8c0a Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:30:09 +0100 Subject: [PATCH 32/34] Improve readme text according to code review --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b68af01c1..b5cade874 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ Validator | Description **isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32]. **isBase58(str)** | check if the string is base58 encoded. **isBase64(str [, options])** | check if the string is base64 encoded. `options` is optional and defaults to `{ urlSafe: false }`
when `urlSafe` is true it tests the given base64 encoded string is [url safe][Base64 URL Safe]. -**isBefore(str [, options])** | check if the string is a date that's before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.

**Options:**
`comparisonDate`: Date to compare to. Defaults to the current date `Date().toString()` (now). +**isBefore(str [, options])** | check if the string is a date that is before the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.

**Options:**
`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now). **isBIC(str)** | check if the string is a BIC (Bank Identification Code) or SWIFT code. **isBoolean(str [, options])** | check if the string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If `loose` is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If `loose` is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (e.g.: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. From 18d652bac4347da5d6f8a2b9042c6ce29d917ad8 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Sun, 5 Feb 2023 21:37:33 +0100 Subject: [PATCH 33/34] Make isAfter arguments more robust --- src/lib/isAfter.js | 2 +- test/validators/isAfter.test.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 30998e8d8..149622a0d 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -3,7 +3,7 @@ import toDate from './toDate'; export default function isAfter(date, options) { // For backwards compatibility: // isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date` - const comparisonDate = options?.comparisonDate || options || Date().toString(); + const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString(); const comparison = toDate(comparisonDate); const original = toDate(date); diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js index d771d9198..f0daf8a17 100644 --- a/test/validators/isAfter.test.js +++ b/test/validators/isAfter.test.js @@ -27,6 +27,21 @@ describe('isAfter', () => { args: [{ comparisonDate: 'invalid date' }], invalid: ['invalid date', '2015-09-17'], }); + test({ + validator: 'isAfter', + args: [], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); + test({ + validator: 'isAfter', + args: [undefined], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); + test({ + validator: 'isAfter', + args: [{ comparisonDate: undefined }], // will fall back to the current date + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + }); }); describe('(legacy syntax)', () => { From 57d89454709e47bf1919a444a91848b0f0e31ac3 Mon Sep 17 00:00:00 2001 From: "pixelbucket.dev" <12937991+pixelbucket-dev@users.noreply.github.com> Date: Mon, 6 Feb 2023 21:41:54 +0100 Subject: [PATCH 34/34] Split test cases into given and default end date --- test/validators/isBefore.test.js | 139 +++++++++++++++++++------------ 1 file changed, 84 insertions(+), 55 deletions(-) diff --git a/test/validators/isBefore.test.js b/test/validators/isBefore.test.js index 0e13b89b8..298e5b410 100644 --- a/test/validators/isBefore.test.js +++ b/test/validators/isBefore.test.js @@ -1,58 +1,35 @@ +import { describe } from 'mocha'; import test from '../testFunctions'; describe('isBefore', () => { - it('should validate dates against an end date', () => { - test({ - validator: 'isBefore', - args: [{ comparisonDate: '08/04/2011' }], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - args: [{ comparisonDate: new Date(2011, 7, 4).toString() }], - valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], - invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], - }); - test({ - validator: 'isBefore', - valid: [ - '2000-08-04', - new Date(0).toString(), - new Date(Date.now() - 86400000).toString(), - ], - invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], - }); - test({ - validator: 'isBefore', - args: [{ comparisonDate: '2011-08-03' }], - valid: ['1999-12-31'], - invalid: ['invalid date'], - }); - test({ - validator: 'isBefore', - args: [{ comparisonDate: 'invalid date' }], - invalid: ['invalid date', '1999-12-31'], - }); - test({ - validator: 'isBefore', - args: [], // will fall back to the current date - valid: ['1999-06-07'], - }); - test({ - validator: 'isBefore', - args: [undefined], // will fall back to the current date - valid: ['1999-06-07'], - }); - test({ - validator: 'isBefore', - args: [{ comparisonDate: undefined }], // will fall back to the current date - valid: ['1999-06-07'], + describe('should validate dates a given end date', () => { + describe('new syntax', () => { + test({ + validator: 'isBefore', + args: [{ comparisonDate: '08/04/2011' }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: new Date(2011, 7, 4).toString() }], + valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], + invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: 'invalid date' }], + invalid: ['invalid date', '1999-12-31'], + }); }); - }); - describe('(legacy syntax)', () => { - it('should validate dates against an end date', () => { + describe('legacy syntax', () => { test({ validator: 'isBefore', args: ['08/04/2011'], @@ -65,6 +42,22 @@ describe('isBefore', () => { valid: ['2010-07-02', '2010-08-04', new Date(0).toString()], invalid: ['08/04/2011', new Date(2011, 9, 10).toString()], }); + test({ + validator: 'isBefore', + args: ['2011-08-03'], + valid: ['1999-12-31'], + invalid: ['invalid date'], + }); + test({ + validator: 'isBefore', + args: ['invalid date'], + invalid: ['invalid date', '1999-12-31'], + }); + }); + }); + + describe('should validate dates a default end date', () => { + describe('new syntax', () => { test({ validator: 'isBefore', valid: [ @@ -76,14 +69,50 @@ describe('isBefore', () => { }); test({ validator: 'isBefore', - args: ['2011-08-03'], - valid: ['1999-12-31'], - invalid: ['invalid date'], + args: undefined, // will fall back to the current date + valid: ['1999-06-07'], }); test({ validator: 'isBefore', - args: ['invalid date'], - invalid: ['invalid date', '1999-12-31'], + args: [], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [undefined], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [{ comparisonDate: undefined }], // will fall back to the current date + valid: ['1999-06-07'], + }); + }); + + describe('legacy syntax', () => { + test({ + validator: 'isBefore', + valid: [ + '2000-08-04', + new Date(0).toString(), + new Date(Date.now() - 86400000).toString(), + ], + invalid: ['2100-07-02', new Date(2217, 10, 10).toString()], + }); + test({ + validator: 'isBefore', + args: undefined, // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [], // will fall back to the current date + valid: ['1999-06-07'], + }); + test({ + validator: 'isBefore', + args: [undefined], // will fall back to the current date + valid: ['1999-06-07'], }); }); });