为什么我的“必需”验证器没有用registerFactory替换标准微风所需的验证器?

时间:2014-05-09 20:09:17

标签: entity metadata breeze validation

因为Breeze Required验证器无法正常工作,我们希望用我们自己的实现替换它。为此,我在验证我的属性之前调用了这个TS代码:

breeze.Validator.registerFactory(
(ctx?: breeze.ValidatorFunctionContext) =>
    new breeze.Validator(
        'required',
        (value: string, context: breeze.ValidatorFunctionContext) => {
            if (context.hasOwnProperty('allowEmptyStrings') && !context['allowEmptyStrings'] && typeof value === 'string') {
                return value != null && value.length > 0;
            }
            return value != null;
        },
        ctx),
'required');

当我跟踪registerFactory方法时,我看到indexable属性中所需函数更改的实现。

__config.functionRegistry[fnName] = fn;

现在,当我运行此代码来验证我的属性时:

this.isValid(this.entity.entityAspect.validateEntity());

我在微风中的validate方法中跟踪代码:

 function validate(aspect, validator, value, context) {

所需的验证器是错误的,它是微风的,不是我的?

这是怎么回事?

2 个答案:

答案 0 :(得分:0)

据我所知,breeze docs (see here)" registerFactory"只有在加载以前导出的数据时才需要。它说:

  

我们认为注册您的自定义验证程序是一种很好的做法,尽管您不必这样做,除非您从服务器以外的序列化来源获取元数据。安全地玩吧;注册他们。

" ...除非您从序列化来源获取元数据,否则不得超过" 意味着相反:工厂注册在其他所有地方都没用。如果你查看微风代码行6922 ff。,函数addValidators(dataProperty):它不会检查函数注册表。也许微风团队中的某个人可以更准确地解释这一点。

最好的方法:创建自己的验证器,然后手动将其添加到您的实体或财产。

答案 1 :(得分:0)

在第2320行修改Breeze源代码(1.4.11),说:

if (ctx && (ctx.allowEmptyStrings || ctx.property && ctx.property.allowEmptyStrings)) return true;

而不是:

if (ctx && ctx.allowEmptyStrings) return true;