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.1 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using Newtonsoft.Json.Linq;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.JsonInterface {
|
---|
9 | public abstract class IntervalRestrictedValueJsonItem<T> : ValueJsonItem<T>, IIntervalRestrictedJsonItem<T>
|
---|
10 | where T : IComparable {
|
---|
11 | public T Minimum { get; set; }
|
---|
12 | public T Maximum { get; set; }
|
---|
13 |
|
---|
14 | protected override ValidationResult Validate() {
|
---|
15 | bool res = Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0;
|
---|
16 | if (res) return ValidationResult.Successful();
|
---|
17 | else return ValidationResult.Faulty($"[{Path}]: Value {Value} is not between {Minimum} and {Maximum}.");
|
---|
18 | }
|
---|
19 |
|
---|
20 | public override void SetJObject(JObject jObject) {
|
---|
21 | base.SetJObject(jObject);
|
---|
22 |
|
---|
23 | var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
|
---|
24 | if (minProp != null) Minimum = minProp.ToObject<T>();
|
---|
25 |
|
---|
26 |
|
---|
27 | var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
|
---|
28 | if (maxProp != null) Maximum = maxProp.ToObject<T>();
|
---|
29 | }
|
---|
30 | }
|
---|
31 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.