Skip to content

Commit

Permalink
fix: invalid name for Parse.Role throws incorrect error (parse-comm…
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy authored May 29, 2022
1 parent 00190aa commit 8326a6f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
9 changes: 9 additions & 0 deletions integration/test/ParseACLTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,13 @@ describe('Parse.ACL', () => {
const obj1withInclude = await query.first();
assert(obj1withInclude.get('other').get('ACL'));
});

it('prevents save with invalid role name', async () => {
expect(() => new Parse.Role(':%#', new Parse.ACL())).toThrow(
new Parse.Error(
Parse.Error.OTHER_CAUSE,
`A role's name can be only contain alphanumeric characters, _, -, and spaces.`
)
);
});
});
25 changes: 17 additions & 8 deletions src/ParseRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ParseRole extends ParseObject {
* @returns {(ParseObject|boolean)} true if the set succeeded.
*/
setName(name: string, options?: mixed): ParseObject | boolean {
this._validateName(name);
return this.set('name', name, options);
}

Expand Down Expand Up @@ -108,6 +109,18 @@ class ParseRole extends ParseObject {
return this.relation('roles');
}

_validateName(newName) {
if (typeof newName !== 'string') {
throw new ParseError(ParseError.OTHER_CAUSE, "A role's name must be a String.");
}
if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
throw new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
);
}
}

validate(attrs: AttributeMap, options?: mixed): ParseError | boolean {
const isInvalid = super.validate(attrs, options);
if (isInvalid) {
Expand All @@ -125,14 +138,10 @@ class ParseRole extends ParseObject {
"A role's name can only be set before it has been saved."
);
}
if (typeof newName !== 'string') {
return new ParseError(ParseError.OTHER_CAUSE, "A role's name must be a String.");
}
if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
return new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
);
try {
this._validateName(newName);
} catch (e) {
return e;
}
}
return false;
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/ParseRole-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('ParseRole', () => {
expect(role.getName()).toBe('');
});

it('should throw error string with invalid name', () => {
expect(() => new ParseRole('invalid:name', new ParseACL())).toThrow(
new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
)
);
});

it('can validate attributes', () => {
const acl = new ParseACL({ aUserId: { read: true, write: true } });
const role = new ParseRole('admin', acl);
Expand Down

0 comments on commit 8326a6f

Please sign in to comment.