gpt4 book ai didi

How to extend validation rule only for some RuleSets(如何仅为某些规则集扩展验证规则)

转载 作者:bug小助手 更新时间:2023-10-24 23:35:30 27 4
gpt4 key购买 nike



I have a property of the model that must always follow some constraints. In case of some actions (e.g. adding) it must also create a unique key. I see no way without duplicating the basic rule to conditionally extend it with an additional constraint.

我有一个模型的属性,它必须始终遵循一些限制。在某些操作(例如添加)的情况下,它还必须创建唯一密钥。我认为没有办法不复制基本规则,以有条件地扩展它,并增加一个额外的约束。


With FluentValidation I have built a Validator that has most of the Rules in the default (unnamed) RuleSet and some additional RuleSets including the "Adding". Wanted to call Validator with appropriate RuleSets to run depending on calling action (for add it would be "Adding" + default).
For the problematic property, let's say "PartialKey", I was thinking about using When but I haven't seen a place to take RuleSets which were passed at the validation call. Defining the rule in "Adding" RuleSet forces me to duplicate the basic part of the rule for "PartialKey" from the default RuleSet.

使用FluentValidation,我构建了一个验证器,它包含默认(未命名)规则集中的大部分规则,以及包括“添加”在内的一些附加规则集。我想使用适当的规则集调用Validator以根据调用操作运行(对于Add,它将是“Adding”+Default)。对于有问题的属性,假设“PartialKey”,我正在考虑使用When,但我还没有看到一个地方来获取在验证调用中传递的规则集。在“添加”Ruleset中定义规则会迫使我从默认Ruleset复制“PartialKey”规则的基本部分。


Simplified piece of code

简化的代码片段


public class MyValidator : AbstractValidator<Model>
{
public MyValidator()
{
this.RuleFor(model => model.PartialKey).MustMeetBasicConstraints()
.MustNotExistUniqueComposeKey()
.When(() => executingRuleSets.Contains("Adding"), ApplyConditionTo.CurrentValidator);

this.RuleSet("Adding", () =>
{
this.RuleFor(model => model.PartialKey).MustMeetBasicConstraints()
.MustNotExistUniqueComposeKey();
});
}
}

更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com