All files validator.js

100% Statements 137/137
100% Branches 31/31
100% Functions 7/7
100% Lines 137/137

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 6x 6x 24x 24x 30x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 6x 6x 23x 23x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 22x 26x 6x 6x 6x 6x 20x 20x 26x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 6x 6x 17x 17x 23x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 6x 6x 17x 17x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 23x 29x 8x 8x 8x 8x 21x 21x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 24x 30x 7x 7x 23x 23x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
"use strict";
 
/**
 * Validate whether moderation is enabled.
 *
 * @param {boolean} enableModeration - Moderation setting to validate.
 * @returns {boolean} The validated setting.
 * @throws {TypeError} When the setting is not a boolean.
 */
function validateEnableModeration(enableModeration) {
  if (typeof enableModeration !== "boolean") {
    throw new TypeError("enableModeration must be a boolean");
  }
 
  return enableModeration;
}
 
/**
 * Validate whether model reply logging is enabled.
 *
 * @param {boolean} enableMessageLogging - Reply logging setting to validate.
 * @returns {boolean} The validated setting.
 * @throws {TypeError} When the setting is not a boolean.
 */
function validateEnableMessageLogging(enableMessageLogging) {
  if (typeof enableMessageLogging !== "boolean") {
    throw new TypeError("enableMessageLogging must be a boolean");
  }
 
  return enableMessageLogging;
}
 
/**
 * Validate the minimum delay between player messages.
 * The delay must be a finite, non-negative number of seconds.
 *
 * @param {number} coolDownInSeconds - Cooldown duration to validate.
 * @returns {number} The validated cooldown duration.
 * @throws {RangeError} When the duration is not finite or is negative.
 */
function validateCoolDownInSeconds(coolDownInSeconds) {
  if (
    typeof coolDownInSeconds !== "number" ||
    !Number.isFinite(coolDownInSeconds) ||
    coolDownInSeconds < 0
  ) {
    throw new RangeError(
      "coolDownInSeconds must be a non-negative finite number",
    );
  }
 
  return coolDownInSeconds;
}
 
/**
 * Validate the message returned when a reply cannot be provided.
 *
 * @param {string} fallbackMessage - Fallback message to validate.
 * @returns {string} The validated fallback message.
 * @throws {TypeError} When the message is not a non-empty string.
 */
function validateFallbackMessage(fallbackMessage) {
  if (typeof fallbackMessage !== "string" || fallbackMessage.length === 0) {
    throw new TypeError("fallbackMessage must be a non-empty string");
  }
 
  return fallbackMessage;
}
 
/**
 * Validate whether security instructions are enabled.
 *
 * @param {boolean} enableSecurityInstructions - Security instruction setting to validate.
 * @returns {boolean} The validated setting.
 * @throws {TypeError} When the setting is not a boolean.
 */
function validateEnableSecurityInstructions(enableSecurityInstructions) {
  if (typeof enableSecurityInstructions !== "boolean") {
    throw new TypeError("enableSecurityInstructions must be a boolean");
  }
 
  return enableSecurityInstructions;
}
 
/**
 * Validate the minimum confidence score used to flag jailbreak attempts.
 * The score must be a finite number in the inclusive range from 0 to 1.
 *
 * @param {number} minimumJailbreakConfidenceScore - Confidence score to validate.
 * @returns {number} The validated confidence score.
 * @throws {RangeError} When the score is not a finite number from 0 to 1.
 */
function validateJailbreakConfidenceScore(minimumJailbreakConfidenceScore) {
  if (
    typeof minimumJailbreakConfidenceScore !== "number" ||
    !Number.isFinite(minimumJailbreakConfidenceScore) ||
    minimumJailbreakConfidenceScore < 0 ||
    minimumJailbreakConfidenceScore > 1
  ) {
    throw new RangeError(
      "minimumJailbreakConfidenceScore must be between 0 and 1",
    );
  }
 
  return minimumJailbreakConfidenceScore;
}
 
/**
 * Validate the minimum confidence score accepted for model replies.
 * The score must be a finite number in the inclusive range from 0 to 1.
 *
 * @param {number} minimumReplyConfidenceScore - Confidence score to validate.
 * @returns {number} The validated confidence score.
 * @throws {RangeError} When the score is not a finite number from 0 to 1.
 */
function validateReplyConfidenceScore(minimumReplyConfidenceScore) {
  if (
    typeof minimumReplyConfidenceScore !== "number" ||
    !Number.isFinite(minimumReplyConfidenceScore) ||
    minimumReplyConfidenceScore < 0 ||
    minimumReplyConfidenceScore > 1
  ) {
    throw new RangeError("minimumReplyConfidenceScore must be between 0 and 1");
  }
 
  return minimumReplyConfidenceScore;
}
 
export {
  validateCoolDownInSeconds,
  validateEnableMessageLogging,
  validateEnableModeration,
  validateEnableSecurityInstructions,
  validateFallbackMessage,
  validateJailbreakConfidenceScore,
  validateReplyConfidenceScore,
};