Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
const md5 = require('md5');

class ValidationError extends Error {
    constructor(msg) {
        super(msg);
        this.name = "validationError";
    }
}

const validateAlertType = (code) => {
    if ((typeof code) !== 'string') {
        throw new ValidationError('Alert type short code must be a string');
    }
    if (code.length > 8) {
        throw new ValidationError('Alert type short code too long. Max 8 chars');
    }
    if (code.length < 2) {
        throw new ValidationError('Alert type short code too short. Min 2 chars');
    }
    if (!code.match(/^[0-9A-Z]*$/)) {
        throw new ValidationError('Alert type short code invalid.  Must be A-Z 0-9')
    }
}

// Note, this now returns the value, which must be less than 64 chars.  The ident can now be arbitrarily long.
const validateAlertIdent = (ident) => {
    if ((typeof ident) !== 'string') {
        throw new ValidationError('Alert ident must be a string');
    }
    // if (ident.length > 64) {
    //    throw new ValidationError('Alert ident too long. Max 64 chars');
    // }

    if (ident.length < 2) {
        throw new ValidationError('Alert ident too short. Min 2 chars');
    }
    if (!ident.match(/^[0-9A-Za-z\_\-\+\.\,]*$/)) {
        throw new ValidationError('Alert ident invalid.  Must be A-Z, a-z, 0-9, +, -, ., ,,');
    }

    if (ident.length > 64) {
        return ident.substring(0,46) + "(" + md5(ident).substring(0,16) + ")";
    } else {
        return ident;
    }
}

const validateStatus = (status) => {
    let valid = ['OK', 'WARNING', 'CRITICAL'];
    if (valid.indexOf(status) === -1) {
        throw new ValidationError('Status invalid.  Must be one of: ' + valid.join(", "));
    }
}

const validateMeta = (meta) => {
    if (meta !== null) {
        // Ensure it's an object
        if (typeof meta !== 'object') {
            throw new ValidationError('Meta data variable "meta" must be an object');
        }
    }
}

const validateExpiry = (expiry) => {
    if (expiry !== null) {
        // Ensure it's a number
        if (typeof expiry !== 'number') {
            throw new ValidationError('Expiry variable "expiry" must be a whole number');
        }
        if (expiry < 0) {
            throw new ValidationError('Expiry cannot be a negative number')
        }
    }
}

const validateRepo = (repo) => {
    if (repo !== null) {
        // Ensure it's an object
        if (typeof repo !== 'object') {
            throw new ValidationError('Repository variable "repo" must be an object');
        }

        // Ensure keys
        if (!('type' in repo) || ((typeof repo.type) !== 'string')) {
            throw new ValidationError('Repository variable "repo" object must contain a string at key "type"');
        }
        if (!('user' in repo) || ((typeof repo.user) !== 'string')) {
            throw new ValidationError('Repository variable "repo" object must contain a string at key "user"');
        }
        if (!('name' in repo) || ((typeof repo.name) !== 'string')) {
            throw new ValidationError('Repository variable "repo" object must contain a string at key "name"');
        }
    }
}

module.exports = {
    validateAlertType,
    validateAlertIdent,
    validateStatus,
    validateMeta,
    validateExpiry,
    validateRepo,
}