@@ -25,6 +25,15 @@ internals.Object = function () {
25
25
26
26
Hoek . inherits ( internals . Object , Any ) ;
27
27
28
+ internals . safeParse = function ( value ) {
29
+
30
+ try {
31
+ return JSON . parse ( value ) ;
32
+ }
33
+ catch ( parseErr ) { }
34
+
35
+ return value ;
36
+ } ;
28
37
29
38
internals . Object . prototype . _base = function ( value , state , options ) {
30
39
@@ -41,10 +50,7 @@ internals.Object.prototype._base = function (value, state, options) {
41
50
if ( typeof value === 'string' &&
42
51
options . convert ) {
43
52
44
- try {
45
- value = JSON . parse ( value ) ;
46
- }
47
- catch ( parseErr ) { }
53
+ value = internals . safeParse ( value ) ;
48
54
}
49
55
50
56
const type = this . _flags . func ? 'function' : 'object' ;
@@ -95,42 +101,42 @@ internals.Object.prototype._base = function (value, state, options) {
95
101
96
102
const renamed = { } ;
97
103
for ( let i = 0 ; i < this . _inner . renames . length ; ++ i ) {
98
- const item = this . _inner . renames [ i ] ;
104
+ const rename = this . _inner . renames [ i ] ;
99
105
100
- if ( item . options . ignoreUndefined && target [ item . from ] === undefined ) {
106
+ if ( rename . options . ignoreUndefined && target [ rename . from ] === undefined ) {
101
107
continue ;
102
108
}
103
109
104
- if ( ! item . options . multiple &&
105
- renamed [ item . to ] ) {
110
+ if ( ! rename . options . multiple &&
111
+ renamed [ rename . to ] ) {
106
112
107
- errors . push ( this . createError ( 'object.rename.multiple' , { from : item . from , to : item . to } , state , options ) ) ;
113
+ errors . push ( this . createError ( 'object.rename.multiple' , { from : rename . from , to : rename . to } , state , options ) ) ;
108
114
if ( options . abortEarly ) {
109
115
return finish ( ) ;
110
116
}
111
117
}
112
118
113
- if ( Object . prototype . hasOwnProperty . call ( target , item . to ) &&
114
- ! item . options . override &&
115
- ! renamed [ item . to ] ) {
119
+ if ( Object . prototype . hasOwnProperty . call ( target , rename . to ) &&
120
+ ! rename . options . override &&
121
+ ! renamed [ rename . to ] ) {
116
122
117
- errors . push ( this . createError ( 'object.rename.override' , { from : item . from , to : item . to } , state , options ) ) ;
123
+ errors . push ( this . createError ( 'object.rename.override' , { from : rename . from , to : rename . to } , state , options ) ) ;
118
124
if ( options . abortEarly ) {
119
125
return finish ( ) ;
120
126
}
121
127
}
122
128
123
- if ( target [ item . from ] === undefined ) {
124
- delete target [ item . to ] ;
129
+ if ( target [ rename . from ] === undefined ) {
130
+ delete target [ rename . to ] ;
125
131
}
126
132
else {
127
- target [ item . to ] = target [ item . from ] ;
133
+ target [ rename . to ] = target [ rename . from ] ;
128
134
}
129
135
130
- renamed [ item . to ] = true ;
136
+ renamed [ rename . to ] = true ;
131
137
132
- if ( ! item . options . alias ) {
133
- delete target [ item . from ] ;
138
+ if ( ! rename . options . alias ) {
139
+ delete target [ rename . from ] ;
134
140
}
135
141
}
136
142
@@ -185,15 +191,15 @@ internals.Object.prototype._base = function (value, state, options) {
185
191
186
192
for ( let i = 0 ; i < unprocessedKeys . length ; ++ i ) {
187
193
const key = unprocessedKeys [ i ] ;
194
+ const localState = { key, path : ( state . path ? state . path + '.' : '' ) + key , parent : target , reference : state . reference } ;
195
+ const item = target [ key ] ;
188
196
189
197
for ( let j = 0 ; j < this . _inner . patterns . length ; ++ j ) {
190
198
const pattern = this . _inner . patterns [ j ] ;
191
199
192
200
if ( pattern . regex . test ( key ) ) {
193
201
delete unprocessed [ key ] ;
194
202
195
- const item = target [ key ] ;
196
- const localState = { key, path : ( state . path ? state . path + '.' : '' ) + key , parent : target , reference : state . reference } ;
197
203
const result = pattern . rule . _validate ( item , localState , options ) ;
198
204
if ( result . errors ) {
199
205
errors . push ( this . createError ( 'object.child' , { key, reason : result . errors } , localState , options ) ) ;
0 commit comments