ajt-validator
Version:
Validation library for JavaScript and TypeScript
41 lines (40 loc) • 1.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiKeyValidator = void 0;
const base_1 = require("../base");
class ApiKeyValidator extends base_1.BaseValidator {
constructor(options = {}) {
super();
this.options = Object.assign({ minLength: 16, maxLength: 64, pattern: /^[a-zA-Z0-9_-]+$/, prefixRequired: '', allowedPrefixes: [] }, options);
}
validate(apiKey) {
if (!apiKey) {
return this.createError('APIKEY_REQUIRED', 'API key is required');
}
// Length validation
if (apiKey.length < this.options.minLength) {
return this.createError('APIKEY_TOO_SHORT', `API key must be at least ${this.options.minLength} characters`);
}
if (apiKey.length > this.options.maxLength) {
return this.createError('APIKEY_TOO_LONG', `API key must not exceed ${this.options.maxLength} characters`);
}
// Format validation
if (!this.options.pattern.test(apiKey)) {
return this.createError('INVALID_APIKEY_FORMAT', 'API key format is invalid');
}
// Prefix validation
if (this.options.prefixRequired && !apiKey.startsWith(this.options.prefixRequired)) {
return this.createError('INVALID_APIKEY_PREFIX', `API key must start with ${this.options.prefixRequired}`);
}
// Check allowed prefixes if specified
if (this.options.allowedPrefixes &&
this.options.allowedPrefixes.length > 0) {
const hasValidPrefix = this.options.allowedPrefixes.some(prefix => apiKey.startsWith(prefix));
if (!hasValidPrefix) {
return this.createError('INVALID_APIKEY_PREFIX', 'API key prefix is not in the list of allowed prefixes');
}
}
return this.createSuccess(apiKey.trim());
}
}
exports.ApiKeyValidator = ApiKeyValidator;