amazon-route-53-dns-zone-file
Version:
Makes DNS Zone File easy. Parses and validates BIND zone files and can be extended for custom features. Functionality is modular. Features are made open for extension and closed for runtime mutation. Written in TypeScript.
53 lines (46 loc) • 1.7 kB
text/typescript
import { ParserInvalidArgumentsError } from '../errors/parsing_error_invalid_arguments';
import {
DirectiveKind,
ErrorKeyKind,
ERROR_KEYS,
} from '../shared/constants_domain_specific';
import { Validation } from './validation_types';
const defaultOnBeforeEmptyInputValidator: Validation.Cb = (parser, builder) => {
if (parser.input.textInput.replace(/\s+/, '') === '') {
throw builder.create({
errorType: ErrorKeyKind.EmptyInput,
instructionType: ErrorKeyKind.Unknown,
});
}
};
const defaultOnAfterNoRecordsValidator: Validation.Cb = (parser, builder) => {
if (
Object.keys(parser.zoneObj).filter(
x => x !== DirectiveKind.Origin && x !== DirectiveKind.TimeToLive
).length === 0
) {
throw builder.create({
errorType: ErrorKeyKind.NoRecords,
instructionType: ErrorKeyKind.Unknown,
});
}
};
const defaultLoopValidator: Validation.Cb = (parser, builder) => {
const errorType = builder.entry.errorType;
if (errorType && ERROR_KEYS.includes(errorType as any)) {
throw builder.create(builder.entry);
}
};
const wrongLengthValidator: Validation.Cb = (parser, builder) => {
if (builder.entry.errorType === ErrorKeyKind.WrongLength) {
const { expected, type, actual } = builder.entry
.error as ParserInvalidArgumentsError;
throw builder.create(builder.entry, { expected, actual, type });
}
};
export const createDefaultValidators = (): Required<Validation.ValidationOptions> => ({
loopValidators: [wrongLengthValidator, defaultLoopValidator],
getErrorText: entry => entry.errorType,
preValidators: [defaultOnBeforeEmptyInputValidator],
postValidators: [defaultOnAfterNoRecordsValidator],
});