Skip to content

Commit 811ddee

Browse files
committed
Release 6.0.2
1 parent db36941 commit 811ddee

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### [6.0.2]
2+
- Fix issue with date/min-date/max-date validators with Moment.js and NgbDateStruct for months January and December
13
### [6.0.1]
24
- Date supports Moment.js
35
### [6.0.0]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
33
"name": "ngx-custom-validators",
4-
"version": "6.0.1",
4+
"version": "6.0.2",
55
"description": "Angular custom directives for validation",
66
"scripts": {
77
"ng": "ng",

src/app/date/validator.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ describe('Date', () => {
2020
expect(date(control)).toBeNull();
2121
});
2222

23+
it('"moment(2018-01-01)" should be date', () => {
24+
const control = new FormControl(moment('2018-01-01'));
25+
expect(date(control)).toBeNull();
26+
});
27+
28+
it('"moment(2018-12-31)" should be date', () => {
29+
const control = new FormControl(moment('2018-12-31'));
30+
expect(date(control)).toBeNull();
31+
});
32+
33+
it('"object { year: 2017, month: 1, day: 11}" should be date', () => {
34+
const control = new FormControl({ year: 2017, month: 1, day: 11});
35+
expect(date(control)).toBeNull();
36+
});
37+
38+
it('"object { year: 2017, month: 12, day: 11}" should be date', () => {
39+
const control = new FormControl({ year: 2017, month: 12, day: 11});
40+
expect(date(control)).toBeNull();
41+
});
42+
2343
it('"2013-13-12" should not be date', () => {
2444
const control = new FormControl('2013-13-12');
2545
expect(date(control)).toEqual(error);
@@ -34,4 +54,14 @@ describe('Date', () => {
3454
const control = new FormControl(moment('2018-17-56'));
3555
expect(date(control)).toEqual(error);
3656
});
57+
58+
it('"moment(2018-13-01)" should not be date', () => {
59+
const control = new FormControl(moment('2018-13-01'));
60+
expect(date(control)).toEqual(error);
61+
});
62+
63+
it('"object { year: 2017, month: 13, day: 11}" should not be date', () => {
64+
const control = new FormControl({ year: 2017, month: 13, day: 11});
65+
expect(date(control)).toEqual(error);
66+
});
3767
});

src/app/min-date/validator.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import * as moment from 'moment';
12
import { FormControl } from '@angular/forms';
2-
33
import { minDate } from './validator';
44

5+
56
describe('MinDate', () => {
67
let control: FormControl;
78

@@ -30,6 +31,11 @@ describe('MinDate', () => {
3031
expect(minDate('2016-09-09')(control)).toBeNull();
3132
});
3233

34+
it('"Date(2016-09-10)" moment should equal to "null"', () => {
35+
control = new FormControl('2016-09-10');
36+
expect(minDate(moment('2016-09-09'))(control)).toBeNull();
37+
});
38+
3339
it('() => Date("2016-09-08)" should equal to "{minDate: true, error: \'lower than minDate\'}"', () => {
3440
control = new FormControl('2016-09-08');
3541
expect(minDate('2016-09-09')(control)).toEqual({minDate: true, error: 'lower than minDate'});
@@ -64,6 +70,12 @@ describe('MinDate', () => {
6470
expect(minDate(obj)(control)).toEqual({minDate: true, error: 'lower than minDate'});
6571
});
6672

73+
it('Date object { year: 2017, month: 11, day: 11} moment should equal to "{minDate: true, error: \'lower than minDate\'}"', () => {
74+
const obj = { year: 2017, month: 11, day: 11};
75+
control = new FormControl(moment('2017-10-01'));
76+
expect(minDate(obj)(control)).toEqual({minDate: true, error: 'lower than minDate'});
77+
});
78+
6779
it('Date form control should equal to "null"', () => {
6880
const control2 = new FormControl('2017-01-01');
6981
control = new FormControl('2018-11-01');

src/app/util/lang.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export function parseDate(obj: any): string {
1111
// Moment.js
1212
if (obj._d instanceof Date) {
1313
const d = obj._d as Date;
14-
return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`;
14+
return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
1515
}
1616

1717
// NgbDateStruct
1818
if (typeof obj === 'object' && obj.year != null && obj.month != null && obj.day != null) {
19-
return `${obj.year}-${+obj.month - 1}-${obj.day}`;
19+
return `${obj.year}-${+obj.month}-${obj.day}`;
2020
}
2121
} catch (e) {}
2222
return obj;

0 commit comments

Comments
 (0)