@@ -75,6 +75,7 @@ class ParseRole extends ParseObject {
7575 * @returns {(ParseObject|boolean) } true if the set succeeded.
7676 */
7777 setName ( name : string , options ?: mixed ) : ParseObject | boolean {
78+ this . _validateName ( name ) ;
7879 return this . set ( 'name' , name , options ) ;
7980 }
8081
@@ -108,6 +109,18 @@ class ParseRole extends ParseObject {
108109 return this . relation ( 'roles' ) ;
109110 }
110111
112+ _validateName ( newName ) {
113+ if ( typeof newName !== 'string' ) {
114+ throw new ParseError ( ParseError . OTHER_CAUSE , "A role's name must be a String." ) ;
115+ }
116+ if ( ! / ^ [ 0 - 9 a - z A - Z \- _ ] + $ / . test ( newName ) ) {
117+ throw new ParseError (
118+ ParseError . OTHER_CAUSE ,
119+ "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
120+ ) ;
121+ }
122+ }
123+
111124 validate ( attrs : AttributeMap , options ? : mixed ) : ParseError | boolean {
112125 const isInvalid = super . validate ( attrs , options ) ;
113126 if ( isInvalid ) {
@@ -125,14 +138,10 @@ class ParseRole extends ParseObject {
125138 "A role's name can only be set before it has been saved."
126139 ) ;
127140 }
128- if ( typeof newName !== 'string' ) {
129- return new ParseError ( ParseError . OTHER_CAUSE , "A role's name must be a String." ) ;
130- }
131- if ( ! / ^ [ 0 - 9 a - z A - Z \- _ ] + $ / . test ( newName ) ) {
132- return new ParseError (
133- ParseError . OTHER_CAUSE ,
134- "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
135- ) ;
141+ try {
142+ this . _validateName ( newName ) ;
143+ } catch ( e ) {
144+ return e ;
136145 }
137146 }
138147 return false ;
0 commit comments