public bool ValidateInstance(string jsonData, out string errorMsg) { errorMsg = ""; bool result = false; ValidationResults validateResult = new ValidationResults(); try { //反序列化 DEEntityInstance resultInstance = JSONSerializerExecute.Deserialize <DEEntityInstanceBase>(jsonData) as DEEntityInstance; validateResult = resultInstance.Validate(); if (validateResult.IsValid()) { result = true; } else { //错误信息格式 json格式 errorMsg = "["; foreach (var rst in validateResult) { errorMsg += string.Format("{{{0}}},", rst.Message.ToString()); } errorMsg = errorMsg.TrimEnd(',') + "]"; } } catch (Exception e) { errorMsg = string.Format("[{{'ErrorMsg':'{0}'}}]", e.Message); } return(result); }
public static void CheckValidationResults <T>(ValidationResults validationResults) where T : System.Exception { if (validationResults.IsValid() == false) { string errorMessage = BuildErrorMessage(validationResults); errorMessage.IsNotEmpty().TrueThrow <T>(errorMessage); } }
protected internal override void DoValidate(object objectToValidate, object currentObject, string key, ValidationResults validateResults) { List<ValidationResult> childrenValidationResults = new List<ValidationResult>(); foreach (Validator validator in this.validators) { ValidationResults childValidationResults = new ValidationResults(); validator.DoValidate(objectToValidate, currentObject, key, childValidationResults); if (childValidationResults.IsValid()) return; childrenValidationResults.AddRange(childValidationResults); } RecordValidationResult(validateResults, this.MessageTemplate, currentObject, key, childrenValidationResults); }