public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            if (schema.Required == null)
            {
                schema.Required = new SortedSet <string>();
            }
            if (!schema.Required.Contains(validationRule.GetPropertyKey()))
            {
                schema.Required.Add(validationRule.GetPropertyKey());
                schema.Properties[validationRule.GetPropertyKey()].Nullable = false;
            }

            return(Task.CompletedTask);
        }
Beispiel #2
0
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            var schemaProperty = schema.Properties[validationRule.GetPropertyKey()];

            var lengthValidator = (ILengthValidator)ruleComponent.Validator;

            if (lengthValidator.Max > 0)
            {
                schemaProperty.Maximum = lengthValidator.Max;
            }
            if (lengthValidator.Min > 0)
            {
                schemaProperty.Minimum  = lengthValidator.Min;
                schemaProperty.Nullable = false;
            }

            return(Task.CompletedTask);
        }
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            var schemaProperty = schema.Properties[validationRule.GetPropertyKey()];

            var betweenValidator = (IBetweenValidator)ruleComponent.Validator;

            if (betweenValidator.From.GetType().IsValueType&& betweenValidator.To.GetType().IsValueType)
            {
                schemaProperty.Maximum = betweenValidator.To.ToDecimal();
                schemaProperty.Minimum = betweenValidator.From.ToDecimal();
                if (betweenValidator.Name == "ExclusiveBetweenValidator")
                {
                    schemaProperty.ExclusiveMaximum = true;
                    schemaProperty.ExclusiveMinimum = true;
                }
            }

            return(Task.CompletedTask);
        }
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            var schemaProperty = schema.Properties[validationRule.GetPropertyKey()];

            schemaProperty.Format = "email";

            return(Task.CompletedTask);
        }
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            var schemaProperty = schema.Properties[validationRule.GetPropertyKey()];

            var comparisonValidator = (IComparisonValidator)ruleComponent.Validator;

            switch (comparisonValidator.Comparison)
            {
            case Comparison.Equal:
                schemaProperty.Maximum = comparisonValidator.ValueToCompare.ToDecimal();
                schemaProperty.Minimum = comparisonValidator.ValueToCompare.ToDecimal();
                break;

            case Comparison.GreaterThan:
                schemaProperty.Minimum          = comparisonValidator.ValueToCompare.ToDecimal();
                schemaProperty.ExclusiveMaximum = true;
                break;

            case Comparison.GreaterThanOrEqual:
                schemaProperty.Minimum = comparisonValidator.ValueToCompare.ToDecimal();
                break;

            case Comparison.LessThan:
                schemaProperty.Minimum          = comparisonValidator.ValueToCompare.ToDecimal();
                schemaProperty.ExclusiveMinimum = true;
                break;

            case Comparison.LessThanOrEqual:
                schemaProperty.Minimum = comparisonValidator.ValueToCompare.ToDecimal();
                break;
            }

            return(Task.CompletedTask);
        }
        public Task Resolve(OpenApiSchema schema,
                            SchemaFilterContext context,
                            IValidationRule validationRule,
                            IRuleComponent ruleComponent,
                            IValidatorFactory validatorFactory,
                            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            var schemaProperty = schema.Properties[validationRule.GetPropertyKey()];

            schemaProperty.MinLength = 1;
            schemaProperty.Nullable  = false;
            return(Task.CompletedTask);
        }
Beispiel #7
0
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            var childValidator = ruleComponent.Validator
                                 .GetType()
                                 .GetField("_validator", BindingFlags.Instance | BindingFlags.NonPublic)
                                 .GetValue(ruleComponent.Validator);

            if (childValidator is not IEnumerable <IValidationRule> validators)
            {
                logger.LogDebug($"Skipped '{childValidator}'");
                return(Task.CompletedTask);
            }

            var innerSchema = validationRule is IIncludeRule ? schema : GetApiSchemeForProperty(context, validationRule);

            foreach (var rule in validators)
            {
                if (rule is not IValidationRule innerPropertyRule)
                {
                    logger.LogDebug($"Skipped '{rule}'");
                    continue;
                }
                foreach (var innerRuleComponent in rule.Components)
                {
                    foreach (var resolver in resolvers.Where(r => r.MatchFunc(innerRuleComponent)))
                    {
                        resolver.Resolve(innerSchema, context, innerPropertyRule, innerRuleComponent, validatorFactory, resolvers);
                    }
                }
            }

            if (validationRule is not IIncludeRule)
            {
                schema.Properties[validationRule.GetPropertyKey()] = innerSchema;
            }

            return(Task.CompletedTask);
        }
        public Task Resolve(
            OpenApiSchema schema,
            SchemaFilterContext context,
            IValidationRule validationRule,
            IRuleComponent ruleComponent,
            IValidatorFactory validatorFactory,
            IEnumerable <IResolver> resolvers)
        {
            if (validationRule.HasConditions() || ruleComponent.HasConditions())
            {
                return(Task.CompletedTask);
            }

            var schemaProperty             = schema.Properties[validationRule.GetPropertyKey()];
            var regularExpressionValidator = (IRegularExpressionValidator)ruleComponent.Validator;

            schemaProperty.Pattern = regularExpressionValidator.Expression;

            return(Task.CompletedTask);
        }