Changeset 17481 for branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs
- Timestamp:
- 03/17/20 17:03:24 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs
r17477 r17481 8 8 9 9 namespace HeuristicLab.JsonInterface { 10 11 public readonly struct ValidationResult { 12 13 public static ValidationResult Successful() => new ValidationResult(true, Enumerable.Empty<string>()); 14 public static ValidationResult Faulty(IEnumerable<string> errors) => new ValidationResult(false, errors); 15 public static ValidationResult Faulty(string error) => new ValidationResult(false, error); 16 17 public ValidationResult(bool success, IEnumerable<string> errors) { 18 Success = success; 19 Errors = errors; 20 } 21 22 public ValidationResult(bool success, string error) { 23 Success = success; 24 Errors = Enumerable.Repeat(error, 1); 25 } 26 27 public bool Success { get; } 28 public IEnumerable<string> Errors { get; } 29 } 30 10 31 /// <summary> 11 32 /// Main data class for json interface. … … 20 41 } 21 42 22 public bool Validate(ref IList<IJsonItem> faultyItems) { 23 faultyItems = new List<IJsonItem>(); 24 return ValidateHelper(Root, ref faultyItems); 25 } 26 27 // TODO: return ValidationResult ? 28 private bool ValidateHelper(JsonItem item, ref IList<IJsonItem> faultyItems) { 29 int hash = item.GetHashCode(); 30 if (Cache.TryGetValue(hash, out bool r)) 31 return r; 32 33 bool res = item.Validate(); 34 if (!res) faultyItems.Add(item); 35 Cache.Add(hash, res); 36 if(item.Children != null) { 37 foreach (var child in item.Children) 38 if (!ValidateHelper(child as JsonItem, ref faultyItems)) 39 res = false && res; 43 public ValidationResult Validate() { 44 IEnumerable<string> errors = Enumerable.Empty<string>(); 45 bool success = true; 46 foreach(var x in Root) { 47 var res = ((JsonItem)x).Validate(); 48 //if one success is false -> whole validation is false 49 success = success && res.Success; 50 errors.Concat(res.Errors); 40 51 } 41 return res;52 return new ValidationResult(success, errors); 42 53 } 43 54 } … … 96 107 } 97 108 98 public virtualIJsonItemValidator GetValidator() => new JsonItemValidator(this);109 public IJsonItemValidator GetValidator() => new JsonItemValidator(this); 99 110 100 111 public void FixatePath() => fixedPath = Path; … … 112 123 113 124 #region Abstract Methods 114 protected abstract boolValidate();125 protected abstract ValidationResult Validate(); 115 126 #endregion 116 127
Note: See TracChangeset
for help on using the changeset viewer.