import { validations } from '../../core/built-in-options/index.js';
import Validator from './validator.js';
const { Validations: { exists, needsValue, MINLENGTH, MAXLENGTH, MAXBYTES, MINBYTES }, } = validations;
export default class ValidationValidator extends Validator {
    constructor() {
        super('validation', ['name']);
    }
    validate(jdlValidation) {
        super.validate(jdlValidation);
        checkForInvalidName(jdlValidation);
        checkForRequiredValue(jdlValidation);
        if ([MINLENGTH, MAXLENGTH, MAXBYTES, MINBYTES].includes(jdlValidation.name)) {
            checkForInvalidNumericValue(jdlValidation);
        }
    }
}
function checkForInvalidName(jdlValidation) {
    if (!exists(jdlValidation.name)) {
        throw new Error(`The validation ${jdlValidation.name} doesn't exist.`);
    }
}
function checkForRequiredValue(jdlValidation) {
    if (jdlValidation.value == null && needsValue(jdlValidation.name)) {
        throw new Error(`The validation ${jdlValidation.name} requires a value.`);
    }
}
function checkForInvalidNumericValue(jdlValidation) {
    if (jdlValidation.value.toString().includes('.')) {
        throw new Error(`Decimal values are forbidden for the ${jdlValidation.name} validation.`);
    }
}
