Skip to content

Commit 8326a6f

Browse files
authored
fix: invalid name for Parse.Role throws incorrect error (#1481)
1 parent 00190aa commit 8326a6f

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

integration/test/ParseACLTest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,13 @@ describe('Parse.ACL', () => {
533533
const obj1withInclude = await query.first();
534534
assert(obj1withInclude.get('other').get('ACL'));
535535
});
536+
537+
it('prevents save with invalid role name', async () => {
538+
expect(() => new Parse.Role(':%#', new Parse.ACL())).toThrow(
539+
new Parse.Error(
540+
Parse.Error.OTHER_CAUSE,
541+
`A role's name can be only contain alphanumeric characters, _, -, and spaces.`
542+
)
543+
);
544+
});
536545
});

src/ParseRole.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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-9a-zA-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-9a-zA-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;

src/__tests__/ParseRole-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ describe('ParseRole', () => {
4545
expect(role.getName()).toBe('');
4646
});
4747

48+
it('should throw error string with invalid name', () => {
49+
expect(() => new ParseRole('invalid:name', new ParseACL())).toThrow(
50+
new ParseError(
51+
ParseError.OTHER_CAUSE,
52+
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
53+
)
54+
);
55+
});
56+
4857
it('can validate attributes', () => {
4958
const acl = new ParseACL({ aUserId: { read: true, write: true } });
5059
const role = new ParseRole('admin', acl);

0 commit comments

Comments
 (0)