pdf-tailwind-forms
Version:
TypeScript Node.js library for generating PDFs with AcroForms and TailwindCSS support
91 lines • 3.03 kB
JavaScript
export class PDFValidator {
constructor() {
this.errors = [];
}
validate(config) {
this.errors = [];
this.validateContent(config.content);
this.validateFields(config.fields || []);
this.validatePDFOptions(config.pdfOptions);
return {
valid: this.errors.length === 0,
errors: [...this.errors],
};
}
validateContent(content) {
if (!content) {
this.errors.push("Content is required");
return;
}
if (content.trim().length === 0) {
this.errors.push("Content cannot be empty");
}
const dangerousTags = ["<script", "<iframe", "<object", "<embed"];
for (const tag of dangerousTags) {
if (content.toLowerCase().includes(tag)) {
this.errors.push(`Dangerous HTML tag detected: ${tag}`);
}
}
}
validateFields(fields) {
const fieldNames = new Set();
for (const field of fields) {
if (fieldNames.has(field.name)) {
this.errors.push(`Duplicate field name: ${field.name}`);
}
fieldNames.add(field.name);
this.validateField(field);
}
}
validateField(field) {
if (!field.name) {
this.errors.push("Field must have a name");
}
if (!field.selector && !field.position) {
this.errors.push(`Field "${field.name}" must have either selector or position`);
}
switch (field.type) {
case "text":
this.validateTextField(field);
break;
case "radio":
this.validateRadioField(field);
break;
case "dropdown":
this.validateDropdownField(field);
break;
case "button":
this.validateButtonField(field);
break;
}
}
validateTextField(field) {
if (field.maxLength && field.maxLength <= 0) {
this.errors.push(`Invalid maxLength for field "${field.name}"`);
}
}
validateRadioField(field) {
if (!field.options || field.options.length === 0) {
this.errors.push(`Radio field "${field.name}" must have options`);
}
}
validateDropdownField(field) {
if (!field.options || field.options.length === 0) {
this.errors.push(`Dropdown field "${field.name}" must have options`);
}
}
validateButtonField(field) {
if (!field.label) {
this.errors.push(`Button field "${field.name}" must have a label`);
}
}
validatePDFOptions(options) {
if (!options)
return;
const validFormats = ["A3", "A4", "A5", "Legal", "Letter", "Tabloid"];
if (options.format && !validFormats.includes(options.format)) {
this.errors.push(`Invalid PDF format: ${options.format}`);
}
}
}
//# sourceMappingURL=validator.js.map