Skip to content

Commit

Permalink
feat: support blank or missing name on API request (generates random …
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Feb 21, 2023
1 parent 5addd54 commit 01731e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/models/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const reservedEmailAddressesList = require('reserved-email-addresses-list');
const slug = require('speakingurl');
const striptags = require('striptags');
const { isIP, isEmail, isURL } = require('validator');
const { randomstring } = require('@sidoshi/random-string');

// <https://github.com/Automattic/mongoose/issues/5534>
mongoose.Error.messages = require('@ladjs/mongoose-error-messages');
Expand Down Expand Up @@ -126,9 +127,16 @@ const Aliases = new mongoose.Schema({
Aliases.plugin(captainHook);

Aliases.pre('validate', function (next) {
// if name was not a string then generate a random one
if (!isSANB(this.name)) {
this.name = randomstring({
characters: 'abcdefghijklmnopqrstuvwxyz0123456789',
length: 10
});
}

// require alias name
if (
!isSANB(this.name) ||
!quotedEmailUserUtf8.test(this.name.trim().toLowerCase()) ||
(!this.name.trim().startsWith('/') && this.name.includes('+'))
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ curl -X GET BASE_URI/v1/domains/DOMAIN_NAME/aliases \
| Body Parameter | Required | Type | Description |
| ---------------------------- | -------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name` | Yes | String | Alias name |
| `name` | No | String | Alias name (if not provided or if blank, then a random alias is generated) |
| `recipients` | Yes | String or Array | List of recipients (must be line-break/space/comma separated String or Array of valid email addresses, fully-qualified domain names ("FQDN"), IP addresses, and/or webhook URL's) |
| `description` | No | String | Alias description |
| `labels` | No | String or Array | List of labels (must be line-break/space/comma separated String or Array) |
Expand Down

0 comments on commit 01731e2

Please sign in to comment.