rawx
Version:
process daemon with utilities
125 lines • 4.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const Ops = require("../../ops/ops");
let { log, accent, keys } = new Ops();
class Validator {
constructor() { }
}
exports.Validator = Validator;
const skips = (prop_name) => /^_/.exec(prop_name);
class Arg_Validator {
constructor(name, arg_dict) {
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "arg_dict", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "warnings", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "errors", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "validated", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
this.name = name;
this.arg_dict = arg_dict;
}
validate(args) {
for (const [key, val] of Object.entries(args)) {
// Ignore JSON `$_` keys (comments or non-external args)
if (skips(key))
continue;
// We will ignore undefined
if (this.arg_dict[key] === undefined) {
continue;
}
const rule = this.arg_dict[key];
if (rule.type === "number") {
if (typeof val !== "number") {
this.errors.push(`Prop: ${key}, is expected to be a number.`);
continue;
}
if (rule.min !== undefined) {
this.validated[key] = Math.min(rule.min, val);
}
else {
this.validated[key] = val;
}
continue;
}
if (rule.type === "string") {
if (typeof val !== "string") {
this.errors.push(`Prop: ${key}, is expected to be a string.`);
continue;
}
if (rule.one_of !== undefined) {
if (rule.one_of.indexOf(val) !== -1)
this.validated[key] = val;
else {
this.errors.push(`Prop: ${key}, must be one of: ${rule.one_of.join(", ")}`);
}
}
else {
this.validated[key] = val;
}
continue;
}
if (rule.type === "boolean") {
if (typeof val !== "boolean") {
this.errors.push(`Prop: ${key}, is expected to be a boolean or undefined.`);
continue;
}
if (rule.polar !== undefined) {
if (rule.polar === val) {
this.validated[key] = val;
continue;
}
this.warnings.push(`prop: ${key} passed in unnecessary polarity, ignoring`);
continue;
}
}
if (typeof val === "object") {
if (Array.isArray(val)) {
this.warnings.push(`Array type validation is a WIP, prop: ${key} set without validation.`);
}
else {
this.warnings.push(`object type validation is a WIP, prop: ${key} set without validation.`);
}
}
}
console.log(`<validate results>`);
console.log(`this.warnings:`);
console.log(this.warnings);
console.log(`this.errors:`);
console.log(this.errors);
}
set_validated(_self) {
for (const k of keys(this.validated)) {
const v = this.validated[k];
console.log(`k, "L:181", v:`);
console.log(`${k}, "L:182", ${v}`);
// self[k as keyof Server_Construct_Class] = v;
}
}
}
module.exports = Arg_Validator;
//# sourceMappingURL=validator.js.map