/**
 * @param {mongoose.Schema} schema
 * @param {?Object=} options
 */
module.exports = exports = function constantPlugin(schema, options) {
    var validSchemaTypes = ['String', 'Number', 'Date', 'ObjectID'];

    function validateSchemaType(path, schemaType) {
        if (!~validSchemaTypes.indexOf(schemaType.instance)) {
            throw new Error('Cannot use constant in path ' + path + '.\nConstant can only be used with: ' +
            validSchemaTypes.join(', '));
        }
    }

    /* Register validations. */
    schema.eachPath(function (path, schemaType) {
        if (!schemaType.options.constant)
            return;
        validateSchemaType(path, schemaType);

        (function (path) {
            schemaType.validators.push(
                [
                    function () {
                        if (this.isNew || !this.isSelected(path))
                            return;
                        return !this.isModified(path);
                    },
                    constantPlugin.ErrorMessages.ERRRESETCONST,
                    'constant plugin'
                ]
            );
        })(path);
    });
};


exports.ErrorMessages = {
    ERRRESETCONST: 'Constant `{PATH}` cannot be changed.'
};

var util = require('util');


/**
 * @param {mongoose.Schema} schema
 * @param {?Object=} options
 */
module.exports = exports = function constantPlugin(schema, options) {
    options = options || {};
    options.ValidSchemaTypes = options.ValidSchemaTypes || ['String', 'Number', 'Date', 'ObjectID'];
    options.ErrorType = options.ErrorType || constantPlugin.ERROR_TYPE;

    function validateSchemaType(path, schemaType) {
        if (!~options.ValidSchemaTypes.indexOf(schemaType.instance))
            throw new Error(util.format(constantPlugin.ErrorMessages.ERRINVALIDTYPE, path, schemaType.instance,
                options.ValidSchemaTypes.join(', ')));
    }

    /* Register validations. */
    schema.eachPath(function (path, schemaType) {
        if (!schemaType.options.constant)
            return;
        validateSchemaType(path, schemaType);

        (function (path) {
            schemaType.validators.push([
                function () {
                    if (this.isNew || !this.isSelected(path))
                        return;
                    return !this.isModified(path);
                },
                constantPlugin.ErrorMessages.ERRCONSTRESET,
                options.ErrorType
            ]);
        })(path);
    });
};


exports.ErrorMessages = {
    ERRCONSTRESET: 'Constant `{PATH}` cannot be changed.',
    ERRINVALIDTYPE: 'Cannot use constant in path `%s` with type `%s`.\nConstant can only be used with: %s'
};


exports.ERROR_TYPE = 'constant plugin';

Make valid schema types and validator error type configurable
Make valid schema types and validator error type configurable
