@@ -24,10 +24,22 @@ type SchemaFields = { [string]: SchemaField }
24
24
type Schema = {
25
25
className : string ,
26
26
fields : SchemaFields ,
27
- classLevelPermissions : any ,
27
+ classLevelPermissions : ClassLevelPermissions ,
28
28
indexes ?: ?any
29
29
} ;
30
30
31
+ type ClassLevelPermissions = {
32
+ find ?: { [ string ] : boolean } ;
33
+ count ?: { [ string ] : boolean } ;
34
+ get ?: { [ string ] : boolean } ;
35
+ create ?: { [ string ] : boolean } ;
36
+ update ?: { [ string ] : boolean } ;
37
+ delete ?: { [ string ] : boolean } ;
38
+ addField ?: { [ string ] : boolean } ;
39
+ readUserFields ?: string [ ] ;
40
+ writeUserFields ?: string [ ] ;
41
+ } ;
42
+
31
43
// @flow -disable-next
32
44
const Parse = require ( 'parse/node' ) . Parse ;
33
45
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter' ;
@@ -176,17 +188,21 @@ function verifyPermissionKey(key) {
176
188
}
177
189
178
190
const CLPValidKeys = Object . freeze ( [ 'find' , 'count' , 'get' , 'create' , 'update' , 'delete' , 'addField' , 'readUserFields' , 'writeUserFields' ] ) ;
179
- function validateCLP ( perms : any , fields : SchemaFields ) {
191
+ function validateCLP ( perms : ClassLevelPermissions , fields : SchemaFields ) {
180
192
if ( ! perms ) {
181
193
return ;
182
194
}
183
195
Object . keys ( perms ) . forEach ( ( operation ) => {
184
196
if ( CLPValidKeys . indexOf ( operation ) == - 1 ) {
185
197
throw new Parse . Error ( Parse . Error . INVALID_JSON , `${ operation } is not a valid operation for class level permissions` ) ;
186
198
}
199
+ if ( ! perms [ operation ] ) {
200
+ return ;
201
+ }
187
202
188
203
if ( operation === 'readUserFields' || operation === 'writeUserFields' ) {
189
204
if ( ! Array . isArray ( perms [ operation ] ) ) {
205
+ // @flow -disable-next
190
206
throw new Parse . Error ( Parse . Error . INVALID_JSON , `'${ perms [ operation ] } ' is not a valid value for class level permissions ${ operation } ` ) ;
191
207
} else {
192
208
perms [ operation ] . forEach ( ( key ) => {
@@ -198,10 +214,13 @@ function validateCLP(perms: any, fields: SchemaFields) {
198
214
return ;
199
215
}
200
216
217
+ // @flow -disable-next
201
218
Object . keys ( perms [ operation ] ) . forEach ( ( key ) => {
202
219
verifyPermissionKey ( key ) ;
220
+ // @flow -disable-next
203
221
const perm = perms [ operation ] [ key ] ;
204
222
if ( perm !== true ) {
223
+ // @flow -disable-next
205
224
throw new Parse . Error ( Parse . Error . INVALID_JSON , `'${ perm } ' is not a valid value for class level permissions ${ operation } :${ key } :${ perm } ` ) ;
206
225
}
207
226
} ) ;
@@ -352,7 +371,7 @@ const _AudienceSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
352
371
} ) ) ;
353
372
const VolatileClassesSchemas = [ _HooksSchema , _JobStatusSchema , _JobScheduleSchema , _PushStatusSchema , _GlobalConfigSchema , _AudienceSchema ] ;
354
373
355
- const dbTypeMatchesObjectType = ( dbType : any , objectType : any ) => {
374
+ const dbTypeMatchesObjectType = ( dbType : SchemaField | string , objectType : SchemaField ) = > {
356
375
if ( dbType . type !== objectType . type ) return false ;
357
376
if ( dbType . targetClass !== objectType . targetClass ) return false ;
358
377
if ( dbType === objectType . type ) return true ;
@@ -629,7 +648,7 @@ export default class SchemaController {
629
648
return this . validateSchemaData ( className , fields , classLevelPermissions , [ ] ) ;
630
649
}
631
650
632
- validateSchemaData ( className : string , fields : SchemaFields , classLevelPermissions : any , existingFieldNames : Array < string > ) {
651
+ validateSchemaData ( className : string , fields : SchemaFields , classLevelPermissions : ClassLevelPermissions , existingFieldNames : Array < string > ) {
633
652
for ( const fieldName in fields ) {
634
653
if ( existingFieldNames . indexOf ( fieldName ) < 0 ) {
635
654
if ( ! fieldNameIsValid ( fieldName ) ) {
@@ -721,7 +740,11 @@ export default class SchemaController {
721
740
return this . reloadData ( { clearCache : true } ) ;
722
741
} ) . then ( ( ) => {
723
742
// Ensure that the schema now validates
724
- if ( ! dbTypeMatchesObjectType ( this . getExpectedType ( className , fieldName ) , type ) ) {
743
+ const expectedType = this . getExpectedType ( className , fieldName ) ;
744
+ if ( typeof type === 'string' ) {
745
+ type = { type } ;
746
+ }
747
+ if ( ! expectedType || ! dbTypeMatchesObjectType ( expectedType , type ) ) {
725
748
throw new Parse . Error ( Parse . Error . INVALID_JSON , `Could not add field ${ fieldName } ` ) ;
726
749
}
727
750
// Remove the cached schema
@@ -732,7 +755,7 @@ export default class SchemaController {
732
755
}
733
756
734
757
// maintain compatibility
735
- deleteField ( fieldName : string , className : any , database : DatabaseController ) {
758
+ deleteField ( fieldName : string , className : string , database : DatabaseController ) {
736
759
return this . deleteFields ( [ fieldName ] , className , database ) ;
737
760
}
738
761
0 commit comments