Last change
on this file since 17481 was
17481,
checked in by dpiringe, 4 years ago
|
#3026:
- refactored json item validation process -> every method now returns a ValidationResult containing bool Success (true if validation is successful) and IEnumerable<string> Errors (error messages)
- this design allows to specify error messages directly in json items
|
File size:
1.0 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.JsonInterface {
|
---|
8 | public abstract class RangedJsonItem<T> : JsonItem, IRangedJsonItem<T>
|
---|
9 | where T : IComparable {
|
---|
10 | public T MinValue { get; set; }
|
---|
11 | public T MaxValue { get; set; }
|
---|
12 | public T Minimum { get; set; }
|
---|
13 | public T Maximum { get; set; }
|
---|
14 |
|
---|
15 | protected override ValidationResult Validate() {
|
---|
16 | IList<string> errors = new List<string>();
|
---|
17 | bool successMin = (Minimum.CompareTo(MinValue) <= 0 && Maximum.CompareTo(MinValue) >= 0);
|
---|
18 | bool successMax = (Minimum.CompareTo(MaxValue) <= 0 && Maximum.CompareTo(MaxValue) >= 0);
|
---|
19 | if (!successMin) errors.Add($"[{Path}]: Value {MinValue} is not between {Minimum} and {Maximum}.");
|
---|
20 | if (!successMax) errors.Add($"[{Path}]: Value {MaxValue} is not between {Minimum} and {Maximum}.");
|
---|
21 | return new ValidationResult((successMin && successMax), errors);
|
---|
22 |
|
---|
23 | }
|
---|
24 |
|
---|
25 | }
|
---|
26 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.