'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hdbTable = exports.hdbDatabase = exports.hdbSchemaTable = exports.commonValidators = exports.schemaRegex = void 0;
exports.checkValidTable = checkValidTable;
exports.validateSchemaExists = validateSchemaExists;
exports.validateTableExists = validateTableExists;
exports.validateSchemaName = validateSchemaName;
const hdbUtils = __importStar(require("../utility/common_utils.js"));
const hdbTerms = __importStar(require("../utility/hdbTerms.js"));
exports.schemaRegex = /^[\x20-\x2E|\x30-\x5F|\x61-\x7E]*$/;
const joi_1 = __importDefault(require("joi"));
exports.commonValidators = {
    schema_format: {
        pattern: exports.schemaRegex,
        message: 'names cannot include backticks or forward slashes',
    },
    schema_length: {
        minimum: 1,
        maximum: 250,
        tooLong: 'cannot exceed 250 characters',
    },
};
// A Joi schema that can be used to validate hdb schemas and tables.
exports.hdbSchemaTable = joi_1.default.alternatives(joi_1.default.string()
    .min(1)
    .max(exports.commonValidators.schema_length.maximum)
    .pattern(exports.schemaRegex)
    .messages({ 'string.pattern.base': '{:#label} ' + exports.commonValidators.schema_format.message }), joi_1.default.number(), joi_1.default.array()).required();
exports.hdbDatabase = joi_1.default.alternatives(joi_1.default.string()
    .min(1)
    .max(exports.commonValidators.schema_length.maximum)
    .pattern(exports.schemaRegex)
    .messages({ 'string.pattern.base': '{:#label} ' + exports.commonValidators.schema_format.message }), joi_1.default.number());
exports.hdbTable = joi_1.default.alternatives(joi_1.default.string()
    .min(1)
    .max(exports.commonValidators.schema_length.maximum)
    .pattern(exports.schemaRegex)
    .messages({ 'string.pattern.base': '{:#label} ' + exports.commonValidators.schema_format.message }), joi_1.default.number()).required();
function checkValidTable(propertyName, value) {
    if (!value)
        return `'${propertyName}' is required`;
    if (typeof value !== 'string')
        return `'${propertyName}' must be a string`;
    if (!value.length)
        return `'${propertyName}' must be at least one character`;
    if (value.length > exports.commonValidators.schema_length.maximum)
        return `'${propertyName}' maximum of 250 characters`;
    if (!exports.schemaRegex.test(value))
        return `'${propertyName}' has illegal characters`;
    return '';
}
function validateSchemaExists(value, helpers) {
    if (!hdbUtils.doesSchemaExist(value)) {
        return helpers.message(`Database '${value}' does not exist`);
    }
    return value;
}
function validateTableExists(value, helpers) {
    const schema = helpers.state.ancestors[0].schema;
    if (!hdbUtils.doesTableExist(schema, value)) {
        return helpers.message(`Table '${value}' does not exist`);
    }
    return value;
}
function validateSchemaName(value, helpers) {
    if (value.toLowerCase() === hdbTerms.SYSTEM_SCHEMA_NAME) {
        return helpers.message(`'subscriptions[${helpers.state.path[1]}]' invalid database name, '${hdbTerms.SYSTEM_SCHEMA_NAME}' name is reserved`);
    }
    return value;
}
//# sourceMappingURL=common_validators.js.map