spee.ch/server/controllers/api/claim/publish/parsePublishApiRequestBody.js

40 lines
847 B
JavaScript
Raw Normal View History

2019-02-23 06:52:31 +01:00
const parsePublishApiRequestBody = ({
name,
nsfw,
license,
licenseUrl,
title,
description,
thumbnail,
}) => {
2018-04-27 18:54:36 +02:00
// validate name
if (!name) {
throw new Error('no name field found in request');
}
const invalidNameCharacters = /[^A-Za-z0-9,-]/.exec(name);
if (invalidNameCharacters) {
2019-02-23 06:52:31 +01:00
throw new Error(
'The claim name you provided is not allowed. Only the following characters are allowed: A-Z, a-z, 0-9, and "-"'
);
2018-04-27 18:54:36 +02:00
}
// optional parameters
2019-02-23 06:52:31 +01:00
nsfw = nsfw === 'true';
2018-04-27 18:54:36 +02:00
license = license || null;
2019-02-23 06:52:31 +01:00
licenseUrl = licenseUrl || null;
2018-04-27 18:54:36 +02:00
title = title || null;
description = description || null;
thumbnail = thumbnail || null;
// return results
return {
name,
nsfw,
license,
2019-02-23 06:52:31 +01:00
licenseUrl,
2018-04-27 18:54:36 +02:00
title,
description,
thumbnail,
};
};
module.exports = parsePublishApiRequestBody;