@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.
@@ -4556,6 +4556,7 @@
4556
4556
  * */
4557
4557
  var GovUkDateComponent = /** @class */ (function () {
4558
4558
  function GovUkDateComponent() {
4559
+ this.isOptional = false;
4559
4560
  }
4560
4561
  /**
4561
4562
  * @return {?}
@@ -4575,20 +4576,45 @@
4575
4576
  * @private
4576
4577
  * @param {?} d
4577
4578
  * @param {?} month
4579
+ * @param {?} year
4578
4580
  * @return {?}
4579
4581
  */
4580
4582
  GovUkDateComponent.prototype.isValidDate = /**
4581
4583
  * @private
4582
4584
  * @param {?} d
4583
4585
  * @param {?} month
4586
+ * @param {?} year
4584
4587
  * @return {?}
4585
4588
  */
4586
- function (d, month) {
4589
+ function (d, month, year) {
4587
4590
  /** @type {?} */
4588
- var dateCheck = d instanceof Date && !isNaN(d.getTime());
4591
+ var dateCheck = !isNaN(d.getTime());
4592
+ // Month mismatch occurs if the provided day or month are invalid, or either is omitted. **Note:** This is insufficient for
4593
+ // checking date validity when the year is omitted because it defaults to 1900 - an extra check is required
4589
4594
  /** @type {?} */
4590
- var leapYearCheck = d.getMonth() === month;
4591
- return dateCheck && leapYearCheck;
4595
+ var monthMatch = d.getMonth() === month;
4596
+ /** @type {?} */
4597
+ var yearMatch = d.getFullYear() === year;
4598
+ return dateCheck && monthMatch && yearMatch;
4599
+ };
4600
+ /**
4601
+ * @private
4602
+ * @param {?} value
4603
+ * @return {?}
4604
+ */
4605
+ GovUkDateComponent.prototype.isEmpty = /**
4606
+ * @private
4607
+ * @param {?} value
4608
+ * @return {?}
4609
+ */
4610
+ function (value) {
4611
+ // Note: Intentional use of == to check for null or undefined
4612
+ /* eslint-disable eqeqeq */
4613
+ /* tslint:disable:triple-equals */
4614
+ // NaN and < 0 checks required for month field
4615
+ return value == null || value === '' || isNaN(value) || value < 0;
4616
+ /* eslint-enable eqeqeq */
4617
+ /* tslint:enable:triple-equals */
4592
4618
  };
4593
4619
  /**
4594
4620
  * @return {?}
@@ -4598,8 +4624,7 @@
4598
4624
  */
4599
4625
  function () {
4600
4626
  var _this = this;
4601
- /** @type {?} */
4602
- var res = ( /**
4627
+ return ( /**
4603
4628
  * @return {?}
4604
4629
  */function () {
4605
4630
  /** @type {?} */
@@ -4608,11 +4633,13 @@
4608
4633
  var month = _this.formGroup.get(_this.month).value - 1;
4609
4634
  /** @type {?} */
4610
4635
  var year = _this.formGroup.get(_this.year).value;
4611
- /** @type {?} */
4612
- var isValid = _this.isValidDate(new Date(year, month, day), month);
4613
- return !isValid ? { dateComponent: true } : null;
4636
+ // Validation should pass if the date field is optional and day, month, and year are all empty
4637
+ if (_this.isOptional && _this.isEmpty(day) && _this.isEmpty(month) && _this.isEmpty(year)) {
4638
+ return null;
4639
+ }
4640
+ // + to coerce year to a number
4641
+ return !_this.isValidDate(new Date(year, month, day), month, +year) ? { dateComponent: true } : null;
4614
4642
  });
4615
- return res;
4616
4643
  };
4617
4644
  GovUkDateComponent.decorators = [
4618
4645
  { type: i0.Component, args: [{
@@ -4626,7 +4653,8 @@
4626
4653
  GovUkDateComponent.propDecorators = {
4627
4654
  config: [{ type: i0.Input }],
4628
4655
  errorMessage: [{ type: i0.Input }],
4629
- formGroup: [{ type: i0.Input }]
4656
+ formGroup: [{ type: i0.Input }],
4657
+ isOptional: [{ type: i0.Input }]
4630
4658
  };
4631
4659
  return GovUkDateComponent;
4632
4660
  }());