"use strict";
/*
 * @adonisjs/validator
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.alphaNum = void 0;
const helpers_1 = require("../../Validator/helpers");
const RULE_NAME = 'alphaNum';
const DEFAULT_MESSAGE = 'alphaNum validation failed';
/**
 * Validation signature for the "alphaNum" regex. Non-string values are
 * ignored.
 */
exports.alphaNum = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, ['string'], ([options]) => {
        let charactersMatch = 'a-zA-Z0-9';
        /**
         * Allow only alphaNum characters
         */
        if (!options || !options.allow || !Array.isArray(options.allow)) {
            return {
                compiledOptions: {
                    pattern: `^[${charactersMatch}]+$`,
                },
            };
        }
        /**
         * Allow spaces
         */
        if (options.allow.includes('space')) {
            charactersMatch += '\\s';
        }
        /**
         * Allow dash charcater
         */
        if (options.allow.includes('dash')) {
            charactersMatch += '-';
        }
        /**
         * Allow underscores
         */
        if (options.allow.includes('underscore')) {
            charactersMatch += '_';
        }
        return {
            compiledOptions: {
                pattern: `^[${charactersMatch}]+$`,
            },
        };
    }),
    validate(value, { pattern }, { errorReporter, arrayExpressionPointer, pointer }) {
        /**
         * Ignore non-string values. The user must apply string rule
         * to validate string
         */
        if (typeof value !== 'string') {
            return;
        }
        if (!new RegExp(pattern).test(value)) {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer);
        }
    },
};
