How to create validation rule using multiple properties

I’m trying to validate an object that has beginDate and endDate properties. As part of the validation, I need to ensure that beginDate is “before” endDate. I tried doing the following:

ValidationRules
 .ensure((s:Session) => s.beginDate)
   .required()
   .satisfies((s:Session) => moment(s.beginDate).isBefore(s.endDate))
.on(Session);

Of course, this doesn’t work because the beginDate – not the Session object – is what is actually being passed to the satisfies() function.

How can I get access to both beginDate and endDate in the satisfies() validation rule?

1 Like

I was finally able to resolve this with the following:

ValidationRules
  .customRule('datecompare',(value,obj : Session,endRangeProperty : string) => {
     return value === null
        || value === undefined
        || obj[endRangeProperty] === null
        || obj[endRangeProperty] === undefined
        || moment(value).isBefore(obj[endRangeProperty]);
   }, "${$displayName} must come before ${$getDisplayName($config.endRangeProperty)}."
    , (endRangeProperty) => ({endRangeProperty}));


ValidationRules
   .ensure((s:Session) => s.StartDate)
     .satisifesRule('datecompare','EndDate')
    ...
    .on(Session);
2 Likes