UNPKG

@trivia-api/models

Version:

Models for The Trivia API.

228 lines (227 loc) 7.64 kB
import { isCategory } from "./category"; /** * Type guard to return whether a question is a valid v2 question * @param input */ export const isV2Question = (arg) => { return (isTextChoiceQuestion(arg) || isImageChoiceQuestion(arg) || isTextInputQuestion(arg)); }; export function assertIsV2Question(arg) { if (typeof arg !== "object" || arg === null) { throw new Error("Question is not an object"); } if (!("type" in arg)) { throw new Error("Question type is missing"); } if (arg.type === "text_choice") { if (!isTextChoiceQuestion(arg)) { throw new Error("Question is not a text choice question"); } } else if (arg.type === "image_choice") { assertIsImageChoiceQuestion(arg); } else if (arg.type === "text_input") { assertIsTextInputQuestion(arg); } else { throw new Error("Question type is invalid"); } } export const isTextChoiceQuestion = (arg) => { if (typeof arg === "object" && arg !== null) { if (["category", "correctAnswer"].every((key) => Object.keys(arg).includes(key))) { if (isCategory(arg.category)) { if (typeof arg.correctAnswer === "string") { if (Array.isArray(arg.incorrectAnswers) && arg.incorrectAnswers.every((a) => typeof a === "string")) { if (typeof arg.question === "object" && arg !== null) { if (typeof arg.question.text === "string") { if (Array.isArray(arg.tags) && arg.tags.every((a) => typeof a === "string")) { if (arg.type === "text_choice") { return true; } } } } } } } } } return false; }; const isImageOption = (arg) => { if (typeof arg !== "object" || arg === null) { console.error(arg); throw new Error("Image option is not an object"); } if (!("url" in arg)) { console.error(arg); throw new Error("Image option does not have url"); } if (typeof arg.url !== "string") { console.error(arg); throw new Error("Image option url is not a string"); } return; }; export function isImageOptionArray(arg) { if (!Array.isArray(arg)) { throw new Error("Image option array is not an array"); } try { arg.forEach(isImageOption); } catch (_a) { throw new Error("Image option array contains invalid image option"); } return; } const _isImageOptionArray = (arg) => { try { isImageOptionArray(arg); return true; } catch (error) { console.error(error); return false; } }; export function assertIsImageChoiceQuestion(arg) { if (typeof arg !== "object" || arg === null) { throw new Error("Image choice question is not an object"); } if (!("category" in arg)) { throw new Error("Image choice question does not have category"); } if (!isCategory(arg.category)) { throw new Error("Image choice question category is not valid"); } if (!("correctAnswer" in arg)) { throw new Error("Image choice question does not have correctAnswer"); } if (!Array.isArray(arg.correctAnswer)) { throw new Error("Image choice question correctAnswer is not an array"); } if (arg.correctAnswer.length === 0) { throw new Error("Image choice question correctAnswer is empty"); } arg.correctAnswer.forEach(isImageOption); if (!("incorrectAnswers" in arg)) { throw new Error("Image choice question does not have incorrectAnswers"); } if (!Array.isArray(arg.incorrectAnswers)) { throw new Error("Image choice question incorrectAnswers is not an array"); } if (arg.incorrectAnswers.length === 0) { throw new Error("Image choice question incorrectAnswers is empty"); } arg.incorrectAnswers.forEach(isImageOptionArray); if (!("question" in arg)) { throw new Error("Image choice question does not have question"); } if (typeof arg.question !== "object" || arg.question === null) { throw new Error("Image choice question question is not an object"); } if (!("text" in arg.question)) { throw new Error("Image choice question question does not have text"); } if (typeof arg.question.text !== "string") { throw new Error("Image choice question question text is not a string"); } if (!("tags" in arg)) { throw new Error("Image choice question does not have tags"); } if (!Array.isArray(arg.tags)) { throw new Error("Image choice question tags is not an array"); } if (arg.tags.some((tag) => typeof tag !== "string")) { throw new Error("Image choice question tags contains non-string"); } if (!("type" in arg)) { throw new Error("Image choice question does not have type"); } if (arg.type !== "image_choice") { throw new Error("Image choice question type is not image_choice"); } /** * TODO: validate difficulty */ /** * TODO: validate regions */ /** * TODO: validate isNiche */ /** * TODO: validate ID */ } export const isImageChoiceQuestion = (arg) => { try { assertIsImageChoiceQuestion(arg); } catch (_a) { return false; } return true; }; export function assertIsTextInputQuestion(arg) { if (typeof arg !== "object" || arg === null) { throw new Error("Text input question is not an object"); } if (!("category" in arg)) { throw new Error("Text input question does not have category"); } if (!isCategory(arg.category)) { throw new Error("Text input question category is not valid"); } if (!("correctAnswer" in arg)) { throw new Error("Text input question does not have correctAnswer"); } if (typeof arg.correctAnswer !== "string") { throw new Error("Text input question correctAnswer is not a string"); } if (!("question" in arg)) { throw new Error("Text input question does not have question"); } if (typeof arg.question !== "object" || arg.question === null) { throw new Error("Text input question question is not an object"); } if (!("text" in arg.question)) { throw new Error("Text input question question does not have text"); } if (typeof arg.question.text !== "string") { throw new Error("Text input question question text is not a string"); } if (!("tags" in arg)) { throw new Error("Text input question does not have tags"); } if (!Array.isArray(arg.tags)) { throw new Error("Text input question tags is not an array"); } if (arg.tags.some((tag) => typeof tag !== "string")) { throw new Error("Text input question tags contains non-string"); } if (!("type" in arg)) { throw new Error("Text input question does not have type"); } if (arg.type !== "text_input") { throw new Error("Text input question type is not text_input"); } } export const isTextInputQuestion = (arg) => { try { assertIsTextInputQuestion(arg); } catch (_a) { return false; } return true; };