Initial content
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.no_op = exports.required = exports.email = exports.minLength = void 0;
|
||||
const validator_1 = __importDefault(require("validator"));
|
||||
const minLength = (min) => (status) => {
|
||||
if (!validator_1.default.isLength(status.value, { min })) {
|
||||
status.setInvalid(true);
|
||||
status.messages.push(`Enter at least ${min} characters`);
|
||||
}
|
||||
};
|
||||
exports.minLength = minLength;
|
||||
const email = (status) => {
|
||||
if (!validator_1.default.isEmail(status.value)) {
|
||||
status.setInvalid(true);
|
||||
status.messages.push("Enter an email address");
|
||||
}
|
||||
};
|
||||
exports.email = email;
|
||||
const required = (status) => {
|
||||
if (validator_1.default.isEmpty(status.value.toString(), { ignore_whitespace: true })) {
|
||||
status.setInvalid(true);
|
||||
status.messages.push("A value is required");
|
||||
}
|
||||
};
|
||||
exports.required = required;
|
||||
const no_op = (status) => { };
|
||||
exports.no_op = no_op;
|
||||
@@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./validation_types"), exports);
|
||||
__exportStar(require("./validator"), exports);
|
||||
__exportStar(require("./basic_rules"), exports);
|
||||
__exportStar(require("./order_rules"), exports);
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AddressValidator = exports.CustomerValidator = void 0;
|
||||
const validator_1 = require("./validator");
|
||||
const basic_rules_1 = require("./basic_rules");
|
||||
exports.CustomerValidator = new validator_1.Validator({
|
||||
name: [basic_rules_1.required, (0, basic_rules_1.minLength)(6)],
|
||||
email: basic_rules_1.email,
|
||||
federatedId: basic_rules_1.no_op
|
||||
});
|
||||
exports.AddressValidator = new validator_1.Validator({
|
||||
street: basic_rules_1.required,
|
||||
city: basic_rules_1.required,
|
||||
state: basic_rules_1.required,
|
||||
zip: basic_rules_1.no_op
|
||||
});
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ValidationStatus = void 0;
|
||||
class ValidationStatus {
|
||||
value;
|
||||
invalid = false;
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
get isInvalid() {
|
||||
return this.invalid;
|
||||
}
|
||||
setInvalid(newValue) {
|
||||
this.invalid = newValue || this.invalid;
|
||||
}
|
||||
messages = [];
|
||||
}
|
||||
exports.ValidationStatus = ValidationStatus;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getData = exports.isValid = exports.Validator = void 0;
|
||||
const validation_types_1 = require("./validation_types");
|
||||
class Validator {
|
||||
rules;
|
||||
breakOnInvalid;
|
||||
constructor(rules, breakOnInvalid = true) {
|
||||
this.rules = rules;
|
||||
this.breakOnInvalid = breakOnInvalid;
|
||||
}
|
||||
async validate(data) {
|
||||
const vdata = Object.entries(this.rules).map(async ([key, rules]) => {
|
||||
const status = new validation_types_1.ValidationStatus(data?.[key] ?? "");
|
||||
const rs = (Array.isArray(rules) ? rules : [rules]);
|
||||
for (const r of rs) {
|
||||
if (!status.isInvalid || !this.breakOnInvalid) {
|
||||
await r(status);
|
||||
}
|
||||
}
|
||||
return [key, status];
|
||||
});
|
||||
const done = await Promise.all(vdata);
|
||||
return Object.fromEntries(done);
|
||||
}
|
||||
validateOriginal(data) {
|
||||
const vdata = Object.entries(this.rules).map(([key, rules]) => {
|
||||
const status = new validation_types_1.ValidationStatus(data?.[key] ?? "");
|
||||
(Array.isArray(rules) ? rules : [rules])
|
||||
.forEach(async (rule) => {
|
||||
if (!status.isInvalid || !this.breakOnInvalid) {
|
||||
await rule(status);
|
||||
}
|
||||
});
|
||||
return [key, status];
|
||||
});
|
||||
return Object.fromEntries(vdata);
|
||||
}
|
||||
}
|
||||
exports.Validator = Validator;
|
||||
function isValid(result) {
|
||||
return Object.values(result)
|
||||
.every(r => r.isInvalid === false);
|
||||
}
|
||||
exports.isValid = isValid;
|
||||
function getData(result) {
|
||||
return Object.fromEntries(Object.entries(result)
|
||||
.map(([key, status]) => [key, status.value]));
|
||||
}
|
||||
exports.getData = getData;
|
||||
Reference in New Issue
Block a user