@hmcts/rpx-xui-common-lib 1.4.30-date-component-common-validation → 1.4.31-date-component-validation-fixes

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4513,6 +4513,7 @@ var GovUkCheckboxesComponent = /** @class */ (function () {
4513
4513
  * */
4514
4514
  var GovUkDateComponent = /** @class */ (function () {
4515
4515
  function GovUkDateComponent() {
4516
+ this.isOptional = false;
4516
4517
  }
4517
4518
  /**
4518
4519
  * @return {?}
@@ -4532,20 +4533,45 @@ var GovUkDateComponent = /** @class */ (function () {
4532
4533
  * @private
4533
4534
  * @param {?} d
4534
4535
  * @param {?} month
4536
+ * @param {?} year
4535
4537
  * @return {?}
4536
4538
  */
4537
4539
  GovUkDateComponent.prototype.isValidDate = /**
4538
4540
  * @private
4539
4541
  * @param {?} d
4540
4542
  * @param {?} month
4543
+ * @param {?} year
4541
4544
  * @return {?}
4542
4545
  */
4543
- function (d, month) {
4546
+ function (d, month, year) {
4544
4547
  /** @type {?} */
4545
- var dateCheck = d instanceof Date && !isNaN(d.getTime());
4548
+ var dateCheck = !isNaN(d.getTime());
4549
+ // Month mismatch occurs if the provided day or month are invalid, or either is omitted. **Note:** This is insufficient for
4550
+ // checking date validity when the year is omitted because it defaults to 1900 - an extra check is required
4546
4551
  /** @type {?} */
4547
- var leapYearCheck = d.getMonth() === month;
4548
- return dateCheck && leapYearCheck;
4552
+ var monthMatch = d.getMonth() === month;
4553
+ /** @type {?} */
4554
+ var yearMatch = d.getFullYear() === year;
4555
+ return dateCheck && monthMatch && yearMatch;
4556
+ };
4557
+ /**
4558
+ * @private
4559
+ * @param {?} value
4560
+ * @return {?}
4561
+ */
4562
+ GovUkDateComponent.prototype.isEmpty = /**
4563
+ * @private
4564
+ * @param {?} value
4565
+ * @return {?}
4566
+ */
4567
+ function (value) {
4568
+ // Note: Intentional use of == to check for null or undefined
4569
+ /* eslint-disable eqeqeq */
4570
+ /* tslint:disable:triple-equals */
4571
+ // NaN and < 0 checks required for month field
4572
+ return value == null || value === '' || isNaN(value) || value < 0;
4573
+ /* eslint-enable eqeqeq */
4574
+ /* tslint:enable:triple-equals */
4549
4575
  };
4550
4576
  /**
4551
4577
  * @return {?}
@@ -4555,8 +4581,7 @@ var GovUkDateComponent = /** @class */ (function () {
4555
4581
  */
4556
4582
  function () {
4557
4583
  var _this = this;
4558
- /** @type {?} */
4559
- var res = (/**
4584
+ return (/**
4560
4585
  * @return {?}
4561
4586
  */
4562
4587
  function () {
@@ -4566,11 +4591,13 @@ var GovUkDateComponent = /** @class */ (function () {
4566
4591
  var month = _this.formGroup.get(_this.month).value - 1;
4567
4592
  /** @type {?} */
4568
4593
  var year = _this.formGroup.get(_this.year).value;
4569
- /** @type {?} */
4570
- var isValid = _this.isValidDate(new Date(year, month, day), month);
4571
- return !isValid ? { dateComponent: true } : null;
4594
+ // Validation should pass if the date field is optional and day, month, and year are all empty
4595
+ if (_this.isOptional && _this.isEmpty(day) && _this.isEmpty(month) && _this.isEmpty(year)) {
4596
+ return null;
4597
+ }
4598
+ // + to coerce year to a number
4599
+ return !_this.isValidDate(new Date(year, month, day), month, +year) ? { dateComponent: true } : null;
4572
4600
  });
4573
- return res;
4574
4601
  };
4575
4602
  GovUkDateComponent.decorators = [
4576
4603
  { type: Component, args: [{
@@ -4584,7 +4611,8 @@ var GovUkDateComponent = /** @class */ (function () {
4584
4611
  GovUkDateComponent.propDecorators = {
4585
4612
  config: [{ type: Input }],
4586
4613
  errorMessage: [{ type: Input }],
4587
- formGroup: [{ type: Input }]
4614
+ formGroup: [{ type: Input }],
4615
+ isOptional: [{ type: Input }]
4588
4616
  };
4589
4617
  return GovUkDateComponent;
4590
4618
  }());