Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItems/ConcreteRestrictedArrayJsonItem.cs @ 18183

Last change on this file since 18183 was 18077, checked in by dpiringe, 3 years ago

#3026

  • added the dockerhub readme file
  • fixed a bug which caused changed values (changed by events) to be overwritten with wrong values
File size: 1.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HEAL.Attic;
4using Newtonsoft.Json.Linq;
5
6namespace HeuristicLab.JsonInterface {
7  [StorableType("4333B6C0-26F7-41BA-A297-071AB50307F9")]
8  public abstract class ConcreteRestrictedArrayJsonItem<T> : ArrayJsonItem<T>, IConcreteRestrictedJsonItem<T> {
9    public IEnumerable<T> ConcreteRestrictedItems { get; set; }
10
11    protected override ValidationResult Validate() {
12      bool res = true;
13      IList<string> errors = new List<string>();
14      if (ConcreteRestrictedItems == null) return ValidationResult.Successful();
15      foreach (var x in Value) {
16        bool tmp = false;
17        foreach (var restrictedItem in ConcreteRestrictedItems) {
18          tmp = tmp || x.Equals(restrictedItem); //if one tmp is true, it stays true (match found)
19        }
20        if (!tmp)
21          errors.Add($"[{Path}]: Value '{x}' is not one of the allowed values: " +
22                     $"'{ string.Join(",", ConcreteRestrictedItems.Select(s => s.ToString()).ToArray()) }'.");
23        res = res && tmp; //if one tmp is false, res will set false
24      }
25      if (res)
26        return ValidationResult.Successful();
27      else
28        return ValidationResult.Faulty(errors);
29    }
30
31    public override void SetJObject(JObject jObject) {
32      base.SetJObject(jObject);
33      ConcreteRestrictedItems =
34        (jObject[nameof(IConcreteRestrictedJsonItem<T>.ConcreteRestrictedItems)]?
35        .ToObject<IEnumerable<T>>());
36    }
37
38    public ConcreteRestrictedArrayJsonItem() { }
39
40    [StorableConstructor]
41    protected ConcreteRestrictedArrayJsonItem(StorableConstructorFlag _) : base(_) { }
42  }
43}
Note: See TracBrowser for help on using the repository browser.