-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(isAfter): allow usage of options object #2075
Merged
profnandaa
merged 6 commits into
validatorjs:master
from
WikiRik:WikiRik/isAfter-options-refactor
Jan 29, 2023
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11b8502
refactor: change test files to prepare for #1874
WikiRik 9ca431d
feat(isAfter): allow usage of options object
WikiRik 42a8490
refactor: improve comparisonDate
WikiRik 9290c62
refactor: undo breaking change to toDate
WikiRik 6e27cac
Merge remote-tracking branch 'origin/master' into WikiRik/isAfter-opt…
WikiRik 991bb07
Merge branch 'master' into WikiRik/isAfter-options-refactor
WikiRik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
WikiRik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const comparison = toDate(comparisonDate); | ||
const original = toDate(date); | ||
return !!(original && comparison && original > comparison); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks!