@@ -58,22 +58,30 @@ Validator.prototype.validate = function(obj, schema) {
5858 return check ( obj ) ;
5959} ;
6060
61- Validator . prototype . wrapRequiredCheckSourceCode = function ( rule , innerSrc ) {
61+ Validator . prototype . wrapRequiredCheckSourceCode = function ( rule , innerSrc , resVar ) {
6262 const src = [ ] ;
63+ const defaultValue = rule . schema . default != null ? JSON . stringify ( rule . schema . default ) : null ;
64+
6365 // Required, optional, forbidden
66+ src . push ( `
67+ if (value === undefined || value === null) {
68+ ` ) ;
6469 if ( rule . schema . optional === true || rule . schema . type == "forbidden" ) {
6570 // Optional field
66- src . push ( `
67- if (value === undefined || value === null) {
68- // Do nothing, it's an optional field
69- } else {` ) ;
71+ if ( defaultValue != null && resVar ) {
72+ src . push ( `${ resVar } = ${ defaultValue } ;` ) ;
73+ } else {
74+ src . push ( "// Do nothing, it's an optional field" ) ;
75+ }
7076 } else {
7177 // Required field
72- src . push ( `
73- if (value === undefined || value === null) {
74- ${ this . makeError ( { type : "required" , actual : "value" , messages : rule . messages } ) }
75- } else {` ) ;
78+ if ( defaultValue != null && resVar ) {
79+ src . push ( `${ resVar } = ${ defaultValue } ;` ) ;
80+ } else {
81+ src . push ( this . makeError ( { type : "required" , actual : "value" , messages : rule . messages } ) ) ;
82+ }
7683 }
84+ src . push ( "} else {" ) ;
7785
7886 if ( innerSrc )
7987 src . push ( innerSrc ) ;
@@ -125,22 +133,7 @@ Validator.prototype.compile = function(schema) {
125133 ] ;
126134
127135 const rule = this . getRuleFromSchema ( schema ) ;
128- sourceCode . push ( this . compileRule ( rule , context , null , "context.fn[%%INDEX%%](value, field, null, errors, context);" ) ) ;
129- /*
130- context.rules[context.index] = rule;
131- const res = rule.ruleFunction.call(this, rule, null, context);
132- if (res.source) {
133- const fn = new Function("value", "field", "parent", "errors", "context", res.source);
134- context.fn[context.index] = fn;
135- sourceCode.push(this.wrapRequiredCheckSourceCode(rule, `
136- context.fn[${context.index}](value, field, null, errors, context);
137- `));
138-
139- } else {
140- sourceCode.push(this.wrapRequiredCheckSourceCode(rule));
141- }
142- context.index++;
143- */
136+ sourceCode . push ( this . compileRule ( rule , context , null , "context.fn[%%INDEX%%](value, field, null, errors, context);" , "value" ) ) ;
144137
145138 sourceCode . push ( "if (errors.length) {" ) ;
146139 sourceCode . push ( `
@@ -180,7 +173,7 @@ Validator.prototype.compile = function(schema) {
180173 } ;
181174} ;
182175
183- Validator . prototype . compileRule = function ( rule , context , path , innerSrc ) {
176+ Validator . prototype . compileRule = function ( rule , context , path , innerSrc , resVar ) {
184177 const sourceCode = [ ] ;
185178
186179 const item = this . cache . get ( rule . schema ) ;
@@ -195,7 +188,7 @@ Validator.prototype.compileRule = function(rule, context, path, innerSrc) {
195188 ${ innerSrc . replace ( "%%INDEX%%" , rule . index ) }
196189 rule.cycleStack.pop(value);
197190 }
198- ` ) ) ;
191+ ` , resVar ) ) ;
199192
200193 } else {
201194 this . cache . set ( rule . schema , rule ) ;
@@ -206,7 +199,7 @@ Validator.prototype.compileRule = function(rule, context, path, innerSrc) {
206199 if ( res . source ) {
207200 const fn = new Function ( "value" , "field" , "parent" , "errors" , "context" , res . source ) ;
208201 context . fn [ rule . index ] = fn ;
209- sourceCode . push ( this . wrapRequiredCheckSourceCode ( rule , innerSrc . replace ( "%%INDEX%%" , rule . index ) ) ) ;
202+ sourceCode . push ( this . wrapRequiredCheckSourceCode ( rule , innerSrc . replace ( "%%INDEX%%" , rule . index ) , resVar ) ) ;
210203 } else {
211204 sourceCode . push ( this . wrapRequiredCheckSourceCode ( rule ) ) ;
212205 }
0 commit comments