@@ -75,6 +75,7 @@ class ParseRole extends ParseObject {
75
75
* @returns {(ParseObject|boolean) } true if the set succeeded.
76
76
*/
77
77
setName ( name : string , options ?: mixed ) : ParseObject | boolean {
78
+ this . _validateName ( name ) ;
78
79
return this . set ( 'name' , name , options ) ;
79
80
}
80
81
@@ -108,6 +109,18 @@ class ParseRole extends ParseObject {
108
109
return this . relation ( 'roles' ) ;
109
110
}
110
111
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
+
111
124
validate ( attrs : AttributeMap , options ? : mixed ) : ParseError | boolean {
112
125
const isInvalid = super . validate ( attrs , options ) ;
113
126
if ( isInvalid ) {
@@ -125,14 +138,10 @@ class ParseRole extends ParseObject {
125
138
"A role's name can only be set before it has been saved."
126
139
) ;
127
140
}
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 ;
136
145
}
137
146
}
138
147
return false ;
0 commit comments