@@ -58,7 +58,7 @@ class Parameter {
5858
5959 if ( this . validateRoot && ( typeof obj !== 'object' || ! obj ) ) {
6060 return [ {
61- message : 'the validated value should be a object' ,
61+ message : this . t ( 'the validated value should be a object' ) ,
6262 code : this . t ( 'invalid' ) ,
6363 field : undefined ,
6464 } ] ;
@@ -111,34 +111,6 @@ class Parameter {
111111 return errors ;
112112 }
113113 }
114-
115- /**
116- * add custom rule
117- *
118- * @param {String } type
119- * @param {Function | RegExp } check
120- * @api public
121- */
122-
123- addRule ( type , check ) {
124- if ( ! type ) {
125- throw new TypeError ( '`type` required' ) ;
126- }
127-
128- if ( typeof check === 'function' ) {
129- TYPE_MAP [ type ] = check ;
130- return ;
131- }
132-
133- if ( check instanceof RegExp ) {
134- TYPE_MAP [ type ] = function ( rule , value ) {
135- return checkString . call ( this , { format : check } , value ) ;
136- } ;
137- return ;
138- }
139-
140- throw new TypeError ( 'check must be function or regexp' ) ;
141- }
142114} ;
143115
144116/**
@@ -147,6 +119,41 @@ class Parameter {
147119 */
148120module . exports = Parameter ;
149121
122+ /**
123+ * add custom rule to global rules list.
124+ *
125+ * @param {String } type
126+ * @param {Function | RegExp } check
127+ * @param {Boolean } [override] - override exists rule or not, default is true
128+ * @api public
129+ */
130+ Parameter . prototype . addRule = Parameter . addRule = function addRule ( type , check , override ) {
131+ if ( ! type ) {
132+ throw new TypeError ( '`type` required' ) ;
133+ }
134+
135+ if ( typeof override !== 'boolean' ) {
136+ override = true ;
137+ }
138+
139+ if ( ! override && TYPE_MAP [ type ] ) {
140+ throw new TypeError ( 'rule `' + type + '` exists' ) ;
141+ }
142+
143+ if ( typeof check === 'function' ) {
144+ TYPE_MAP [ type ] = check ;
145+ return ;
146+ }
147+
148+ if ( check instanceof RegExp ) {
149+ TYPE_MAP [ type ] = function ( rule , value ) {
150+ return checkString . call ( this , { format : check } , value ) ;
151+ } ;
152+ return ;
153+ }
154+
155+ throw new TypeError ( 'check must be function or regexp' ) ;
156+ } ;
150157
151158/**
152159 * Simple type map
@@ -299,7 +306,7 @@ function checkString(rule, value) {
299306 */
300307
301308function checkId ( rule , value ) {
302- return checkString . call ( this , { format : ID_RE , allowEmpty : rule . allowEmpty } , value ) ;
309+ return checkString . call ( this , { format : ID_RE , allowEmpty : rule . allowEmpty } , value ) ;
303310}
304311
305312/**
@@ -313,7 +320,7 @@ function checkId(rule, value) {
313320 */
314321
315322function checkDate ( rule , value ) {
316- return checkString . call ( this , { format : DATE_TYPE_RE , allowEmpty : rule . allowEmpty } , value ) ;
323+ return checkString . call ( this , { format : DATE_TYPE_RE , allowEmpty : rule . allowEmpty } , value ) ;
317324}
318325
319326/**
@@ -327,7 +334,7 @@ function checkDate(rule, value) {
327334 */
328335
329336function checkDateTime ( rule , value ) {
330- return checkString . call ( this , { format : DATETIME_TYPE_RE , allowEmpty : rule . allowEmpty } , value ) ;
337+ return checkString . call ( this , { format : DATETIME_TYPE_RE , allowEmpty : rule . allowEmpty } , value ) ;
331338}
332339
333340/**
0 commit comments