Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/17/20 17:03:24 (4 years ago)
Author:
dpiringe
Message:

#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:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs

    r17477 r17481  
    88
    99namespace 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
    1031  /// <summary>
    1132  /// Main data class for json interface.
     
    2041      }
    2142
    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);
    4051        }
    41         return res;
     52        return new ValidationResult(success, errors);
    4253      }
    4354    }
     
    96107    }
    97108
    98     public virtual IJsonItemValidator GetValidator() => new JsonItemValidator(this);
     109    public IJsonItemValidator GetValidator() => new JsonItemValidator(this);
    99110
    100111    public void FixatePath() => fixedPath = Path;
     
    112123
    113124    #region Abstract Methods
    114     protected abstract bool Validate();
     125    protected abstract ValidationResult Validate();
    115126    #endregion
    116127
Note: See TracChangeset for help on using the changeset viewer.