UNPKG

aurelia-validatejs

Version:

This is a plugin that will allow using validate.js in your Aurelia application for expressive validation.

368 lines (272 loc) 11.6 kB
'use strict'; System.register(['aurelia-metadata', 'aurelia-validation', 'validate.js'], function (_export, _context) { "use strict"; var metadata, ValidationError, ValidatorInterface, validate, metadataKey, ValidationRule, ValidationRules, Validator; function cleanResult(data) { var result = {}; for (var prop in data) { if (data.hasOwnProperty(prop)) { result = { propertyName: prop, message: data[prop][0] }; } } return result; } _export('cleanResult', cleanResult); function base(targetOrConfig, key, descriptor, rule) { if (key) { var target = targetOrConfig; targetOrConfig = null; return addRule(target, key, descriptor, targetOrConfig, rule); } return function (t, k, d) { return addRule(t, k, d, targetOrConfig, rule); }; } _export('base', base); function addRule(target, key, descriptor, targetOrConfig, rule) { var rules = metadata.getOrCreateOwn(metadataKey, ValidationRules, target); if (targetOrConfig === null || targetOrConfig === undefined) { targetOrConfig = true; } rules.addRule(key, rule(targetOrConfig)); if (descriptor) { descriptor.configurable = true; } } _export('addRule', addRule); function length(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.lengthRule); } _export('length', length); function presence(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.presence, true); } _export('presence', presence); function required(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.presence, true); } _export('required', required); function date(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.date); } _export('date', date); function datetime(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.datetime); } _export('datetime', datetime); function email(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.email); } _export('email', email); function equality(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.equality); } _export('equality', equality); function exclusion(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.exclusion); } _export('exclusion', exclusion); function inclusion(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.inclusion); } _export('inclusion', inclusion); function format(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.format); } _export('format', format); function url(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.url); } _export('url', url); function numericality(targetOrConfig, key, descriptor) { return base(targetOrConfig, key, descriptor, ValidationRule.numericality); } _export('numericality', numericality); function configure(config) { config.container.registerInstance(ValidatorInterface, new Validator()); } _export('configure', configure); return { setters: [function (_aureliaMetadata) { metadata = _aureliaMetadata.metadata; }, function (_aureliaValidation) { ValidationError = _aureliaValidation.ValidationError; ValidatorInterface = _aureliaValidation.Validator; }, function (_validateJs) { validate = _validateJs.default; }], execute: function () { _export('metadataKey', metadataKey = 'aurelia-validatejs:rules'); _export('metadataKey', metadataKey); _export('ValidationRule', ValidationRule = function () { function ValidationRule(name, config) { this.name = ''; this.name = name; this.config = config; } ValidationRule.date = function date() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('date', config); }; ValidationRule.datetime = function datetime() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('datetime', config); }; ValidationRule.email = function email() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('email', config); }; ValidationRule.equality = function equality(config) { return new ValidationRule('equality', config); }; ValidationRule.exclusion = function exclusion(config) { return new ValidationRule('exclusion', config); }; ValidationRule.format = function format(config) { return new ValidationRule('format', config); }; ValidationRule.inclusion = function inclusion(config) { return new ValidationRule('inclusion', config); }; ValidationRule.lengthRule = function lengthRule(config) { return new ValidationRule('length', config); }; ValidationRule.numericality = function numericality() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('numericality', config); }; ValidationRule.presence = function presence() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('presence', config); }; ValidationRule.url = function url() { var config = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0]; return new ValidationRule('url', config); }; return ValidationRule; }()); _export('ValidationRule', ValidationRule); _export('ValidationRules', ValidationRules = function () { function ValidationRules() { this.rules = []; } ValidationRules.ensure = function ensure(prop) { var rules = new ValidationRules(); return rules.ensure(prop); }; ValidationRules.prototype.on = function on(target) { if (target instanceof Function) { target = target.prototype; } metadata.define(metadataKey, this, target); return this; }; ValidationRules.prototype.decorate = function decorate() { throw new Error('not implemented'); }; ValidationRules.prototype.addRule = function addRule(key, rule) { this.rules.push({ key: key, rule: rule }); }; ValidationRules.prototype.ensure = function ensure(prop) { this.currentProperty = prop; return this; }; ValidationRules.prototype.length = function length(configuration) { this.addRule(this.currentProperty, ValidationRule.lengthRule(configuration)); return this; }; ValidationRules.prototype.presence = function presence(configuration) { this.addRule(this.currentProperty, ValidationRule.presence(configuration)); return this; }; ValidationRules.prototype.required = function required(configuration) { this.addRule(this.currentProperty, ValidationRule.presence(configuration)); return this; }; ValidationRules.prototype.numericality = function numericality(configuration) { this.addRule(this.currentProperty, ValidationRule.numericality(configuration)); return this; }; ValidationRules.prototype.date = function date(configuration) { this.addRule(this.currentProperty, ValidationRule.date(configuration)); return this; }; ValidationRules.prototype.datetime = function datetime(configuration) { this.addRule(this.currentProperty, ValidationRule.datetime(configuration)); return this; }; ValidationRules.prototype.email = function email(configuration) { this.addRule(this.currentProperty, ValidationRule.email(configuration)); return this; }; ValidationRules.prototype.equality = function equality(configuration) { this.addRule(this.currentProperty, ValidationRule.equality(configuration)); return this; }; ValidationRules.prototype.format = function format(configuration) { this.addRule(this.currentProperty, ValidationRule.format(configuration)); return this; }; ValidationRules.prototype.inclusion = function inclusion(configuration) { this.addRule(this.currentProperty, ValidationRule.inclusion(configuration)); return this; }; ValidationRules.prototype.exclusion = function exclusion(configuration) { this.addRule(this.currentProperty, ValidationRule.exclusion(configuration)); return this; }; ValidationRules.prototype.url = function url(configuration) { this.addRule(this.currentProperty, ValidationRule.url(configuration)); return this; }; return ValidationRules; }()); _export('ValidationRules', ValidationRules); _export('Validator', Validator = function () { function Validator() { } Validator.prototype._validate = function _validate(object) { var propertyName = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; var rules = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; var errors = []; if (!rules) { rules = metadata.get(metadataKey, object); } if (!rules) { return errors; } rules = rules.rules; for (var i = 0, ii = rules.length; i < ii; i++) { var _propertyName, _validator; var ruleInfo = rules[i]; if (propertyName !== null && ruleInfo.key !== propertyName) { continue; } var _ruleInfo$rule = ruleInfo.rule; var name = _ruleInfo$rule.name; var config = _ruleInfo$rule.config; var validator = (_validator = {}, _validator[propertyName] = (_propertyName = {}, _propertyName[name] = config, _propertyName), _validator); var result = validate(object, validator); if (result) { errors.push(new ValidationError(ruleInfo.rule, result[propertyName][0], object, propertyName)); } } return errors; }; Validator.prototype.validateProperty = function validateProperty(object, propertyName) { var rules = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; return this._validate(object, propertyName, rules); }; Validator.prototype.validateObject = function validateObject(object) { var rules = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; return this._validate(object, null, rules); }; return Validator; }()); _export('Validator', Validator); } }; });