-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Maksim Sukharev <[email protected]>
- Loading branch information
Showing
2 changed files
with
44 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import type { createPollParams } from '../types/index.ts' | ||
|
||
const pollFormExample: createPollParams = { | ||
question: '', | ||
options: ['', ''], | ||
resultMode: 0, | ||
maxVotes: 0, | ||
} | ||
const REQUIRED_KEYS: Array<keyof createPollParams> = Object.keys(pollFormExample) as Array<keyof createPollParams> | ||
|
||
/** | ||
* Parses a given JSON string and validates with required poll form object. | ||
* Throws an error if parsed object doesn't match | ||
* @param jsonString The string to evaluate | ||
*/ | ||
function validatePollForm(jsonString: string): createPollParams { | ||
const parsedObject = JSON.parse(jsonString) | ||
|
||
if (REQUIRED_KEYS.some(key => parsedObject[key] === undefined)) { | ||
throw new Error('Missing required key') | ||
} | ||
|
||
if (REQUIRED_KEYS.some(key => typeof pollFormExample[key] !== typeof parsedObject[key]) | ||
|| Object.values(parsedObject.options).some(opt => typeof opt !== 'string')) { | ||
throw new Error('Invalid parsed value') | ||
} | ||
|
||
return parsedObject | ||
} | ||
|
||
export { | ||
validatePollForm, | ||
} |