Repository: validatorjs/validator.js
Pull request #1654 (2021-04-21 12:40:52)

feat(isRFC3339): Disallow prepended and appended strings to RFC 3339 date-time
Fixes validatorjs/validator.js#1653

Adds test cases for invalid RFC 3339 strings as valid `date-time` strings, but prepended and appended with the word "nonsense".

Also adds equivalent test cases for ISO 8601 validator (but did not change the implementation as it was already OK).

## Checklist

- [x] PR contains only changes related; no stray files, etc.
- [x] README updated (where applicable)
- [x] Tests written (where applicable)


codecov[bot]: # [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) Report
> Merging [#1654](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) (9658447) into [master](https://codecov.io/gh/validatorjs/validator.js/commit/24b3fd3309d1057dd87af3a568def0db01dbaade?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) (24b3fd3) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/validatorjs/validator.js/pull/1654/graphs/tree.svg?width=650&height=150&src=pr&token=YzR3kbN1m2&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs)](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs)

```diff
@@            Coverage Diff            @@
##            master     #1654   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          101       101           
  Lines         1854      1854           
=========================================
  Hits          1854      1854           
```


| [Impacted Files](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs) | Coverage Δ | |
|---|---|---|
| [src/lib/isRFC3339.js](https://codecov.io/gh/validatorjs/validator.js/pull/1654/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs#diff-c3JjL2xpYi9pc1JGQzMzMzkuanM=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs). Last update [24b3fd3...9658447](https://codecov.io/gh/validatorjs/validator.js/pull/1654?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=validatorjs).


Patch:
diff --git a/src/lib/isRFC3339.js b/src/lib/isRFC3339.js
index 5b3eaba..48b025e 100644
--- a/src/lib/isRFC3339.js
+++ b/src/lib/isRFC3339.js
@@ -19,7 +19,7 @@ const partialTime = new RegExp(`${timeHour.source}:${timeMinute.source}:${timeSe
 const fullDate = new RegExp(`${dateFullYear.source}-${dateMonth.source}-${dateMDay.source}`);
 const fullTime = new RegExp(`${partialTime.source}${timeOffset.source}`);
 
-const rfc3339 = new RegExp(`${fullDate.source}[ tT]${fullTime.source}`);
+const rfc3339 = new RegExp(`^${fullDate.source}[ tT]${fullTime.source}$`);
 
 export default function isRFC3339(str) {
   assertString(str);


Test patch:
diff --git a/test/validators.js b/test/validators.js
index 31b7f96..56e581d 100644
--- a/test/validators.js
+++ b/test/validators.js
@@ -8925,6 +8925,8 @@ describe('Validators', () => {
     '2010-02-18T16:23.33.600',
     '2010-02-18T16,25:23:48,444',
     '2010-13-1',
+    'nonsense2021-01-01T00:00:00Z',
+    '2021-01-01T00:00:00Znonsense',
   ];
 
   it('should validate ISO 8601 dates', () => {
@@ -9107,6 +9109,8 @@ describe('Validators', () => {
         '2009-05-00 14:39:22+0600',
         '2009-00-1 14:39:22Z',
         '2009-05-19T14:39:22',
+        'nonsense2021-01-01T00:00:00Z',
+        '2021-01-01T00:00:00Znonsense',
       ],
     });
   });