FluentValidation.ts 多个验证器和消息
FluentValidation.ts multiple validators and messages
验证 child of parent 不显示 child 消息,仅显示 parent.
如何查看 child 和 parent 验证的所有消息?
interface IItem {
id: number;
subItem: ISubItem;
customSubItem: ICustomSubItem;
}
interface ISubItem {
someNumber: number;
}
interface ICustomSubItem {
someNumber: number;
}
class CustomSubItemValidator extends Validator<ICustomSubItem> {
constructor() {
super();
this.ruleFor('someNumber')
.greaterThan(2)
.withMessage('Please enter a number');
}
}
class SubItemValidator extends Validator<ISubItem> {
constructor() {
super();
this.ruleFor('someNumber')
.greaterThan(2)
.withMessage('Please enter a number');
}
}
class ItemValidator extends Validator<IItem> {
constructor() {
super();
this.ruleFor('id')
.greaterThan(0)
.withMessage('Please enter your an id');
this.ruleFor('subItem')
.setValidator(() => new SubItemValidator());
this.ruleFor('customSubItem')
.setValidator(() => new CustomSubItemValidator());
this.ruleFor('subItem')
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item');
}
}
const itemValidator = new ItemValidator();
console.log(itemValidator.validate({
id: 1,
subItem: { someNumber: 1 },
customSubItem: { someNumber: 2 }
}));
我刚刚才遇到这个问题,很抱歉让您久等了回复!
遗憾的是,不支持同时显示对象 属性 的 属性 级验证错误和子级验证错误。我们只能在错误对象中为 属性 设置一个值——它不能既是字符串又是对象。
此行为类似于简单 属性 的行为,因为第一个失败的规则是在错误对象中产生错误消息的原因 - 如果有多个失败的验证,我们仍然只显示第一个.
基本上,您需要决定是先应用 属性 级验证规则还是应用子级验证。
属性级第一:
this.ruleFor('subItem')
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item')
.setValidator(() => new SubItemValidator());
儿童级别优先:
this.ruleFor('subItem')
.setValidator(() => new SubItemValidator())
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item');
我真的希望这对您有所帮助,如果您还有任何问题,请随时 raise an issue on GitHub - 您应该会在那里得到更快的回复,因为我不经常扫描 Stack Overflow!
验证 child of parent 不显示 child 消息,仅显示 parent.
如何查看 child 和 parent 验证的所有消息?
interface IItem {
id: number;
subItem: ISubItem;
customSubItem: ICustomSubItem;
}
interface ISubItem {
someNumber: number;
}
interface ICustomSubItem {
someNumber: number;
}
class CustomSubItemValidator extends Validator<ICustomSubItem> {
constructor() {
super();
this.ruleFor('someNumber')
.greaterThan(2)
.withMessage('Please enter a number');
}
}
class SubItemValidator extends Validator<ISubItem> {
constructor() {
super();
this.ruleFor('someNumber')
.greaterThan(2)
.withMessage('Please enter a number');
}
}
class ItemValidator extends Validator<IItem> {
constructor() {
super();
this.ruleFor('id')
.greaterThan(0)
.withMessage('Please enter your an id');
this.ruleFor('subItem')
.setValidator(() => new SubItemValidator());
this.ruleFor('customSubItem')
.setValidator(() => new CustomSubItemValidator());
this.ruleFor('subItem')
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item');
}
}
const itemValidator = new ItemValidator();
console.log(itemValidator.validate({
id: 1,
subItem: { someNumber: 1 },
customSubItem: { someNumber: 2 }
}));
我刚刚才遇到这个问题,很抱歉让您久等了回复!
遗憾的是,不支持同时显示对象 属性 的 属性 级验证错误和子级验证错误。我们只能在错误对象中为 属性 设置一个值——它不能既是字符串又是对象。
此行为类似于简单 属性 的行为,因为第一个失败的规则是在错误对象中产生错误消息的原因 - 如果有多个失败的验证,我们仍然只显示第一个.
基本上,您需要决定是先应用 属性 级验证规则还是应用子级验证。
属性级第一:
this.ruleFor('subItem')
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item')
.setValidator(() => new SubItemValidator());
儿童级别优先:
this.ruleFor('subItem')
.setValidator(() => new SubItemValidator())
.must((subItem, item) => {
return subItem.someNumber > item.customSubItem.someNumber
})
.withMessage('Sub item number should be greater then custom sub item');
我真的希望这对您有所帮助,如果您还有任何问题,请随时 raise an issue on GitHub - 您应该会在那里得到更快的回复,因为我不经常扫描 Stack Overflow!