private void Validate(ValidatorResult result, T entity, ValidationTypes type) { switch (type) { case ValidationTypes.Default: DefaultValidations(result, entity); break; case ValidationTypes.Insert: InsertValidations(result, entity); break; case ValidationTypes.Update: UpdateValidations(result, entity); break; case ValidationTypes.Delete: DeleteValidations(result, entity); break; default: DefaultValidations(result, entity); break; } }
public static void ValidateAnnotations(this ValidatorResult result, object entity) { if (entity == null) { return; } var ctx = new ValidationContext(entity); var type = entity.GetType(); var props = type.GetProperties(); foreach (var prop in props) { var value = prop.GetValue(entity, null); ctx.MemberName = prop.Name; var validationList = new List <ValidationResult>(); Validator.TryValidateProperty(value, ctx, validationList); foreach (var validation in validationList) { result.AddError(validation.ErrorMessage); } } }
public ValidatorResult Validate(T entity, ValidationTypes type = ValidationTypes.Default) { var result = new ValidatorResult(); Validate(result, entity, type); return(result); }
protected override void DefaultValidations(ValidatorResult result, User entity) { base.DefaultValidations(result, entity); if (Service.GetAll(x => x.Login.ToLower() == entity.Login.ToLower() && x.Id != entity.Id).Any()) { result.AddError($"There is already a user with this Login: {entity.Login}"); } }
protected virtual void DefaultValidations(ValidatorResult result, T entity) { AnnotationsValidations(result, entity); if (entity == null) { result.AddError("Entity can not be null!"); } }
public ValidatorResult Validate(T[] entities, ValidationTypes type = ValidationTypes.Default) { var result = new ValidatorResult(); foreach (var entity in entities) { Validate(result, entity, type); } return(result); }
protected virtual void DeleteValidations(ValidatorResult result, T entity) { }
protected virtual void UpdateValidations(ValidatorResult result, T entity) { DefaultValidations(result, entity); }
protected virtual void AnnotationsValidations(ValidatorResult result, T entity) { result.ValidateAnnotations(entity); }