@trivia-api/models
Version:
Models for The Trivia API.
239 lines (238 loc) • 8.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTextInputQuestion = exports.assertIsTextInputQuestion = exports.isImageChoiceQuestion = exports.assertIsImageChoiceQuestion = exports.isImageOptionArray = exports.isTextChoiceQuestion = exports.assertIsV2Question = exports.isV2Question = void 0;
const category_1 = require("./category");
/**
* Type guard to return whether a question is a valid v2 question
* @param input
*/
const isV2Question = (arg) => {
return ((0, exports.isTextChoiceQuestion)(arg) ||
(0, exports.isImageChoiceQuestion)(arg) ||
(0, exports.isTextInputQuestion)(arg));
};
exports.isV2Question = isV2Question;
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 (!(0, exports.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");
}
}
exports.assertIsV2Question = assertIsV2Question;
const isTextChoiceQuestion = (arg) => {
if (typeof arg === "object" && arg !== null) {
if (["category", "correctAnswer"].every((key) => Object.keys(arg).includes(key))) {
if ((0, category_1.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;
};
exports.isTextChoiceQuestion = isTextChoiceQuestion;
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;
};
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;
}
exports.isImageOptionArray = isImageOptionArray;
const _isImageOptionArray = (arg) => {
try {
isImageOptionArray(arg);
return true;
}
catch (error) {
console.error(error);
return false;
}
};
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 (!(0, category_1.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
*/
}
exports.assertIsImageChoiceQuestion = assertIsImageChoiceQuestion;
const isImageChoiceQuestion = (arg) => {
try {
assertIsImageChoiceQuestion(arg);
}
catch (_a) {
return false;
}
return true;
};
exports.isImageChoiceQuestion = isImageChoiceQuestion;
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 (!(0, category_1.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");
}
}
exports.assertIsTextInputQuestion = assertIsTextInputQuestion;
const isTextInputQuestion = (arg) => {
try {
assertIsTextInputQuestion(arg);
}
catch (_a) {
return false;
}
return true;
};
exports.isTextInputQuestion = isTextInputQuestion;