private ActionJoinBuilder <T, TProperty> AddRuleAndReturnActionJoin(RuleValidator specRule) { if (OrNextRule) { if (NextRuleIsConditional) { _propertyValidator.ConditionalOrRule(specRule); } else { _propertyValidator.OrRule(specRule); } } else { if (NextRuleIsConditional) { _propertyValidator.ConditionalAndRule(specRule); } else { _propertyValidator.AndRule(specRule); } } return(new ActionJoinBuilder <T, TProperty>(_propertyValidator)); }
public void ChangeNomeApresentacao(string nomeApresentacao) { RuleValidator.New() .When(string.IsNullOrEmpty(nomeApresentacao), Resource.InvalidProductApresentation) .ThrowExceptionIfExists(); NomeApresentacao = nomeApresentacao; }
// Checks the conditions stored in the stone against the playermobile // passed it and reports publicly + with stone hue public void CheckConditions(PlayerMobile pm) { ArrayList FailurePoints = new ArrayList(); RuleValidator rv = new RuleValidator(this); rv.Validate(pm, ref FailurePoints); if (FailurePoints.Count > 0) { // Tell them what + why pm.PublicOverheadMessage(Network.MessageType.Regular, 0, false, string.Format("You failed the test for {0} reason{1} :", FailurePoints.Count, FailurePoints.Count != 1 ? "s" : "")); foreach (string strFailure in FailurePoints) pm.PublicOverheadMessage(Network.MessageType.Regular, 0, false, strFailure); // Hue the rune invalid ((AddonComponent)Components[0]).Hue = 137; new RehueTimer(pm, this).Start(); } else { pm.PublicOverheadMessage(Network.MessageType.Regular, 0, false, "You've passed the test!"); // Hue the rune valid ((AddonComponent)Components[0]).Hue = 1000; new RehueTimer(pm, this).Start(); } }
public void Storer(ProjectDto projectDto) { var dataOrcamento = new DateTime(); RuleValidator.New() .When(!Enum.TryParse <Status>(projectDto.Status, out var status), Resource.InvalidStatus) .When(!string.IsNullOrEmpty(projectDto.DataOrcamento) && !DateTime.TryParseExact(projectDto.DataOrcamento, "ddMMyyyy", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dataOrcamento), Resource.InvalidDataOrcamentoProjeto) .ThrowExceptionIfExists(); if (projectDto.Id == 0) { var project = new Project(projectDto.Descricao, projectDto.Largura, projectDto.Comprimento, dataOrcamento, status); _projectRepository.Add(project); } else { var project = _projectRepository.GetById(projectDto.Id); RuleValidator.New() .When(project == null, Resource.ProjectNotFound) .ThrowExceptionIfExists(); project.ChangeDataOrcamento(dataOrcamento); project.ChangeDescricao(projectDto.Descricao); project.ChangeStatus(status); } }
public void SimpleRuleLoaderPersonValidateExpressionRules() { Person person1 = new Person() { Name = "Mathias", Age = 35, Children = 2 }; Person person2 = new Person() { Name = "Anna", Age = 32, Children = 2 }; ExpressionRuleLoader expressionRuleLoader = new ExpressionRuleLoader(); // new Rule(" Name = 'mathias' "); Rule rule1 = expressionRuleLoader.Load(1); // new Rule(" Age = 35 "); Rule rule2 = expressionRuleLoader.Load(2); RuleValidator ruleValidator = new RuleValidator(); var result = ruleValidator.ValidateExpressionRules(new Person[] { person1, person2 }, new Rule[] { rule1, rule2 }); foreach (var item in result) { if (item.Item3) { Debug.WriteLine("Value: " + item.Item1.Name + " with ProcessingRule: " + item.Item2.ProcessingRule + item.Item2.Value + " passed."); } else { Debug.WriteLine("Value: " + item.Item1.Name + " with ProcessingRule: " + item.Item2.ProcessingRule + item.Item2.Value + " failed."); } } }
public void SimpleRuleLoaderPersonValidateRulesCount() { Person person1 = new Person() { Name = "Mathias", Birthdate = new DateTime(1976, 5, 9) }; Person person2 = new Person() { Name = "Anna", Birthdate = new DateTime(2001, 4, 4) }; Rule rule1 = new Rule("Birthdate", Operator.GreaterThanOrEqual, new DateTime(2000, 1, 1)); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFunc = ruleEngine.CompileRule <Person>(rule1); RuleValidator ruleValidator = new RuleValidator(); // calculates the count of persons that match rule1 var resultSingleRule = ruleValidator.ValidateRuleCount(new[] { person1, person2 }, ruleFunc); // one person ("Anna") matches the Birthdate rule Assert.AreEqual(resultSingleRule, 1); Rule rule2 = new Rule("Name", Operator.Equal, "Anna"); var combinedRulesFunc = ruleEngine.CombineRules <Person>(new Rule[] { rule1, rule2 }); // calculates the count of persons that match rule1 and rule2 var resultMultibleRule = ruleValidator.ValidateRulesCount(new[] { person1, person2 }, combinedRulesFunc); // one person ("Anna") matches the Birthdate rule and the Name rule Assert.AreEqual(resultMultibleRule, 1); }
public void ChangeCodBonanza(string codBonanza) { RuleValidator.New() .When(string.IsNullOrEmpty(codBonanza), Resource.InvalidProductCode) .ThrowExceptionIfExists(); CodBonanza = codBonanza; }
public void SimpleExpressionEvaluatorRules() { Person person1 = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true, Birthdate = new DateTime(1976, 5, 9) }; Person person2 = new Person() { Name = "Anna", Age = 32, Children = 2, Married = false, Birthdate = new DateTime(2002, 2, 2) }; RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); RuleValidator ruleValidator = new RuleValidator(); var counter = ruleValidator.ValidateExpressionRulesCount( new Person[] { person1, person2 }, new Rule[] { new Rule("Birthdate < '2001-09-05'") }); Assert.AreEqual(counter, 1); }
public void Storer(UserDto userDto) { var userFound = _userRepository.GetByEmail(userDto.Email); RuleValidator.New() .When(userFound != null && userFound.Id != userDto.Id, Resource.UserAlreadyExists) .When(!Enum.TryParse <Status>(userDto.Status, out var status), Resource.InvalidStatus) .When(!Enum.TryParse <TipoUsuario>(userDto.TipoUsuario, out var tipoUsuario), Resource.InvalidTipoUsuario) .ThrowExceptionIfExists(); if (userDto.Id == 0) { var user = new User(userDto.Nome, userDto.Email, userDto.Empresa, userDto.Telefone, userDto.Senha, userDto.GeoLocalizacao, tipoUsuario, status); _userRepository.Add(user); } else { userFound = _userRepository.GetById(userDto.Id); RuleValidator.New() .When(userFound == null, Resource.UserNotFound) .ThrowExceptionIfExists(); userFound.ChangeEmail(userDto.Email); userFound.ChangeEmpresa(userDto.Empresa); userFound.ChangeGeoLocalizacao(userDto.GeoLocalizacao); userFound.ChangeNome(userDto.Nome); userFound.ChangeTelefone(userDto.Telefone); } }
public void Save(CourseDTO courseDTO) { var cursoJaSalvo = _courseRepository.GetByName(courseDTO.Name); RuleValidator.New() .When(cursoJaSalvo != null && cursoJaSalvo.Id != courseDTO.Id, Resource.CourseNameAlreadyExists) .When(!Enum.TryParse(courseDTO.TargetAudience, out TargetAudience targetAudience), Resource.InvalidTargetAudience) .ThrowExceptionIfExists(); var curso = new Course(courseDTO.Name, courseDTO.Workload, targetAudience, courseDTO.CourseFee, courseDTO.Description); if (courseDTO.Id > 0) { curso = _courseRepository.GetById(courseDTO.Id); curso.UpdateName(courseDTO.Name) .UpdateCourseFee(courseDTO.CourseFee) .UpdateWorkload(courseDTO.Workload) .UpdateDescription(courseDTO.Description) .UpdateTargetAudience(targetAudience); } else { _courseRepository.Add(curso); } }
public void BeforeCreate(List <NOMEvaluationCategory> lstObjectsToValidate) { //all good var validator = new RuleValidator <NOMEvaluationCategory>(); validator.ValidateRules(lstObjectsToValidate, createRules); }
public void Storer(ItemProjetoDto itemProjetoDto) { var projetoFound = _projectRepository.GetById(itemProjetoDto.ProjetoId); RuleValidator.New() .When(projetoFound == null, Resource.ProjectNotFound); }
public static ValidationResult Create(RuleValidator validator, RuleValidatorContext context, IList <Object> parameterValues, object messageKey, IEnumerable <ValidationResult> nestedValidationResults = null) { string message = string.Empty; var messageService = new MessageService(); if (String.IsNullOrEmpty(validator.Message)) { var messageContext = new MessageContext(context, validator.GetType(), validator.Negate, validator.MessageStoreName, messageKey, validator.MessageFormatter); message = messageService.GetDefaultMessageAndFormat(messageContext, parameterValues); } else { //Since the message was supplied, don't get the default message from the store, just format it message = messageService.FormatMessage(validator.Message, context, parameterValues, validator.MessageFormatter); } //Override level if all the nested validation errors are Warnings if (nestedValidationResults != null && nestedValidationResults.All(vr => vr.Level == ValidationLevelType.Warn)) { return(new ValidationResult(context.PropertyInfo, message, ValidationLevelType.Warn, context.PropertyValue, nestedValidationResults)); } else { return(new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults)); } //return new ValidationResult(context.PropertyInfo, message, context.Level, context.PropertyValue, nestedValidationResults); }
public ModelClientValidationRule Create(RuleValidator ruleValidator) { var clientRule = new ModelClientValidationRule(); var ruleValidatorType = ruleValidator.GetType().GetGenericTypeDefinition(); if (!Mapping.ContainsKey(ruleValidatorType)) { return(null); } var rule = Mapping[ruleValidatorType]; clientRule.ValidationType = rule.JQueryRuleName; clientRule.ErrorMessage = ruleValidator.ErrorMessageTemplate; //map all the parameters foreach (var parameter in rule.Parameters) { // parameter.value is index of the matching expression in the rulevalidator PropertyExpressions collection var ruleParamQry = from ruleParameter in ruleValidator.Params where ruleParameter.PropertyName == parameter.Value select ruleParameter; if (ruleParamQry.Any()) { var ruleParam = ruleParamQry.First(); if (ruleParam.IsExpressionParam) { var expression = ruleParam.CompiledExpression.Expression; if (expression.Body.NodeType == ExpressionType.MemberAccess) { var propertyName = ((MemberExpression)expression.Body).Member.Name; var propertyType = ((MemberExpression)expression.Body).Type; clientRule.ValidationParameters.Add(parameter.Key, new PropertyExpressionParam() { PropertyName = propertyName, IsDate = propertyType == typeof(DateTime) }); } } else { var paramValue = ruleParam.GetParamValue(); if (paramValue is DateTime) { clientRule.ValidationParameters.Add(parameter.Key, new DateTimeParam(ruleParam.GetParamValue())); } else { //parameter.value is the index of the matching value in the rulevalidator parameters collection clientRule.ValidationParameters.Add(parameter.Key, ruleParam.GetParamValue()); } } } } return(clientRule); }
private static void ValidatePESTXMLReflection(string xmlfil) { var rulesToValidateAgainst = new List <string>(); var genRulesToValidateAgainst = new List <string>() { "GBR8a", "GBR15", "GBR16", "GBR18", "GBR19", "GBR20", "GBR21", "GBR22", "GBR23", "GBR24", "GBR25", "GBR26", "GBR27", "GBR28", "GBR29", "GBR30", "GBR31", "GBR32", "GBR33", "GBR34", "GBR35", "GBR36", "GBR37", "GBR38", "GBR39", "GBR40", "GBR41", "GBR42", "GBR43", "GBR44", "GBR45", "GBR46", "GBR47", "GBR48", "GBR49", "GBR50", "GBR51", "GBR53", "GBR54", "GBR55", "GBR56", "GBR57", "GBR58", "GBR60", "GBR61", "GBR62", "GBR63", "GBR64", "GBR65", "GBR67", "GBR69", "GBR70", "GBR71", "GBR72", "GBR73", "GBR74", "GBR75", "GBR77", "GBR78", "GBR79", "GBR80", "GBR81", "GBR82", "GBR83", "GBR85", "GBR86", "GBR87", "GBR88", "GBR89", "GBR90", "GBR91", "GBR92", "GBR93", "GBR94", "GBR95", "GBR96", "GBR97", "GBR99", "GBR100" }; var missingGenRulesToValidateAgainst = new List <string>() { "GBR12", "GBR13", "GBR14", "GBR34a" }; var pestRulesToValidateAgainst = new List <string>() { "PEST04", "PEST08", "PEST09", "PEST10", "PEST11", "PEST12", "PEST13", "PEST14", "PEST17", "PEST18", "PEST19", "PEST20", "PEST21", "PEST22", "PEST23", "PEST24", "PEST25", "PEST_sampInfo005", "PEST_sampInfo009", "PEST_sampInfo018", "PEST_sampInfo019", "PEST669_1", "PEST669_CN", "PEST669_DO", "PEST669_EG", "PEST669_KE", "PEST669_KH", "PEST669_TH", "PEST669_TR", "PEST669_VN", "MTX_W06", "MRL_01", "MRL_02", "MRL_03" }; var missingPestRulesToValidateAgainst = new List <string>() { "FOODEX2_SAMPMAT", "FOODEX2_ANMAT", "FOODEX2_TO_MATRIX", "PEST13_a", "PEST669_BJ", "MTX_E04", "MTX_W02", "MTX_E08", "MTX_W04", "BABYF_E05", "BABYF_W01", "BABYF_E02", "BABYF_W05", "MTX_E01", "MRL_04", "MRL_05" }; var inactiveRules = new List <string>() { "GBR17", "GBR66" }; rulesToValidateAgainst.AddRange(genRulesToValidateAgainst); //rulesToValidateAgainst.AddRange(missingGenRulesToValidateAgainst); //rulesToValidateAgainst.AddRange(pestRulesToValidateAgainst); //rulesToValidateAgainst.AddRange(missingPestRulesToValidateAgainst); var xml = XDocument.Load(xmlfil); var samples = XDocument.Load(@xmlfil).Descendants("result"); //Använder Workflow 2 var tests = new List <BusinessRuleError>(); var ruleValidator = new RuleValidator("2017"); Console.WriteLine($"There are {samples.Count()} results in the xml-file"); foreach (var el in samples) { var t = ruleValidator.ValidateRules(RuleValidatorType.PEST, rulesToValidateAgainst, el); tests.AddRange(t.Select(x => new BusinessRuleError() { El = x.El, Meddelande = x.Message, Outcome = x.Outcome, })); } PrintErrorCountInfo(tests); HandleErrorInfo(tests); }
public void ChangeValue(decimal value) { RuleValidator.New() .When(value < 1, Messages.INVALID_VALUE) .ThrowExceptionIfExists(); this.Value = value; }
public void ChangeWorkLoad(double workLoad) { RuleValidator.New() .When(workLoad < 1, Messages.INVALID_WORKLOAD) .ThrowExceptionIfExists(); this.WorkLoad = workLoad; }
public void ChangeTelefone(string telefone) { RuleValidator.New() .When(string.IsNullOrEmpty(telefone), Resource.InvalidUserTelefone) .ThrowExceptionIfExists(); Telefone = telefone; }
public void ChangeName(string name) { RuleValidator.New() .When(string.IsNullOrEmpty(name), Messages.INVALID_NAME) .ThrowExceptionIfExists(); this.Name = name; }
public void ChangeEmail(string email) { RuleValidator.New() .When(string.IsNullOrEmpty(email), Resource.InvalidUserEmail) .ThrowExceptionIfExists(); Email = email; }
public void ChangeEmpresa(string empresa) { RuleValidator.New() .When(string.IsNullOrEmpty(empresa), Resource.InvalidUserEmpresa) .ThrowExceptionIfExists(); Empresa = empresa; }
public void ChangeDescricao(string descricao) { RuleValidator.New() .When(string.IsNullOrEmpty(descricao), Resource.InvalidProjectDescription) .ThrowExceptionIfExists(); Descricao = descricao; }
public void ChangeNome(string nome) { RuleValidator.New() .When(string.IsNullOrEmpty(nome), Resource.InvalidUserNome) .ThrowExceptionIfExists(); Nome = nome; }
public void ChangeFotoProjeto(string fotoProjeto) { RuleValidator.New() .When(string.IsNullOrEmpty(fotoProjeto), Resource.InvalidProductProjectPhoto) .ThrowExceptionIfExists(); FotoProjeto = fotoProjeto; }
public void Cancel() { RuleValidator.New() .When(Course.Concluded, Messages.CONCLUDED_REGISTRATION) .ThrowExceptionIfExists(); Canceled = true; }
public Student UpdateEmail(string email) { RuleValidator .New() .When(string.IsNullOrEmpty(email) || !RegexUtilities.IsValidEmail(email), Resource.InvalidEmail) .ThrowExceptionIfExists(); Email = email; return(this); }
public Course UpdateName(string name) { RuleValidator .New() .When(string.IsNullOrEmpty(name), "The name cannot be empty or null") .ThrowExceptionIfExists(); Name = name; return(this); }
public Course UpdateWorkload(int workload) { RuleValidator .New() .When(workload < 1, Resource.InvalidWorkload) .ThrowExceptionIfExists(); Workload = workload; return(this); }
public Course UpdateCourseFee(double courseFee) { RuleValidator .New() .When(courseFee < 1, Resource.InvalidCourseFee) .ThrowExceptionIfExists(); CourseFee = courseFee; return(this); }
public Student UpdateName(string name) { RuleValidator .New() .When(string.IsNullOrEmpty(name), Resource.InvalidName) .ThrowExceptionIfExists(); Name = name; return(this); }
// Checks the conditions stored in the stone against the playermobile // passed it and reports publicly + with stone hue public void CheckConditions(PlayerMobile pm) { ArrayList FailurePoints = new ArrayList(); RuleValidator rv = new RuleValidator( this ); rv.Validate(pm, ref FailurePoints); if(FailurePoints.Count > 0) { // Tell them what + why pm.PublicOverheadMessage( Network.MessageType.Regular, 0, false, string.Format("You failed the test for {0} reason{1} :", FailurePoints.Count, FailurePoints.Count!=1 ? "s" : "") ); foreach (string strFailure in FailurePoints) pm.PublicOverheadMessage( Network.MessageType.Regular, 0, false, strFailure ); // Hue the rune invalid ((AddonComponent) Components[0]).Hue = 137; new RehueTimer( pm, this ).Start(); } else { pm.PublicOverheadMessage( Network.MessageType.Regular, 0, false, "You've passed the test!" ); // Hue the rune valid ((AddonComponent) Components[0]).Hue = 1000; new RehueTimer( pm, this ).Start(); } }