47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
import { ValidationError } from "../ValidationError.js";
|
|
|
|
export class DnsTool {
|
|
static fields = [];
|
|
|
|
constructor(text) {
|
|
this.text = text;
|
|
}
|
|
|
|
getKeyValues() {
|
|
throw new Error("Unimplemented");
|
|
}
|
|
|
|
validate() {
|
|
const values = this.getKeyValues();
|
|
|
|
// Validate field order
|
|
for (const field of this.constructor.fields) {
|
|
const valueIdx = values.findIndex(v => v.key === field.key);
|
|
|
|
if (field.isRequired && valueIdx === -1) {
|
|
throw new ValidationError(`Field "${field.key}" is required`);
|
|
}
|
|
|
|
if (field.requiredIndex && field.requiredIndex !== valueIdx) {
|
|
if (field.requiredIndex === 0) throw new ValidationError(`Field "${field.key}" must come first`);
|
|
if (field.afterFieldName) throw new ValidationError(`Field "${field.key}" must come after "${field.afterFieldName}"`);
|
|
throw new ValidationError(`Field "${field.key}" must be at position ${field.requiredIndex + 1}`);
|
|
}
|
|
}
|
|
|
|
// Validate field values
|
|
for (let i = 0; i < values.length; i++) {
|
|
const input = values[i];
|
|
const field = this.constructor.fields.find(f => f.key === input.key);
|
|
|
|
if (!field) {
|
|
throw new ValidationError(`Unknown field: ${input.key}`)
|
|
}
|
|
|
|
field.validate(input.value);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|