mcard-js
Version:
MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers
51 lines • 1.89 kB
JavaScript
import { ValidationError } from './BaseValidator';
import { TextValidator } from './TextValidator';
import { BinaryValidator } from './BinaryValidator';
export class ValidationRegistry {
validators;
constructor() {
this.validators = [
new TextValidator(),
new BinaryValidator(),
];
}
/**
* Validate content using appropriate validator.
*
* @param content The content to validate
* @param mimeType The detected MIME type
* @throws ValidationError If content is invalid
*/
validate(content, mimeType) {
if (!content || (content instanceof Uint8Array && content.length === 0)) {
throw new ValidationError("Empty content");
}
if (typeof content === 'string' && !content) {
throw new ValidationError("Empty content");
}
// Find appropriate validator
for (const validator of this.validators) {
if (validator.canValidate(mimeType)) {
validator.validate(content, mimeType);
return;
}
}
// If no specific validator found, do basic validation
this.basicValidation(content);
}
basicValidation(content) {
/** Basic validation for unknown content types. */
if (content instanceof Uint8Array) {
// Check if empty byte array (already checked above mostly, but trimmed check?)
// Python: if isinstance(content, bytes) and not content.strip():
// Byte strip is stripping whitespace?
// In JS simple check:
if (content.length === 0) {
throw new ValidationError("Invalid content: empty byte array");
}
}
}
}
// Global registry instance
export const validationRegistry = new ValidationRegistry();
//# sourceMappingURL=ValidationRegistry.js.map