Skip to content

Commit

Permalink
feat(COOP-2237): add new validator to check for minDate
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarinkov committed Aug 9, 2024
1 parent 91255c4 commit ab26bdb
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/validators/src/date.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,27 @@ export function isDate(): BalValidatorFn {
return DateFns.isDate(value)
}
}

/**
* Returns `true` if the given data is at the same or after the value date
*
* ```typescript
* BalValidators.isMinDate('2000-01-01')('2000-01-01') // true
* BalValidators.isMinDate('2000-01-02')('2000-01-01') // true
* BalValidators.isMinDate(new Date(2020, 0, 1))(new Date(2020, 0, 1)) // true
* ```
*/
export function isMinDate(date: Date | string): BalValidatorFn {
return function (value: any) {
if (BalUtils.isEmpty(value)) {
return true
}
if (isString(value)) {
value = BalUtils.parse(value)
}
if (isString(date)) {
date = BalUtils.parse(date)
}
return DateFns.isSameDay(value, date) || DateFns.isAfter(value, date)
}
}

0 comments on commit ab26bdb

Please sign in to comment.