@elasticapi/wpengine-typescript-sdk
Version:
Unofficial TypeScript SDK for the WP Engine API
177 lines • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validators = exports.ValidationError = exports.patterns = void 0;
/**
* Common validation patterns
*/
exports.patterns = {
email: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
// WP Engine specific patterns
installId: /^[a-zA-Z0-9-]+$/,
accountId: /^[a-zA-Z0-9-]+$/,
userId: /^[a-zA-Z0-9-]+$/,
roles: /^(owner|full,billing|full|partial,billing|partial)$/,
};
/**
* Validation error class
*/
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
/**
* Input validators
*/
exports.validators = {
/**
* Validate email address
*/
email(email) {
if (!email || !exports.patterns.email.test(email)) {
throw new ValidationError('Invalid email address format');
}
return true;
},
/**
* Validate UUID format
*/
uuid(id) {
if (!id || !exports.patterns.uuid.test(id)) {
throw new ValidationError('Invalid UUID format');
}
return true;
},
/**
* Validate install ID format
*/
installId(id) {
if (!id || !exports.patterns.installId.test(id)) {
throw new ValidationError('Invalid install ID format');
}
return true;
},
/**
* Validate account ID format
*/
accountId(id) {
if (!id || !exports.patterns.accountId.test(id)) {
throw new ValidationError('Invalid account ID format');
}
return true;
},
/**
* Validate user ID format
*/
userId(id) {
if (!id || !exports.patterns.userId.test(id)) {
throw new ValidationError('Invalid user ID format');
}
return true;
},
/**
* Validate user roles
*/
roles(roles) {
if (!roles || !exports.patterns.roles.test(roles)) {
throw new ValidationError('Invalid role format. Must be one of: owner, full,billing, full, partial,billing, partial');
}
return true;
},
/**
* Validate string is not empty
*/
required(value, fieldName) {
if (!value || value.trim().length === 0) {
throw new ValidationError(`${fieldName} is required`);
}
return true;
},
/**
* Validate string length
*/
length(value, fieldName, min, max) {
if (value.length < min || value.length > max) {
throw new ValidationError(`${fieldName} must be between ${min} and ${max} characters`);
}
return true;
},
/**
* Validate URL format
*/
url(url) {
try {
new URL(url);
return true;
}
catch (_a) {
throw new ValidationError('Invalid URL format');
}
},
/**
* Validate credentials
*/
credentials(username, password) {
if (!username || username.trim().length === 0) {
throw new ValidationError('Username is required');
}
if (!password || password.trim().length === 0) {
throw new ValidationError('Password is required');
}
return true;
}
};
/**
* Validation helper functions
*/
exports.validate = {
/**
* Validate user input for creating/updating users
*/
userInput(data) {
if (data.first_name) {
exports.validators.required(data.first_name, 'First name');
exports.validators.length(data.first_name, 'First name', 1, 50);
}
if (data.last_name) {
exports.validators.required(data.last_name, 'Last name');
exports.validators.length(data.last_name, 'Last name', 1, 50);
}
if (data.email) {
exports.validators.email(data.email);
}
if (data.roles) {
exports.validators.roles(data.roles);
}
if (data.install_ids) {
data.install_ids.forEach(id => exports.validators.installId(id));
}
},
/**
* Validate backup input
*/
backupInput(data) {
if (data.description) {
exports.validators.length(data.description, 'Description', 1, 255);
}
if (data.notification_emails) {
data.notification_emails.forEach(email => exports.validators.email(email));
}
},
/**
* Validate domain input
*/
domainInput(data) {
if (data.name) {
exports.validators.required(data.name, 'Domain name');
// Basic domain format validation
if (!/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/.test(data.name)) {
throw new ValidationError('Invalid domain name format');
}
}
}
};
//# sourceMappingURL=validators.js.map