"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.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.string = void 0;
const escape_1 = __importDefault(require("validator/lib/escape"));
const helpers_1 = require("../../Validator/helpers");
const DEFAULT_MESSAGE = 'string validation failed';
const RULE_NAME = 'string';
/**
 * Ensure value is a valid string
 * @type {SyncValidation}
 */
exports.string = {
    compile: (0, helpers_1.wrapCompile)(RULE_NAME, [], ([options]) => {
        return {
            compiledOptions: {
                escape: !!(options && options.escape),
                trim: !!(options && options.trim),
            },
        };
    }),
    validate(value, compiledOptions, { pointer, errorReporter, arrayExpressionPointer, mutate }) {
        if (typeof value !== 'string') {
            errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer);
            return;
        }
        let mutated = false;
        /**
         * Escape string
         */
        if (compiledOptions.escape) {
            mutated = true;
            value = (0, escape_1.default)(value);
        }
        /**
         * Trim whitespaces
         */
        if (compiledOptions.trim) {
            mutated = true;
            value = value.trim();
        }
        mutated && mutate(value);
    },
};
