ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
130 lines • 7.6 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);
};
import { transient } from "aurelia-framework";
import { ValidationRules } from "aurelia-validation";
let FormSchemaRulesProvider = class FormSchemaRulesProvider {
constructor() {
this.validationRules = null;
this.registerCustomRules();
}
generateValidationRules(formSchema, showingKeyColumn) {
let rules = this.createRules(formSchema, showingKeyColumn);
return this.validationRules = rules ? rules.rules : null;
}
createRules(formSchema, showingKeyColumn) {
let ensureApi = ValidationRules;
for (let property of formSchema.properties) {
if (this.requiresValidation(property, formSchema, showingKeyColumn)) {
let 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;
}
registerCustomRules() {
ValidationRules.customRule("range", (value, object, min, max) => {
return value === null || value === undefined ||
(value >= min && value <= max);
}, `\${$displayName} must be between \${$config.min} and \${$config.max}`, (min, max) => ({ min, max }));
ValidationRules.customRule("minimum", (value, object, min) => {
return value === null || value === undefined || value >= min;
}, `\${$displayName} must be \${$config.min} or greater`, (min) => ({ min }));
ValidationRules.customRule("maximum", (value, object, max) => {
return value === null || value === undefined || value <= max;
}, `\${$displayName} must be \${$config.max} or less`, (max) => ({ max }));
ValidationRules.customRule("exclusiveMinimum", (value, object, min) => {
return value === null || value === undefined || value > min;
}, `\${$displayName} must be greater than \${$config.min}`, (min) => ({ min }));
ValidationRules.customRule("exclusiveMaximum", (value, object, max) => {
return value === null || value === undefined || value < max;
}, `\${$displayName} must be less than \${$config.min}`, (max) => ({ max }));
ValidationRules.customRule("currency", (value, object) => {
return ((typeof (value) === "number") && !Number.isNaN(value));
}, `\${$displayName} must be a valid currency amount`);
}
requiresValidation(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([
transient(),
__metadata("design:paramtypes", [])
], FormSchemaRulesProvider);
export { FormSchemaRulesProvider };
//# sourceMappingURL=form-schema-rules-provider.js.map