private ValidationResult Validate(T value) { try { return(_validate(value)); } catch (Exception e) { return(ValidationResult.Error(String.Format("Exception during validation of '{0}' for '{1}': {2}", value, Name, e.Message))); } }
public ValidationResult LoadFromDictionary(Dictionary <string, Object> dict) { object value; if (!dict.TryGetValue(Name, out value)) { return(ValidationResult.Error(String.Format("There's no data for '{0}'.", Name))); } // MiniJson does support only a couple of types // and there are types not mapped directly. // For example: int gets deserialized as long // https://gist.github.com/darktable/1411710 // null is always wrong if (value == null) { return(ValidationResult.Error(String.Format("Data for '{0}' is null.", Name))); } try { T v = (T)Convert.ChangeType(value, typeof(T)); var validation = Validate(v); if (validation.IsOk()) { Value = v; } return(validation); } catch (Exception e) { return(ValidationResult.Error(String.Format("Exception during loading of '{0}' with value '{1}': {2}", Name, value, e.Message))); } }
private static Func <T, ValidationResult> Validate <T>(Func <T, bool> validator, string errorMessage) => (value) => validator(value) ? ValidationResult.OK() : ValidationResult.Error(errorMessage);