grab-picture
Version:
grab your images in secconds
66 lines • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateQuery = validateQuery;
exports.validateAccessKey = validateAccessKey;
exports.validateAndNormalizeOptions = validateAndNormalizeOptions;
// Input validation functions for the GrabPic library
const types_1 = require("./types");
// Validates the search query parameter
function validateQuery(query) {
// Check if query is provided and not empty
if (!query || typeof query !== "string") {
throw new types_1.GrabPicError("Query parameter is required and must be a non-empty string", types_1.GrabPicErrorType.MISSING_QUERY, 400);
}
// Check if query is not just whitespace
if (query.trim().length === 0) {
throw new types_1.GrabPicError("Query parameter cannot be empty or contain only whitespace", types_1.GrabPicErrorType.MISSING_QUERY, 400);
}
// Check query length (reasonable limits)
if (query.length > 200) {
throw new types_1.GrabPicError("Query parameter is too long (maximum 200 characters)", types_1.GrabPicErrorType.MISSING_QUERY, 400);
}
}
// Validates the Unsplash access key parameter
function validateAccessKey(accessKey) {
// Check if access key is provided
if (!accessKey || typeof accessKey !== "string") {
throw new types_1.GrabPicError("Unsplash access key is required and must be a string", types_1.GrabPicErrorType.MISSING_ACCESS_KEY, 400);
}
// Check if access key is not just whitespace
if (accessKey.trim().length === 0) {
throw new types_1.GrabPicError("Unsplash access key cannot be empty or contain only whitespace", types_1.GrabPicErrorType.MISSING_ACCESS_KEY, 400);
}
// Basic format validation (Unsplash access keys are typically 64 characters)
if (accessKey.length < 20) {
throw new types_1.GrabPicError("Unsplash access key appears to be invalid (too short)", types_1.GrabPicErrorType.INVALID_ACCESS_KEY, 400);
}
}
// Validates the options parameter and returns validated options with defaults
function validateAndNormalizeOptions(options = {}) {
const { count = 5, orientation, size = "regular" } = options;
// Validate count parameter
if (typeof count !== "number" || !Number.isInteger(count)) {
throw new types_1.GrabPicError("Count parameter must be an integer", types_1.GrabPicErrorType.INVALID_COUNT, 400);
}
// Check count range (Unsplash API limits)
if (count < 1 || count > 30) {
throw new types_1.GrabPicError("Count parameter must be between 1 and 30 (Unsplash API limitation)", types_1.GrabPicErrorType.INVALID_COUNT, 400);
}
// Validate orientation if provided
const validOrientations = ["landscape", "portrait", "squarish"];
if (orientation && !validOrientations.includes(orientation)) {
throw new types_1.GrabPicError(`Invalid orientation "${orientation}". Must be one of: ${validOrientations.join(", ")}`, types_1.GrabPicErrorType.INVALID_COUNT, 400);
}
// Validate size parameter
const validSizes = ["raw", "full", "regular", "small", "thumb"];
if (!validSizes.includes(size)) {
throw new types_1.GrabPicError(`Invalid size "${size}". Must be one of: ${validSizes.join(", ")}`, types_1.GrabPicErrorType.INVALID_COUNT, 400);
}
// Return validated and normalized options
return {
count,
orientation: orientation || "landscape", // Default to landscape if not provided
size,
};
}
//# sourceMappingURL=validators.js.map