public bool Validate <T>(HttpRequest httpRequest, T request, FluentValidation.IValidator <T> validator, ref IActionResult invalidResult) where T : class { bool result = false; if (request == null) { _logger.Warn(typeof(T).FullName + " was passed null, request must contain data. Throwing ValidationException"); invalidResult = new BadRequest(new List <string>() { "Request must contain data" }, httpRequest); } else { var validationResult = validator.Validate(request); if (!validationResult.IsValid) { invalidResult = new BadRequest(validationResult.Errors.Select(x => x.ErrorMessage).ToList(), httpRequest); string errors = string.Join(";", validationResult.Errors); _logger.Warn(typeof(T).FullName + " failed validation, request invalid."); } else { result = true; } } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="FluentValidatorToCatelValidatorAdapter"/> class. /// </summary> /// <param name="validatorType"> /// The validator type. /// </param> private FluentValidatorToCatelValidatorAdapter(Type validatorType) { _validator = (IValidator)Activator.CreateInstance(validatorType); if (!validatorType.TryGetAttribute(out _validatorDescriptionAttribute)) { _validatorDescriptionAttribute = new ValidatorDescriptionAttribute(validatorType.Name); } }
/// <summary> /// 初始化构造 /// 使用注入的同一个上下文 /// </summary> /// <param name="service">服务集合</param> /// <param name="logger">日志</param> protected CrudControllerBase(IServiceCollection service, ILogger <Controller> logger) : base(service, logger) { _validator = service.BuildServiceProvider().GetService <FluentValidation.IValidator <TCodeTabelModel> >(); _readerService = service.BuildServiceProvider().GetService <IGenericReaderService <TCodeTabelEntity, long> >(); _writerService = service.BuildServiceProvider().GetService <IGenericWriterService <TCodeTabelEntity, long> >(); _repository = service.BuildServiceProvider().GetService <IEFCoreQueryableRepository <TCodeTabelEntity, long> >(); CrudDtoMapper = service.BuildServiceProvider().GetService <ICrudDtoMapper <TCodeTabelEntity, TCodeTabelModel> >(); }
/// <summary> /// 初始化构造 /// 用于不同的个上下文,使用注入的工作单元 /// </summary> /// <param name="dbContext">上下文实例</param> /// <param name="service">服务集合</param> /// <param name="logger">日志</param> protected CrudControllerBase(IEFCoreUnitOfWork unitOfWork, IServiceCollection service, ILogger <Controller> logger) : base(service, logger) { _validator = service.BuildServiceProvider().GetService <FluentValidation.IValidator <TCodeTabelModel> >(); _repository = new EFCoreBaseRepository <TCodeTabelEntity>(unitOfWork.Context); _readerService = new GenericReaderService <TCodeTabelEntity, long>(logger, _repository); _writerService = new GenericWriterService <TCodeTabelEntity, long>(logger, _repository, unitOfWork); CrudDtoMapper = service.BuildServiceProvider().GetService <ICrudDtoMapper <TCodeTabelEntity, TCodeTabelModel> >(); }
public FluentValidator(Type typeOfValidator) : base("FluentValidationWrappedValidator") { if (typeOfValidator is null) { throw new ArgumentNullException(nameof(typeOfValidator)); } if (!typeOfValidator.IsDerivedFrom <FluentValidation.IValidator>()) { throw new ArgumentException("This type must derived from 'FluentValidation.IValidator'.", nameof(typeOfValidator)); } _validatorImpl = TypeVisit.CreateInstance <FluentValidation.IValidator>(typeOfValidator); _typeOfValidator = typeOfValidator; }
private Result <Nothing, Error> Validate <T>(FluentValidation.IValidator <T> validator, T instance) { Guard.Against.Null(instance, nameof(instance)); var result = validator.Validate(instance); if (result.IsValid) { return(Result.Success <Nothing, Error>(Nothing.Value)); } else { return(Result.Failure <Nothing, Error>(new Error.ValidationFailed(result))); } }
/// <summary> /// Initializes a new instance of the <see cref="FluentValidatorToCatelValidatorAdapter"/> class. /// </summary> /// <param name="validatorType"> /// The validator type. /// </param> private FluentValidatorToCatelValidatorAdapter(Type validatorType) { ConstructorInfo constructorInfo = validatorType.GetConstructor(new Type[] { }); if (constructorInfo != null) { _validator = (IValidator)constructorInfo.Invoke(new object[] { }); } if (!AttributeHelper.TryGetAttribute(validatorType, out _validatorDescriptionAttribute)) { _validatorDescriptionAttribute = new ValidatorDescriptionAttribute(validatorType.Name); } }
public IActionResult AddEvent([FromServices] FluentValidation.IValidator <EventViewModel> validator, [FromBody] EventViewModel model) { var userName = HttpContext.User.Identity.Name; var currentAttendeeId = userRepository.GetUserByUsername(userName).Id; if (!ModelState.IsValid) { return(ValidationError(GeneralMessages.Event)); } var newEvent = Mapper.Map <Event>(model); newEvent.DateCreated = DateTime.UtcNow; newEvent.AttendeeId = currentAttendeeId; var startDateText = model.StartDate.ToString(); DateTime startDate = new DateTime(); DateTime.TryParse(startDateText, out startDate); var avList = availabilityRepository.GetAvailabilitiesByHour(startDate, model.RoomId); var first = avList.FirstOrDefault(); if (first != null) { newEvent.HostId = first.HostId; } else { newEvent.HostId = 3; //TODO: get first host from db } eventRepository.AddEvent(newEvent); Context.SaveChanges(); // TODO: return DTO object return(Ok(new { Id = newEvent.Id, StartDate = newEvent.StartDate, EndDate = newEvent.EndDate })); }
public IPipeline <TContext> AddValidator <TRequest>(FluentValidation.IValidator <TRequest> validator) where TRequest : class { throw new NotImplementedException(); }
public FluentValidator(FluentValidation.IValidator <TObject> validator) { Validator = validator; }
public Validator() { validator = new V(); }
public FluentValidationValidator(FluentValidation.IValidator validator) { _validator = validator; }
public FluentValidatorAdapter(FluentValidation.IValidator <T> fluentValidator) { _fluentValidator = fluentValidator; }
public static FluentValidator By(FluentValidation.IValidator validator) { return(new FluentValidator(validator)); }
public FluentValidator(FluentValidation.IValidator validator) : base("FluentValidationWrappedValidator") { _validatorImpl = validator ?? throw new ArgumentNullException(nameof(validator)); _typeOfValidator = validator.GetType(); }
public Validator(FluentValidation.IValidator <T> validator) { this.validator = validator; }
/// <summary> /// Register for Sink Validator for FluentValidation /// </summary> /// <param name="registrar"></param> /// <param name="validator"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static IFluentValidationRegistrar AndForFluentValidator(this IWaitForMessageValidationRegistrar registrar, FluentValidation.IValidator validator) { if (registrar is null) { throw new ArgumentNullException(nameof(registrar)); } return(registrar.AndForCustomValidator(FluentValidator.By(validator))); }