-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13487 from nextcloud/feat/13439/import-polls
- Loading branch information
Showing
4 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
/** | ||
* Converts given payload to Data URI scheme | ||
* data:[<mediatype>][;base64],<data> | ||
* | ||
* @param payload data to convert | ||
* @param mediatype a MIME type string | ||
* @param base64Token an optional base64 token if non-textual data is given | ||
*/ | ||
const convertToDataURI = function(payload: string, mediatype: string = 'text/plain;charset=US-ASCII', base64Token: string = ''): string { | ||
return 'data:' + mediatype + base64Token + ',' + encodeURIComponent(payload) | ||
} | ||
|
||
/** | ||
* Converts given JS object to Data URI scheme | ||
* | ||
* @param payload JS object to convert | ||
*/ | ||
const convertToJSONDataURI = function(payload: object): string { | ||
return convertToDataURI(JSON.stringify(payload, null, 2), 'application/json;charset=utf-8') | ||
} | ||
|
||
export { | ||
convertToDataURI, | ||
convertToJSONDataURI, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import type { createPollParams } from '../types/index.ts' | ||
|
||
type requiredPollParams = Omit<createPollParams, 'draft'> | ||
const pollFormExample = { | ||
question: '', | ||
options: ['', ''], | ||
resultMode: 0, | ||
maxVotes: 0, | ||
} | ||
const REQUIRED_KEYS: Array<keyof requiredPollParams> = Object.keys(pollFormExample) as Array<keyof requiredPollParams> | ||
|
||
/** | ||
* Parses a given JSON object and validates with required poll form object. | ||
* Throws an error if parsed object doesn't match | ||
* @param jsonObject The object to validate | ||
*/ | ||
function validatePollForm(jsonObject: requiredPollParams): requiredPollParams { | ||
if (typeof jsonObject !== 'object') { | ||
throw new Error('Invalid parsed object') | ||
} | ||
|
||
for (const key of REQUIRED_KEYS) { | ||
if (jsonObject[key] === undefined) { | ||
throw new Error('Missing required key') | ||
} | ||
|
||
if (typeof pollFormExample[key] !== typeof jsonObject[key]) { | ||
throw new Error('Invalid parsed value') | ||
} | ||
|
||
if (key === 'options' && jsonObject[key]?.some((opt: unknown) => typeof opt !== 'string')) { | ||
throw new Error('Invalid parsed option values') | ||
} | ||
} | ||
|
||
return jsonObject | ||
} | ||
|
||
export { | ||
validatePollForm, | ||
} |