1- import changeCase from 'case' ;
21import has from 'lodash/has' ;
32import omit from 'lodash/omit' ;
3+ import snakeCase from 'lodash/snakeCase' ;
4+ import camelCase from 'lodash/camelCase' ;
45import mapKeys from 'lodash/mapKeys' ;
56import transform from 'lodash/transform' ;
67
@@ -20,20 +21,8 @@ function unknown(ctx, value) {
2021 . filter ( key => known . indexOf ( key ) === - 1 )
2122}
2223
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 ( / [ ^ _ ] / )
3024
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 ) {
3726 if ( ! ( this instanceof ObjectSchema ) )
3827 return new ObjectSchema ( spec )
3928
@@ -222,8 +211,10 @@ inherits(ObjectSchema, MixedSchema, {
222211 } ,
223212
224213 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+ }
227218
228219 var next = this . test ( {
229220 name : 'noUnknown' ,
@@ -244,18 +235,21 @@ inherits(ObjectSchema, MixedSchema, {
244235 return next
245236 } ,
246237
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 ) )
250241 )
251242 } ,
252243
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 )
257250 } ,
258- } )
259251
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