ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
134 lines • 8.35 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_validation_1 = require("aurelia-validation");
var FormSchemaRulesProvider = (function () {
function FormSchemaRulesProvider() {
this.validationRules = null;
this.registerCustomRules();
}
FormSchemaRulesProvider.prototype.generateValidationRules = function (formSchema, showingKeyColumn) {
var rules = this.createRules(formSchema, showingKeyColumn);
return this.validationRules = rules ? rules.rules : null;
};
FormSchemaRulesProvider.prototype.createRules = function (formSchema, showingKeyColumn) {
var ensureApi = aurelia_validation_1.ValidationRules;
for (var _i = 0, _a = formSchema.properties; _i < _a.length; _i++) {
var property = _a[_i];
if (this.requiresValidation(property, formSchema, showingKeyColumn)) {
var ruleApi = ensureApi.ensure(property.name)
.displayName(property.description);
if (property.required) {
ruleApi = ruleApi.satisfiesRule("required");
}
if (property.minLength !== undefined) {
ruleApi = ruleApi.satisfiesRule("minLength", property.minLength)
.withMessage("${$displayName} must contain at least " + property.minLength + " characters");
}
if (property.maxLength !== undefined) {
ruleApi = ruleApi.satisfiesRule("maxLength", property.maxLength)
.withMessage("${$displayName} must contain no more than " + property.maxLength + " characters");
}
if (property.range !== undefined) {
ruleApi = ruleApi.satisfiesRule("range", property.range.Minimum, property.range.Maximum);
}
else {
if (property.minimum !== undefined) {
ruleApi = ruleApi.satisfiesRule("minimum", property.minimum);
}
if (property.maximum !== undefined) {
ruleApi = ruleApi.satisfiesRule("maximum", property.maximum);
}
if (property.exclusiveMinimum !== undefined) {
ruleApi = ruleApi.satisfiesRule("exclusiveMinimum", property.exclusiveMinimum);
}
if (property.exclusiveMaximum !== undefined) {
ruleApi = ruleApi.satisfiesRule("exclusiveMaximum", property.exclusiveMaximum);
}
}
if (property.matches !== undefined) {
ruleApi = ruleApi.satisfiesRule("matches", property.matches);
}
if (property.format !== undefined) {
switch (property.format) {
case "email":
ruleApi = ruleApi.satisfiesRule("email");
break;
case "currency":
ruleApi = ruleApi.satisfiesRule("currency");
break;
case "date":
break;
}
}
if (property.passwordRequirements !== undefined) {
ruleApi = ruleApi.satisfiesRule("matches", new RegExp("\\d", "u"))
.withMessage("${$displayName} must contain at least one numeric digit");
ruleApi = ruleApi.satisfiesRule("matches", new RegExp("[^A-Za-z0-9]", "u"))
.withMessage("${$displayName} must contain at least one non-alphanumeric character");
ruleApi = ruleApi.satisfiesRule("matches", new RegExp("[A-Z]", "u"))
.withMessage("${$displayName} must contain at least one capital letter");
ruleApi = ruleApi.satisfiesRule("minLength", property.passwordRequirements.MinLength)
.withMessage("${$displayName} must contain at least " + property.passwordRequirements.MinLength + " characters");
ruleApi = ruleApi.satisfiesRule("maxLength", property.passwordRequirements.MaxLength)
.withMessage("${$displayName} must contain no more than " + property.passwordRequirements.MaxLength + " characters");
}
ensureApi = ruleApi;
}
}
return ensureApi;
};
FormSchemaRulesProvider.prototype.registerCustomRules = function () {
aurelia_validation_1.ValidationRules.customRule("range", function (value, object, min, max) {
return value === null || value === undefined ||
(value >= min && value <= max);
}, "${$displayName} must be between ${$config.min} and ${$config.max}", function (min, max) { return ({ min: min, max: max }); });
aurelia_validation_1.ValidationRules.customRule("minimum", function (value, object, min) {
return value === null || value === undefined || value >= min;
}, "${$displayName} must be ${$config.min} or greater", function (min) { return ({ min: min }); });
aurelia_validation_1.ValidationRules.customRule("maximum", function (value, object, max) {
return value === null || value === undefined || value <= max;
}, "${$displayName} must be ${$config.max} or less", function (max) { return ({ max: max }); });
aurelia_validation_1.ValidationRules.customRule("exclusiveMinimum", function (value, object, min) {
return value === null || value === undefined || value > min;
}, "${$displayName} must be greater than ${$config.min}", function (min) { return ({ min: min }); });
aurelia_validation_1.ValidationRules.customRule("exclusiveMaximum", function (value, object, max) {
return value === null || value === undefined || value < max;
}, "${$displayName} must be less than ${$config.min}", function (max) { return ({ max: max }); });
aurelia_validation_1.ValidationRules.customRule("currency", function (value, object) {
return ((typeof (value) === "number") && !Number.isNaN(value));
}, "${$displayName} must be a valid currency amount");
};
FormSchemaRulesProvider.prototype.requiresValidation = function (property, jsonSchema, showingKeyColumn) {
if (property.isKey && !showingKeyColumn) {
return false;
}
return (property.required ||
(property.range !== undefined) ||
(property.minLength !== undefined) ||
(property.maxLength !== undefined) ||
(property.minimum !== undefined) ||
(property.maximum !== undefined) ||
(property.exclusiveMinimum !== undefined) ||
(property.exclusiveMaximum !== undefined) ||
(property.matches !== undefined) ||
(property.passwordRequirements !== undefined) ||
(property.format !== undefined));
};
FormSchemaRulesProvider = __decorate([
aurelia_framework_1.transient(),
__metadata("design:paramtypes", [])
], FormSchemaRulesProvider);
return FormSchemaRulesProvider;
}());
exports.FormSchemaRulesProvider = FormSchemaRulesProvider;
//# sourceMappingURL=form-schema-rules-provider.js.map