Multiple validation rules in Aurelia for interdependent values and ranges

Hi there,

I’m trying to validate four interdependent values using Aurelia Validation plugin. I have an object called device settings with the following properties.

 public MinBrandPromise: number;
 public MaxBrandPromise: number;
 public MinProductSpoilt: number;
 public MaxProductSpoilt: number;

I’m trying to find a way to validate those properties against the following rules;

  1. MinBrandPromise should be greater or equal to -15.0°C and less MaxBrandPromise.
  2. If MaxBrandPromise is not null MinBrandPromise value is required.
  3. MaxBrandPromise should be less or equal to 50.0°C and greater MinBrandPromise.
  4. If MinBrandPromise is not null MaxBrandPromise is required.
    The same set of rules with different ranges applies to Min and Max Product Spoilt values.
    An additional rule is that MinBrandPromise and MaxBrandPromise range cannot overlap with MinProductSpoilt and MaxProductSpoil range.

Is there a way to check for overlapping ranges in Aurelia?

The code so far:

ValidationRules.customRule(
'minBrand',
(value, obj: Device, min) => {
return (parseFloat(value) >= min && (parseFloat(value) < obj.MaxBrandPromise))
},
''
)

ValidationRules.customRule(
'maxBrand',
(value, obj: Device, max) => {
  return (parseFloat(value) <= max && (parseFloat(value) > obj.MinBrandPromise))
},
''
)

ValidationRules.customRule(
'minProductSpoilt',
(value, obj: Device, min ) => {
  return (parseFloat(value) >= min && (parseFloat(value) < obj.MaxProductSpoilt))
},
 ''
);

ValidationRules.customRule(
'maxProductSpoilt',
(value, obj: Device, max) => {
  return (value == null || value == "") || (parseFloat(value) <= max && (parseFloat(value) > obj.MinProductSpoilt))
},
''
);

ValidationRules
.ensure((p: Device) => p.MinBrandPromise)
.required()
.when(t => ((t.MaxBrandPromise != null || t.MaxBrandPromise.toString() != "")))
.withMessageKey("this_field_is_required")
.then()
.satisfiesRule("minBrand", -15.0)
.withMessageKey('min_brand_promise_rule')

.ensure((p: Device) => p.MaxBrandPromise)
.required()
.when(t => ((t.MinBrandPromise != null || t.MinBrandPromise.toString() != "")))
.withMessageKey("this_field_is_required")
.then() 
.satisfiesRule("maxBrand", 50.0)
.withMessageKey("max_brand_promise_rule")
 
.ensure((p: Device) => p.MinProductSpoilt)
.required()
.when(t => (t.MaxProductSpoilt != null || t.MaxProductSpoilt.toString() != ""))
.withMessageKey("this_field_is_required")
.then()
.satisfiesRule('minProductSpoilt', -50.0)
.withMessageKey('min_product_spoilt_rule')

.ensure((p: Device) => p.MaxProductSpoilt)
.required()
.when(t => (t.MinProductSpoilt != null || t.MinProductSpoilt.toString() != ""))
.withMessageKey("this_field_is_required")
.then()
.satisfiesRule('maxProductSpoilt', 50.0)
.withMessageKey('max_product_spoilt_rule')

Essentially would you be checking:

  1. if minBrandPromise has a value, it cannot be between minProductSpoilt and maxProductSpoilt
  2. if maxBrandPromise has a value, it cannot be between minProductSpoilt and maxProductSpoilt
1 Like

Thank you very much @Stuart. As always the solution is simple but I never thought about that. You saved me a lot of time!