-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add isDuration validator #2669
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
Open
os-moussao
wants to merge
6
commits into
validatorjs:master
Choose a base branch
from
os-moussao:feat/is-duration-validator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−0
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9524e8
feat: add isDuration validator
os-moussao 36345c6
chore: add isDuration description to readme
os-moussao 9e7fbd1
fix: potential voulnerability reported in review
os-moussao 9191014
chore: unuse named groups for backward compability
os-moussao 9f764b2
fix: unuse flatMap for backward compatibility
os-moussao b1205f5
fix: backward compatibility issues
os-moussao 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
| 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.flatMap(unit => [ | ||
| 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); | ||
|
|
||
| // using the same number regex used in the `ms` package | ||
| const match = value.match(/^(?<nbr>-?(?:\d+)?\.?\d+)(?:\s?(?<unit>[a-zA-Z]+))?$/); | ||
|
|
||
| if (!match || !match.groups) { | ||
| return false; | ||
| } | ||
|
|
||
| const { unit } = match.groups; | ||
|
|
||
| return unit === undefined || AllDurationUnits.has(unit); | ||
| } | ||
This file contains hidden or 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,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', | ||
| ], | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.