1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HEAL.Attic;
|
---|
4 | using Newtonsoft.Json.Linq;
|
---|
5 |
|
---|
6 | namespace 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 | }
|
---|