private void AddRulesFromIncludedValidators(SchemaFilterContext context, IValidator validator, SchemaGenerationContext schemaGenerationContext) { FluentValidationSchemaBuilder.AddRulesFromIncludedValidators( validator: validator, logger: _logger, schemaGenerationContext: schemaGenerationContext); }
private void AddRulesFromIncludedValidators(OpenApiSchema schema, SchemaFilterContext context, IValidator validator) { FluentValidationSchemaBuilder.AddRulesFromIncludedValidators( schema: schema, schemaFilterContext: context, validator: validator, rules: _rules, logger: _logger); }
private void ApplyRulesToSchema(SchemaFilterContext context, IValidator validator, SchemaGenerationContext schemaGenerationContext) { FluentValidationSchemaBuilder.ApplyRulesToSchema( schemaType: context.Type, schemaPropertyNames: null, validator: validator, logger: _logger, schemaGenerationContext: schemaGenerationContext); }
private static OpenApiSchema GetSchemaForType( OperationFilterContext context, Func <Type, string> schemaIdSelector, Type parameterType) { SchemaRepository schemaRepository = context.SchemaRepository; ISchemaGenerator schemaGenerator = context.SchemaGenerator; return(FluentValidationSchemaBuilder.GetSchemaForType(schemaRepository, schemaGenerator, schemaIdSelector, parameterType)); }
private void ApplyRulesToSchema(OpenApiSchema schema, SchemaFilterContext context, IValidator validator) { FluentValidationSchemaBuilder.ApplyRulesToSchema( schema: schema, schemaType: context.Type, schemaPropertyNames: null, schemaFilterContext: context, validator: validator, rules: _rules, logger: _logger); }
private void ApplyInternal(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters == null) { return; } if (_validatorFactory == null) { _logger.LogWarning(0, "ValidatorFactory is not provided. Please register FluentValidation."); return; } var schemaIdSelector = _swaggerGenOptions.SchemaGeneratorOptions.SchemaIdSelector ?? new SchemaGeneratorOptions().SchemaIdSelector; foreach (var operationParameter in operation.Parameters) { var apiParameterDescription = context.ApiDescription.ParameterDescriptions.FirstOrDefault(description => description.Name.Equals(operationParameter.Name, StringComparison.InvariantCultureIgnoreCase)); var modelMetadata = apiParameterDescription?.ModelMetadata; if (modelMetadata != null) { var parameterType = modelMetadata.ContainerType; if (parameterType == null) { continue; } var validator = _validatorFactory.GetValidator(parameterType); if (validator == null) { continue; } OpenApiSchema schema = GetSchemaForType(context, schemaIdSelector, parameterType); if (schema.Properties != null && schema.Properties.Count > 0) { var schemaPropertyName = operationParameter.Name; var apiProperty = schema.Properties.FirstOrDefault(property => property.Key.EqualsIgnoreAll(schemaPropertyName)); if (apiProperty.Key != null) { schemaPropertyName = apiProperty.Key; } FluentValidationSchemaBuilder.ApplyRulesToSchema( schema: schema, schemaType: parameterType, schemaPropertyNames: new[] { schemaPropertyName }, schemaFilterContext: null, validator: validator, rules: _rules, logger: _logger); if (schema.Required != null) { operationParameter.Required = schema.Required.Contains(schemaPropertyName, IgnoreAllStringComparer.Instance); } var parameterSchema = operationParameter.Schema; if (parameterSchema != null) { if (schema.Properties.TryGetValue(schemaPropertyName.ToLowerCamelCase(), out var property) || schema.Properties.TryGetValue(schemaPropertyName, out property)) { // Copy from property schema to parameter schema. parameterSchema.MinLength = property.MinLength; parameterSchema.Nullable = property.Nullable; parameterSchema.MaxLength = property.MaxLength; parameterSchema.Pattern = property.Pattern; parameterSchema.Minimum = property.Minimum; parameterSchema.Maximum = property.Maximum; parameterSchema.ExclusiveMaximum = property.ExclusiveMaximum; parameterSchema.ExclusiveMinimum = property.ExclusiveMinimum; parameterSchema.AllOf = property.AllOf; } } } } } }