public async Task Validate(DocumentIOResolveFieldContext <object> context, IValidationContext validationContext) { var model = context.GetArgument <Organization>(); validationContext.When(model, m => m.Name) .IsNullOrWhitespace() .AddValidationDetail("Задайте имя организации"); if (validationContext.IsValid(model, m => m.Name)) { var organizationNameExists = await databaseContext .Organizations .AnyAsync(organization => organization.Name == model.Name); validationContext.When(model, m => m.Name) .Is(() => organizationNameExists) .AddValidationDetail("Имя организации уже используется"); } validationContext.When(model, m => m.Accounts) .IsEmpty() .AddValidationDetail("Создайте хотя-бы один аккаунт"); for (var index = 0; index < model.Accounts.Count; index++) { var account = model.Accounts[index]; using (var scope = validationContext.CreateScope(model, m => m.Accounts[index])) { await accountValidation.ValidateAccount(scope, account); } } }
/// <summary> /// Creates new validation scope with specified validation path from expression /// </summary> public static IValidationScope CreateScope <TModel, TValue>( this IValidationContext validationContext, TModel model, Expression <Func <TModel, TValue> > expression, ValidationSeverity?validationSeverity = null) { var serviceProvider = (IServiceProvider)validationContext; var validationResolver = serviceProvider.GetRequiredService <IValidationPathResolver>(); var validationPart = validationResolver.FromExpression(expression.Body); return(validationContext.CreateScope(validationPart, validationSeverity)); }
public void InnerPath() { var withPrefix = validationContext.CreateScope("path"); var(key, _) = withPrefix.When("key", "value").AddValidationDetail("Error"); var validationDetail = Assert.Single(validationContext.ValidationDetails); Assert.Equal("path.key", key); Assert.Equal("path.key", validationDetail.ValidationKey); }
public void Save(IValidationContext validationContext) { validationContext.When(this, m => m.Name) .IsNullOrWhitespace() .AddValidationDetail("Order name must be set"); validationContext.When(this, m => m.Cost) .IsNotInRange(0, 10) // .IsLessOrEqual(0).IsGreaterOrEqual(10) .AddValidationDetail("Cost must be in [1, 9] range"); validationContext.When(this, m => m.Address) .IsNull() .AddValidationDetail("You should add your address"); Address?.Save(/*databaseContext, */ validationContext.CreateScope(this, m => m.Address)); }
public void CreateScope() { validationContext.CreateScope("Model"); }