Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Validator | Description
--------------------------------------- | --------------------------------------
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false<br/>`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.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a breaking change, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it's only a change to the docs
They only document the new options now, but the current method still works. See also the tests that are copied. I chose to only document the new options since the README is already long enough, in #2154 we can discuss future methods of making sure all possible ways of using validators are documented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>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).<br/><br/>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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 7 additions & 5 deletions src/lib/isAfter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import assertString from './util/assertString';
import toDate from './toDate';

export default function isAfter(str, date = String(new Date())) {
assertString(str);
const comparison = toDate(date);
const original = toDate(str);
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 comparison = toDate(comparisonDate);
const original = toDate(date);
return !!(original && comparison && original > comparison);
}
1 change: 1 addition & 0 deletions src/lib/toDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assertString from './util/assertString';

export default function toDate(date) {
assertString(date);

date = Date.parse(date);
return !isNaN(date) ? new Date(date) : null;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 56 additions & 0 deletions test/testFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import assert from 'assert';
import { format } from 'util';
import validator from '../src/index';

export default function test(options) {
const 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) {
const 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) {
const 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) {
const warning = format(
'validator.%s(%s) passed but should have failed',
options.validator, args.join(', ')
);

throw new Error(warning);
}
});
}
}
File renamed without changes.
95 changes: 10 additions & 85 deletions test/validators.js → test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,10 @@ import fs from 'fs';
import { format } from 'util';
import vm from 'vm';
import validator from '../src/index';
import test from './testFunctions';

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({
Expand All @@ -74,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`,
'[email protected]',
'[email protected]',
'[email protected]',
Expand All @@ -90,10 +40,10 @@ describe('Validators', () => {
'[email protected].',
'[email protected]',
'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`,
'[email protected] m',
'[email protected] m',
'[email protected] m',
Expand Down Expand Up @@ -128,10 +78,10 @@ describe('Validators', () => {
'[email protected]',
'[email protected]',
'[email protected]',
`${repeat('a', 30)}@gmail.com`,
`${'a'.repeat(30)}@gmail.com`,
],
invalid: [
`${repeat('a', 31)}@gmail.com`,
`${'a'.repeat(31)}@gmail.com`,
'[email protected]',
'[email protected]',
'[email protected]',
Expand Down Expand Up @@ -5043,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',
Expand Down
61 changes: 61 additions & 0 deletions test/validators/isAfter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import test from '../testFunctions';

describe('isAfter', () => {
it('should validate dates against a start date', () => {
test({
validator: 'isAfter',
args: [{ comparisonDate: '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: [{ comparisonDate: '2011-08-03' }],
valid: ['2015-09-17'],
invalid: ['invalid date'],
});

test({
validator: 'isAfter',
args: [{ comparisonDate: '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'],
});
});
});
});