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.3 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 IntervalRestrictedMatrixJsonItem<T> : MatrixJsonItem<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 | IList<string> errors = new List<string>();
|
---|
16 | bool success = true;
|
---|
17 | foreach (var x in Value) {
|
---|
18 | foreach (var y in x) {
|
---|
19 | if (Minimum.CompareTo(y) > 0 || Maximum.CompareTo(y) < 0) {
|
---|
20 | success = false;
|
---|
21 | errors.Add($"[{Path}]: Value {y} is not between {Minimum} and {Maximum}.");
|
---|
22 | }
|
---|
23 | }
|
---|
24 | }
|
---|
25 | return new ValidationResult(success, errors);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override void SetJObject(JObject jObject) {
|
---|
29 | base.SetJObject(jObject);
|
---|
30 |
|
---|
31 | var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
|
---|
32 | if (minProp != null) Minimum = minProp.ToObject<T>();
|
---|
33 |
|
---|
34 |
|
---|
35 | var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
|
---|
36 | if (maxProp != null) Maximum = maxProp.ToObject<T>();
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.