Example #1
0
    public ValidationResult Validate(IValidationContext context)
    {
        var validationResults = validatorProvider
                                .GetValidators <T>()
                                .Select(validator => validator.Validate(context));

        return(validationResults.Merge());
    }
Example #2
0
        protected virtual void AddInstanceInternal(object instanceToValidate)
        {
            if (_instanceToValidators.ContainsKey(instanceToValidate))
            {
                RemoveInstanceInternal(instanceToValidate);
            }
            IValidatorContext  context    = CreateContext(instanceToValidate);
            IList <IValidator> validators = ValidatorProvider.GetValidators(context);

            for (int index = 0; index < validators.Count; index++)
            {
                AddValidatorInternal(validators[index]);
            }
        }
Example #3
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var arguments = context.Field.Arguments;

            var validationResults = new List <ValidationResult>();

            foreach (var argument in arguments)
            {
                if (argument == null ||
                    !_options.ShouldValidate(context, argument))
                {
                    continue;
                }

                var resolvedValidators = _validatorProvider.GetValidators(context, argument);
                try
                {
                    var value = context.ArgumentValue <object>(argument.Name);
                    foreach (var resolvedValidator in resolvedValidators)
                    {
                        var validationContext = new ValidationContext <object>(value);
                        var validationResult  = await resolvedValidator.Validator.ValidateAsync(validationContext, context.RequestAborted);

                        if (validationResult != null)
                        {
                            validationResults.Add(validationResult);
                            _validationResultHandler.Handle(context, validationResult);
                        }
                    }
                }
                finally
                {
                    foreach (var resolvedValidator in resolvedValidators)
                    {
                        resolvedValidator.Scope?.Dispose();
                    }
                }
            }

            var invalidValidationResults = validationResults.Where(r => !r.IsValid);

            if (invalidValidationResults.Any())
            {
                OnInvalid(context, invalidValidationResults);
            }

            await _next(context);
        }
    public static async Task <ValidationResult> ValidateAsync(this IValidatable validatable, IValidatorProvider validatorFactory, bool throwOnNoValidatorFound = false, bool throwOnValidationError = false)
    {
        if (validatable is null)
        {
            throw new ArgumentNullException(nameof(validatable));
        }

        if (validatorFactory is null)
        {
            throw new ArgumentNullException(nameof(validatorFactory));
        }

        var validators = validatorFactory.GetValidators(validatable.GetType());

        if (validators.Any())
        {
            var context = new ValidationContext <IValidatable>(validatable);

            var validationTasks = validators
                                  .Select(validator => validator.ValidateAsync(context));

            var validationResults = await Task
                                    .WhenAll(validationTasks)
                                    .ConfigureAwait(false);

            var validationResult = validationResults.Merge();

            if (!validationResult.IsValid && throwOnValidationError)
            {
                throw validationResult.ToException();
            }

            return(validationResult);
        }

        if (throwOnNoValidatorFound)
        {
            throw new NotSupportedException($"Validation of type {validatable.GetType()} is not supported");
        }

        return(new ValidationResult());
    }
Example #5
0
 internal CompositeValidatorDescriptor(IValidatorProvider validatorProvider)
 {
     validatorDescriptors = validatorProvider
                            .GetValidators <T>()
                            .Select(validator => validator.CreateDescriptor());
 }
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            var arguments = context.Selection.Field.Arguments;

            var invalidResults = new List <ArgumentValidationResult>();

            foreach (var argument in arguments)
            {
                if (argument == null)
                {
                    continue;
                }

                var resolvedValidators = _validatorProvider
                                         .GetValidators(context, argument)
                                         .ToArray();
                if (resolvedValidators.Length > 0)
                {
                    try
                    {
                        var value = context.ArgumentValue <object?>(argument.Name);
                        if (value == null)
                        {
                            continue;
                        }

                        foreach (var resolvedValidator in resolvedValidators)
                        {
                            var validationContext = new ValidationContext <object?>(value);
                            var validationResult  = await resolvedValidator.Validator.ValidateAsync(
                                validationContext,
                                context.RequestAborted);

                            if (validationResult != null &&
                                !validationResult.IsValid)
                            {
                                invalidResults.Add(
                                    new ArgumentValidationResult(
                                        argument.Name,
                                        resolvedValidator.Validator,
                                        validationResult));
                            }
                        }
                    }
                    finally
                    {
                        foreach (var resolvedValidator in resolvedValidators)
                        {
                            resolvedValidator.Scope?.Dispose();
                        }
                    }
                }
            }

            if (invalidResults.Any())
            {
                _validationErrorsHandler.Handle(context, invalidResults);
                return;
            }

            await _next(context);
        }