cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
190 lines (186 loc) • 7.54 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/validator/index.ts
var validator_exports = {};
__export(validator_exports, {
Validator: () => Validator,
validator: () => validator
});
module.exports = __toCommonJS(validator_exports);
// src/validator/locales/en.ts
var localeEn = {
min: (min) => `Min ${min}`,
max: (max) => `Max ${max}`,
isEmail: "Invalid Email",
required: "Required",
alphanumeric: "Value needs to be alphanumeric ( 0~9, a~Z )",
noSpecials: "Must not contain special symbols (!@$%...etc)",
isUUIDv4: "Value needs to be a v4 UUID",
isSameAs: "Value does not match"
};
// src/validator/locales/ja.ts
var localeJa = {
min: (min) => `\u6700\u5C0F ${min}`,
max: (max) => `\u6700\u5927 ${max}`,
isEmail: "\u7121\u52B9\u306A\u30E1\u30FC\u30EB\u3067\u3059",
required: "\u5FC5\u8981\u9805\u76EE\u3067\u3059",
alphanumeric: "\u534A\u89D2\u82F1\u6587\u5B57\u30FB\u6570\u5B57\u3092\u5165\u308C\u3066\u304F\u3060\u3055\u3044 ( 0~9, a~Z )",
noSpecials: "\u7279\u6B8A\u8A18\u53F7\u306F\u4F7F\u3048\u307E\u305B\u3093 (!@$%...\u306A\u3069)",
isUUIDv4: "\u7121\u52B9\u306AUUIDv4",
isSameAs: "\u518D\u78BA\u8A8D\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F"
};
// src/validator/index.ts
var errorMsg = {
en: localeEn,
ja: localeJa
};
var _Validator = class _Validator {
constructor() {
this.locale = "en";
this.currentErrorName = "";
this.errorMap = {};
this.pristineMap = {};
this.valueMap = {};
}
setLocale(locale) {
this.locale = locale;
}
value(inputValue) {
this.inputValue = inputValue;
return this;
}
/**
* Name the current value for grouping errors in errorMap.
* Must be chained after value().
*/
as(inputName) {
this.currentErrorName = inputName;
if (this.pristineMap[inputName] === void 0) {
this.pristineMap[inputName] = true;
} else if (this.valueMap[inputName] !== this.inputValue) {
this.pristineMap[inputName] = false;
}
this.errorMap[inputName] = [];
this.valueMap[inputName] = this.inputValue;
return this;
}
_addError(err) {
if (!this.currentErrorName) return;
if (this.pristineMap[this.currentErrorName]) return;
if (!this.errorMap[this.currentErrorName])
this.errorMap[this.currentErrorName] = [];
this.errorMap[this.currentErrorName].push(err);
}
/**
* Check minimum length (strings/arrays) or minimum value (numbers).
* Does not check for null/undefined — use required() for that.
*/
min(min) {
if (!this.inputValue) return this;
if (_Validator.isArray(this.inputValue) || _Validator.isString(this.inputValue)) {
if (this.inputValue.length < min)
this._addError({ errored: this.inputValue, code: "MinReachedS", label: errorMsg[this.locale].min(String(min)) });
} else if (_Validator.isNumber(this.inputValue)) {
if (this.inputValue < min)
this._addError({ errored: this.inputValue, code: "MinReachedN", label: errorMsg[this.locale].min(String(min)) });
}
return this;
}
max(max) {
if (!this.inputValue) return this;
if (_Validator.isArray(this.inputValue) || _Validator.isString(this.inputValue)) {
if (this.inputValue.length > max)
this._addError({ errored: this.inputValue, code: "MaxReachedS", label: errorMsg[this.locale].max(String(max)) });
} else if (_Validator.isNumber(this.inputValue)) {
if (this.inputValue > max)
this._addError({ errored: this.inputValue, code: "MaxReachedN", label: errorMsg[this.locale].max(String(max)) });
}
return this;
}
isEmail() {
if (!this.inputValue) return this;
if (!/.+@.+\..+/.test(String(this.inputValue)))
this._addError({ errored: this.inputValue, code: "InvalidEmail", label: errorMsg[this.locale].isEmail });
return this;
}
required() {
if (this.inputValue === "" || this.inputValue === null || this.inputValue === void 0)
this._addError({ errored: this.inputValue, code: "REQUIRED", label: errorMsg[this.locale].required });
return this;
}
alphanumeric() {
if (!this.inputValue) throw new Error("Please provide a value");
if (!/^[0-9a-zA-Z]+$/.test(String(this.inputValue)))
this._addError({ errored: this.inputValue, code: "ALPHANUM", label: errorMsg[this.locale].alphanumeric });
return this;
}
noSpecials(space = true, extendJpn = false) {
if (!this.inputValue) return this;
let format = /[`!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?~]/;
if (!space) format = /[ `!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?~]/;
if (extendJpn) format = /[`!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?~`!@#$%^&*()_+ー=「」『』;':"\|、。<>・?〜]/;
if (!space && extendJpn) format = /[ `!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?~ `!@#$%^&*()_+ー=「」『』;':"\|、。<>・?〜]/;
if (format.test(String(this.inputValue)))
this._addError({ errored: this.inputValue, code: "SPESYMBOL", label: errorMsg[this.locale].noSpecials });
return this;
}
isUUIDv4() {
if (!this.inputValue) throw new Error("Please provide a value");
if (!/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i.test(String(this.inputValue)))
this._addError({ errored: this.inputValue, code: "INVALID_UUIDV4", label: errorMsg[this.locale].isUUIDv4 });
return this;
}
isSameAs(value) {
if (!this.inputValue) throw new Error("Please provide a value");
if (this.inputValue !== value)
this._addError({ errored: this.inputValue, code: "NOT_SAME", label: errorMsg[this.locale].isSameAs });
return this;
}
verify(returnErrors = false) {
const hasErrors = Object.values(this.errorMap).some((errs) => errs.length > 0);
if (hasErrors) return returnErrors ? this.errorMap : false;
return true;
}
hasError() {
return Object.values(this.errorMap).some((errs) => errs.length > 0);
}
reset() {
this.inputValue = null;
this.errorMap = {};
this.currentErrorName = "";
this.pristineMap = {};
this.valueMap = {};
}
};
// Static type guards
_Validator.isDefined = (x) => x !== void 0 && x !== null;
_Validator.isUndefined = (x) => x === void 0;
_Validator.isArray = Array.isArray;
_Validator.isString = (x) => typeof x === "string" || x instanceof String;
_Validator.isObject = (x) => Object.prototype.toString.call(x) === "[object Object]";
_Validator.isNumber = (x) => Object.prototype.toString.call(x) === "[object Number]";
_Validator.isBoolean = (x) => Object.prototype.toString.call(x) === "[object Boolean]";
var Validator = _Validator;
var validator = new Validator();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Validator,
validator
});
//# sourceMappingURL=index.js.map