@@ -24,7 +24,19 @@ type SchemaFields = { [string]: SchemaField }
24
24
type Schema = {
25
25
className : string ,
26
26
fields : SchemaFields ,
27
- classLevelPermissions : any
27
+ classLevelPermissions : ClassLevelPermissions
28
+ } ;
29
+
30
+ type ClassLevelPermissions = {
31
+ find ?: { [ string ] : boolean } ;
32
+ count ?: { [ string ] : boolean } ;
33
+ get ?: { [ string ] : boolean } ;
34
+ create ?: { [ string ] : boolean } ;
35
+ update ?: { [ string ] : boolean } ;
36
+ delete ?: { [ string ] : boolean } ;
37
+ addField ?: { [ string ] : boolean } ;
38
+ readUserFields ?: string [ ] ;
39
+ writeUserFields ?: string [ ] ;
28
40
} ;
29
41
30
42
// @flow -disable-next
@@ -175,17 +187,21 @@ function verifyPermissionKey(key) {
175
187
}
176
188
177
189
const CLPValidKeys = Object . freeze ( [ 'find' , 'count' , 'get' , 'create' , 'update' , 'delete' , 'addField' , 'readUserFields' , 'writeUserFields' ] ) ;
178
- function validateCLP ( perms : any , fields : SchemaFields ) {
190
+ function validateCLP ( perms : ClassLevelPermissions , fields : SchemaFields ) {
179
191
if ( ! perms ) {
180
192
return ;
181
193
}
182
194
Object . keys ( perms ) . forEach ( ( operation ) => {
183
195
if ( CLPValidKeys . indexOf ( operation ) == - 1 ) {
184
196
throw new Parse . Error ( Parse . Error . INVALID_JSON , `${ operation } is not a valid operation for class level permissions` ) ;
185
197
}
198
+ if ( ! perms [ operation ] ) {
199
+ return ;
200
+ }
186
201
187
202
if ( operation === 'readUserFields' || operation === 'writeUserFields' ) {
188
203
if ( ! Array . isArray ( perms [ operation ] ) ) {
204
+ // @flow -disable-next
189
205
throw new Parse . Error ( Parse . Error . INVALID_JSON , `'${ perms [ operation ] } ' is not a valid value for class level permissions ${ operation } ` ) ;
190
206
} else {
191
207
perms [ operation ] . forEach ( ( key ) => {
@@ -197,10 +213,13 @@ function validateCLP(perms: any, fields: SchemaFields) {
197
213
return ;
198
214
}
199
215
216
+ // @flow -disable-next
200
217
Object . keys ( perms [ operation ] ) . forEach ( ( key ) => {
201
218
verifyPermissionKey ( key ) ;
219
+ // @flow -disable-next
202
220
const perm = perms [ operation ] [ key ] ;
203
221
if ( perm !== true ) {
222
+ // @flow -disable-next
204
223
throw new Parse . Error ( Parse . Error . INVALID_JSON , `'${ perm } ' is not a valid value for class level permissions ${ operation } :${ key } :${ perm } ` ) ;
205
224
}
206
225
} ) ;
@@ -341,7 +360,7 @@ const _AudienceSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
341
360
} ) ) ;
342
361
const VolatileClassesSchemas = [ _HooksSchema , _JobStatusSchema , _JobScheduleSchema , _PushStatusSchema , _GlobalConfigSchema , _AudienceSchema ] ;
343
362
344
- const dbTypeMatchesObjectType = ( dbType : any , objectType : any ) => {
363
+ const dbTypeMatchesObjectType = ( dbType : SchemaField | string , objectType : SchemaField ) => {
345
364
if ( dbType . type !== objectType . type ) return false ;
346
365
if ( dbType . targetClass !== objectType . targetClass ) return false ;
347
366
if ( dbType === objectType . type ) return true ;
@@ -602,7 +621,7 @@ export default class SchemaController {
602
621
return this . validateSchemaData ( className , fields , classLevelPermissions , [ ] ) ;
603
622
}
604
623
605
- validateSchemaData ( className : string , fields : SchemaFields , classLevelPermissions : any , existingFieldNames : Array < string > ) {
624
+ validateSchemaData ( className : string , fields : SchemaFields , classLevelPermissions : ClassLevelPermissions , existingFieldNames : Array < string > ) {
606
625
for ( const fieldName in fields ) {
607
626
if ( existingFieldNames . indexOf ( fieldName ) < 0 ) {
608
627
if ( ! fieldNameIsValid ( fieldName ) ) {
@@ -695,7 +714,11 @@ export default class SchemaController {
695
714
return this . reloadData ( { clearCache : true } ) ;
696
715
} ) . then ( ( ) => {
697
716
// Ensure that the schema now validates
698
- if ( ! dbTypeMatchesObjectType ( this . getExpectedType ( className , fieldName ) , type ) ) {
717
+ const expectedType = this . getExpectedType ( className , fieldName ) ;
718
+ if ( typeof type === 'string' ) {
719
+ type = { type } ;
720
+ }
721
+ if ( ! expectedType || ! dbTypeMatchesObjectType ( expectedType , type ) ) {
699
722
throw new Parse . Error ( Parse . Error . INVALID_JSON , `Could not add field ${ fieldName } ` ) ;
700
723
}
701
724
// Remove the cached schema
@@ -706,7 +729,7 @@ export default class SchemaController {
706
729
}
707
730
708
731
// maintain compatibility
709
- deleteField ( fieldName : string , className : any , database : DatabaseController ) {
732
+ deleteField ( fieldName : string , className : string , database : DatabaseController ) {
710
733
return this . deleteFields ( [ fieldName ] , className , database ) ;
711
734
}
712
735
0 commit comments