private static IMemberValidator ConvertCommands(IReadOnlyCollection <ICommand> commands) { var memberValidator = new MemberValidator(); for (var i = 0; i < commands.Count; ++i) { var command = commands.ElementAt(i); if (command is IRule rule) { memberValidator.AddRule(rule); } else if (command is SetOptionalCommand) { if (memberValidator.IsOptional) { throw new InvalidCommandDuplicationException(i, command.Name); } memberValidator.IsOptional = true; } else if (command is SetRequiredCommand setRequiredErrorCommand) { if (memberValidator.RequiredError != null) { throw new InvalidCommandDuplicationException(i, command.Name); } memberValidator.RequiredError = new Error(setRequiredErrorCommand.Message); } else if (command is SetSingleErrorCommand setSingleErrorCommand) { if (memberValidator.SingleError != null) { throw new InvalidCommandDuplicationException(i, command.Name); } memberValidator.SingleError = new Error(setSingleErrorCommand.Message); } else if (command is WithMessageCommand withMessageCommand) { if (i == 0) { throw new InvalidCommandOrderException($"Command {withMessageCommand.Name} is invalid at the beginning", i, withMessageCommand.Name); } var singleErrorHolder = CommandsHelper.GetClosestCommand <IRule>(commands, i, _withMessageCommandCompatible); singleErrorHolder.RuleSingleError = new Error(withMessageCommand.Message); } } return(memberValidator); }
private static IValidator <TModel> ConvertCommands <TModel>(IReadOnlyCollection <ICommand> commands) where TModel : class { var validator = new Validator <TModel>(); for (var i = 0; i < commands.Count; ++i) { var command = commands.ElementAt(i); if (command is IScope <TModel> scope) { validator.AddScope(scope); } else if (command is SetSingleErrorCommand setSingleErrorCommand) { if (validator.SingleError != null) { throw new InvalidCommandDuplicationException(i, command.Name); } validator.SingleError = new Error(setSingleErrorCommand.Message); } else if (command is WithMessageCommand withMessageCommand) { if (i == 0) { throw new InvalidCommandOrderException($"Command {withMessageCommand.Name} is invalid at the beginning", i, withMessageCommand.Name); } var singleErrorHolder = CommandsHelper.GetClosestCommand <IRule>(commands, i, _withMessageCommandCompatible); singleErrorHolder.RuleSingleError = new Error(withMessageCommand.Message); } } return(validator); }