Example #1
0
        /// <summary>
        /// Validate an instance
        /// </summary>
        public static async Task ValidateAsync <TArgument>(ValidatorTypeCache cache, Type type, TArgument instance, IDictionary <string, object> userContext, IServiceProvider?provider, CancellationToken cancellation = default)
        {
            Guard.AgainstNull(cache, nameof(cache));
            Guard.AgainstNull(userContext, nameof(userContext));
            Guard.AgainstNull(type, nameof(type));
            Guard.AgainstNull(userContext, nameof(userContext));

            var currentType       = (Type?)type;
            var validationContext = default(ValidationContext <TArgument>);

            while (currentType != null)
            {
                if (cache.TryGetValidators(currentType, provider, out var buildAll))
                {
                    validationContext ??= BuildValidationContext(instance, userContext);

                    var tasks             = buildAll.Select(x => x.ValidateAsync(validationContext, cancellation));
                    var validationResults = await Task.WhenAll(tasks);

                    var results = validationResults
                                  .SelectMany(result => result.Errors);

                    ThrowIfResults(results);
                }

                currentType = currentType.BaseType;
            }
        }
Example #2
0
        /// <summary>
        /// Validate an instance
        /// </summary>
        public static void Validate <TArgument>(ValidatorTypeCache cache, Type type, TArgument instance, IDictionary <string, object> userContext, IServiceProvider?provider)
        {
            Guard.AgainstNull(cache, nameof(cache));
            Guard.AgainstNull(userContext, nameof(userContext));
            Guard.AgainstNull(type, nameof(type));
            Guard.AgainstNull(userContext, nameof(userContext));
            if (instance == null)
            {
                return;
            }

            var currentType       = (Type?)type;
            var validationContext = default(ValidationContext <TArgument>);

            while (currentType != null)
            {
                if (cache.TryGetValidators(currentType, provider, out var buildAll))
                {
                    validationContext ??= BuildValidationContext(instance, userContext);
                    var results = buildAll
                                  .SelectMany(validator => validator.Validate(validationContext).Errors);

                    ThrowIfResults(results);
                }

                currentType = currentType.BaseType;
            }
        }
        /// <summary>
        /// Validate an instance
        /// </summary>
        public static void Validate(ValidatorTypeCache cache, Type type, object?instance, object userContext)
        {
            Guard.AgainstNull(cache, nameof(cache));
            Guard.AgainstNull(userContext, nameof(userContext));
            Guard.AgainstNull(type, nameof(type));
            Guard.AgainstNull(userContext, nameof(userContext));
            if (!cache.TryGetValidators(type, out var buildAll))
            {
                return;
            }

            var validationContext = BuildValidationContext(instance, userContext);
            var results           = buildAll
                                    .SelectMany(validator => validator.Validate(validationContext).Errors);

            ThrowIfResults(results);
        }
        /// <summary>
        /// Validate an instance
        /// </summary>
        public static async Task ValidateAsync(ValidatorTypeCache cache, Type type, object?instance, object userContext)
        {
            Guard.AgainstNull(cache, nameof(cache));
            Guard.AgainstNull(userContext, nameof(userContext));
            Guard.AgainstNull(type, nameof(type));
            Guard.AgainstNull(userContext, nameof(userContext));
            if (!cache.TryGetValidators(type, out var buildAll))
            {
                return;
            }

            var validationContext = BuildValidationContext(instance, userContext);

            var tasks             = buildAll.Select(x => x.ValidateAsync(validationContext));
            var validationResults = await Task.WhenAll(tasks);

            var results = validationResults
                          .SelectMany(result => result.Errors);

            ThrowIfResults(results);
        }
        /// <summary>
        /// Validate an instance
        /// </summary>
        public static async Task ValidateAsync(ValidatorTypeCache cache, Type type, object?instance, IDictionary <string, object> userContext, IServiceProvider?provider, CancellationToken cancellation = default)
        {
            Guard.AgainstNull(cache, nameof(cache));
            Guard.AgainstNull(userContext, nameof(userContext));
            Guard.AgainstNull(type, nameof(type));
            Guard.AgainstNull(userContext, nameof(userContext));
            if (!cache.TryGetValidators(type, provider, out var buildAll))
            {
                return;
            }

            var validationContext = BuildValidationContext(instance, userContext);

            var tasks             = buildAll.Select(x => x.ValidateAsync(validationContext, cancellation));
            var validationResults = await Task.WhenAll(tasks);

            var results = validationResults
                          .SelectMany(result => result.Errors);

            ThrowIfResults(results);
        }
Example #6
0
 /// <summary>
 /// Validate an instance
 /// </summary>
 public static void Validate <TArgument>(ValidatorTypeCache cache, Type type, TArgument instance, IDictionary <string, object> userContext)
 => Validate(cache, type, instance, userContext, null);
 /// <summary>
 /// Validate an instance
 /// </summary>
 public static Task ValidateAsync(ValidatorTypeCache cache, Type type, object?instance, IDictionary <string, object> userContext)
 => ValidateAsync(cache, type, instance, userContext, null);