Ejemplo n.º 1
0
        /// <inheritdoc />
        public void Process(SchemaProcessorContext context)
        {
            if (!context.Schema.IsObject || context.Schema.Properties.Count == 0)
            {
                // Ignore other
                // Ignore objects with no properties
                return;
            }

            IValidator validator = null;

            try
            {
                validator = _validatorFactory.GetValidator(context.Type);
            }
            catch (Exception e)
            {
                _logger.LogWarning(0, e, $"GetValidator for type '{context.Type}' fails.");
            }

            if (validator == null)
            {
                return;
            }

            _logger.LogDebug($"Applying FluentValidation rules to swagger schema for type '{context.Type}'.");

            ApplyRulesToSchema(context, validator);

            try
            {
                AddRulesFromIncludedValidators(context, validator);
            }
            catch (Exception e)
            {
                _logger.LogWarning(0, e, $"Applying IncludeRules for type '{context.Type}' fails.");
            }
        }
        private void AddRulesFromIncludedValidators(SchemaProcessorContext context, IValidator validator)
        {
            // Note: IValidatorDescriptor doesn't return IncludeRules so we need to get validators manually.
            var includeRules = (validator as IEnumerable <IValidationRule>)
                               .NotNull()
                               .Where(
                includeRule =>
                !includeRule.HasCondition && !includeRule.HasAsyncCondition &&
                includeRule is IIncludeRule
                )
                               .ToList();

            var childAdapters = includeRules
                                // 2nd filter
                                .SelectMany(
                includeRule => { return(includeRule.Components.Select(c => c.Validator)); }
                )
                                .Where(
                x => x.GetType().IsGenericType &&
                x.GetType().GetGenericTypeDefinition() == typeof(ChildValidatorAdaptor <,>)
                )
                                .ToList();

            foreach (var adapter in childAdapters)
            {
                if (adapter.GetType().GetGenericTypeDefinition() == typeof(ChildValidatorAdaptor <,>))
                {
                    var adapterType = adapter.GetType();

                    var adapterMethod = adapterType
                                        .GetMethod("GetValidator");

                    if (adapterMethod != null)
                    {
                        // Create validation context of generic type
                        var validationContext = Activator.CreateInstance(
                            adapterMethod.GetParameters().First().ParameterType, new object[] { null ! }
        private void ApplyRulesToSchema(SchemaProcessorContext context, IValidator validator)
        {
            _logger.LogDebug($"Applying FluentValidation rules to swagger schema for type '{context.Type}'");

            var schema = context.Schema;

            // Loop through properties
            foreach (var key in schema?.Properties?.Keys ?? Array.Empty <string>())
            {
                var validators = validator.GetValidatorsForMemberIgnoreCase(key);

                foreach (var propertyValidator in validators)
                {
                    foreach (var rule in _rules)
                    {
                        if (rule.Matches(propertyValidator))
                        {
                            try
                            {
                                rule.Apply(new RuleContext(context, key, propertyValidator));

                                _logger.LogDebug(
                                    $"Rule '{rule.Name}' applied for property '{context.Type.Name}.{key}'"
                                    );
                            }
                            catch (Exception e)
                            {
                                _logger.LogWarning(
                                    0, e, $"Error on apply rule '{rule.Name}' for property '{context.Type.Name}.{key}'"
                                    );
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public void Process(SchemaProcessorContext context)
 {
     context.Schema.Example = _example;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates new instance of <see cref="RuleContext"/>.
 /// </summary>
 /// <param name="schemaProcessorContext">SchemaProcessorContext.</param>
 /// <param name="propertyKey">Property name.</param>
 /// <param name="propertyValidator">Property validator.</param>
 public RuleContext(SchemaProcessorContext schemaProcessorContext, string propertyKey, IPropertyValidator propertyValidator)
 {
     SchemaProcessorContext = schemaProcessorContext;
     PropertyKey            = propertyKey;
     PropertyValidator      = propertyValidator;
 }
Ejemplo n.º 6
0
 public Task ProcessAsync(SchemaProcessorContext context)
 {
     context.Schema.Example = _example;
     return(Task.FromResult <object>(null));
 }
Ejemplo n.º 7
0
 public async Task ProcessAsync(SchemaProcessorContext context)
 {
 }