1
- import changeCase from 'case' ;
2
1
import has from 'lodash/has' ;
3
2
import omit from 'lodash/omit' ;
3
+ import snakeCase from 'lodash/snakeCase' ;
4
+ import camelCase from 'lodash/camelCase' ;
4
5
import mapKeys from 'lodash/mapKeys' ;
5
6
import transform from 'lodash/transform' ;
6
7
@@ -20,20 +21,8 @@ function unknown(ctx, value) {
20
21
. filter ( key => known . indexOf ( key ) === - 1 )
21
22
}
22
23
23
- /**
24
- * maintain "private" fields
25
- * `"__FOO_BAR"` becomes `"__fooBar"` not `"fooBar"`
26
- */
27
- function camelize ( str ) {
28
- let result = changeCase . camel ( str )
29
- , idx = str . search ( / [ ^ _ ] / )
30
24
31
- return idx === 0 ? result : ( str . substr ( 0 , idx ) + result )
32
- }
33
-
34
- module . exports = ObjectSchema
35
-
36
- function ObjectSchema ( spec ) {
25
+ export default function ObjectSchema ( spec ) {
37
26
if ( ! ( this instanceof ObjectSchema ) )
38
27
return new ObjectSchema ( spec )
39
28
@@ -222,8 +211,10 @@ inherits(ObjectSchema, MixedSchema, {
222
211
} ,
223
212
224
213
noUnknown ( noAllow = true , message = locale . noUnknown ) {
225
- if ( typeof noAllow === 'string' )
226
- message = noAllow , noAllow = true ;
214
+ if ( typeof noAllow === 'string' ) {
215
+ message = noAllow ;
216
+ noAllow = true ;
217
+ }
227
218
228
219
var next = this . test ( {
229
220
name : 'noUnknown' ,
@@ -244,18 +235,21 @@ inherits(ObjectSchema, MixedSchema, {
244
235
return next
245
236
} ,
246
237
247
- camelcase ( ) {
248
- return this . transform ( obj =>
249
- obj && mapKeys ( obj , ( _ , key ) => camelize ( key ) )
238
+ transformKeys ( fn ) {
239
+ return this . transform ( obj => obj &&
240
+ mapKeys ( obj , ( _ , key ) => fn ( key ) )
250
241
)
251
242
} ,
252
243
253
- constantcase ( ) {
254
- return this . transform ( obj =>
255
- obj && mapKeys ( obj , ( _ , key ) => changeCase . constant ( key ) )
256
- )
244
+ camelCase ( ) {
245
+ return this . transformKeys ( camelCase )
246
+ } ,
247
+
248
+ snakeCase ( ) {
249
+ return this . transformKeys ( snakeCase )
257
250
} ,
258
- } )
259
251
260
- ObjectSchema . prototype . camelCase = ObjectSchema . prototype . camelcase ;
261
- ObjectSchema . prototype . constantCase = ObjectSchema . prototype . constantcase ;
252
+ constantCase ( ) {
253
+ return this . transformKeys ( key => snakeCase ( key ) . toUpperCase ( ) )
254
+ } ,
255
+ } )
0 commit comments