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 => ({ loopValidators: [wrongLengthValidator, defaultLoopValidator], getErrorText: entry => entry.errorType, preValidators: [defaultOnBeforeEmptyInputValidator], postValidators: [defaultOnAfterNoRecordsValidator], });