2016-02-27 68 views
0

我在项目中使用流星,其中aldeed:简单模式用于验证输入。我的模式看起来像这样:流星/ aldeed:简单模式 - RegexProblem

const name = { 
    type: 'String', 
    regEx: /^[\w\d]+$/, 
    optional: false 
}; 

const nationalId = { 
    type: 'String', 
    regEx: /^[\w\d]+$/, 
    optional: true 
}; 

schema = new SimpleSchema({ 
    firstName: name, 
    lastName: name, 
    nationalId 
}); 

我测试了架构的工作原理是通过传递非字符串值或在名称字段中省略必需的值。所有这些都按预期工作。

但是,regEx验证不起作用。它似乎接受任何字符串,如'123%^ &'。我测试了一些这些字符串here,他们应该都不通过。这是我第一次用简单模式的预定义正则表达式以外的其他东西,并想知道我是否错过了一些东西。

我也尝试将正则表达式放在数组中,具有相同的效果。

我使用流星1.2,并已更新到所有软件包的最新版本:

accounts-password  1.1.4 Password support for accounts             
accounts-ui   1.1.6 Simple templates to add login widgets to an app         
aldeed:collection2 2.9.0 Automatic validation of insert and update  operations on the client and server. 
aldeed:simple-schema 1.5.3 A simple schema validation object with reactivity. Used by collection2 and au... 
blaze-html-templates 1.0.1 Compile HTML templates into reactive UI with Meteor Blaze      
ecmascript   0.1.6* Compiler plugin that supports ES2015+ in all .js files       
es5-shim    4.1.14 Shims and polyfills to improve ECMAScript 5 support        
fourseven:scss  3.4.1 Style with attitude. Sass and SCSS support for Meteor.js (with autoprefixer a... 
jquery    1.11.4 Manipulate the DOM using CSS selectors           
kadira:blaze-layout 2.3.0 Layout Manager for Blaze (works well with FlowRouter)       
kadira:flow-router 2.10.1 Carefully Designed Client Side Router for Meteor        
mdg:validated-method 1.0.1 A simple wrapper for Meteor.methods            
meteor-base   1.0.1 Packages that every Meteor app needs            
meteortoys:allthings 2.3.1 Insanely Handy Development Tools             
mobile-experience  1.0.1 Packages for a great mobile user experience          
mongo     1.1.3 Adaptor for using MongoDB and Minimongo over DDP         
session    1.1.1 Session variable                 
standard-minifiers 1.0.2 Standard minifiers used with Meteor apps by default.        
tracker    1.0.9 Dependency tracker to allow reactive callbacks         
useraccounts:core  1.13.1 Meteor sign up and sign in templates core package.        
wolves:bitters  3.1.0 Meteor 1.2.0+ - Scaffold styles, variables and structure for Bourbon projects. 
wolves:bourbon  3.1.0 Meteor 1.2.0+ - Bourbon is a simple and lightweight mixin library for Sass.  
wolves:neat   3.1.0 Meteor 1.2.0+ - A lightweight, semantic grid framework built on top of Bourbon. 
+1

是否有匹配'name'和'national ID'的规则?既然你正在使用'/^[\ w \ d] + $ /',我猜想你缺少特定的匹配规则。 – 2016-02-27 14:46:21

+0

最后的规则应该是'/^[\ w \ d \ s - ] + $ /'的名字和'/^[\ w \ d - ] + $ /'的国家ID。问题不在于regEx匹配似乎被验证拒绝。它使我使用'aldeed:simple-schema'的一些方法不正确,我只是不明白为什么。 – Hans

+1

*更新*:修正了我声明'type:“String”'而不是'type:String',导致正则表达式不被调用。 – Hans

回答

0

我解决了自己的问题。按照上面的评论,我不得不设置类型

type: String 

,而不是

type: "String" 

这就引发了正则表达式被解雇。