js-formdata-validator
Advanced tools
Comparing version 0.0.6 to 0.0.7
export { default as Validator } from "./validator"; | ||
export { FieldValidator } from "./field"; |
@@ -1,5 +0,5 @@ | ||
var d = Object.defineProperty; | ||
var g = (t, r, e) => r in t ? d(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e; | ||
var n = (t, r, e) => (g(t, typeof r != "symbol" ? r + "" : r, e), e); | ||
const l = { | ||
var m = Object.defineProperty; | ||
var f = (t, r, e) => r in t ? m(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e; | ||
var l = (t, r, e) => (f(t, typeof r != "symbol" ? r + "" : r, e), e); | ||
const n = { | ||
required: "The field cannot be empty", | ||
@@ -14,30 +14,30 @@ array: "The field must be instance of Array", | ||
min: "The field has minimum of {minSize} but it got value {value}" | ||
}, c = { | ||
}, d = { | ||
required(t) { | ||
if (t === void 0 || t === "" || t === null) | ||
return l.required; | ||
return n.required; | ||
}, | ||
array(t) { | ||
if (!(t instanceof Array)) | ||
return l.array; | ||
return n.array; | ||
}, | ||
integer(t) { | ||
if (!Number.isInteger(t)) | ||
return l.integer; | ||
return n.integer; | ||
}, | ||
numeric(t) { | ||
if (typeof t != "number") | ||
return l.numeric; | ||
return n.numeric; | ||
}, | ||
string(t) { | ||
if (typeof t != "string") | ||
return l.string; | ||
return n.string; | ||
}, | ||
boolean(t) { | ||
if (typeof t != "boolean") | ||
return l.boolean; | ||
return n.boolean; | ||
}, | ||
allowed(t, ...r) { | ||
if (!r.includes(t)) | ||
return l.allowed.replace( | ||
return n.allowed.replace( | ||
"{args}", | ||
@@ -49,3 +49,3 @@ r.join(", ") | ||
if (!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(t)) | ||
return l.email; | ||
return n.email; | ||
}, | ||
@@ -56,14 +56,14 @@ min(t, r) { | ||
if (e < r) | ||
return l.min.replace("{minSize}", r.toString()).replace("{value}", e.toString()); | ||
return n.min.replace("{minSize}", r.toString()).replace("{value}", e.toString()); | ||
} else if (typeof t == "string") { | ||
const e = t.length; | ||
if (e < r) | ||
return l.min.replace("{minSize}", r.toString()).replace("{value}", e.toString()); | ||
return n.min.replace("{minSize}", r.toString()).replace("{value}", e.toString()); | ||
} | ||
} | ||
}; | ||
class m { | ||
class g { | ||
constructor() { | ||
n(this, "errorMessage"); | ||
n(this, "errorBag", {}); | ||
l(this, "errorMessage"); | ||
l(this, "errorBag", {}); | ||
} | ||
@@ -92,13 +92,13 @@ add(r, e) { | ||
} | ||
class p { | ||
class v { | ||
constructor({ formData: r, customRules: e, rules: a, stopOnFirstFailure: i }) { | ||
n(this, "formData"); | ||
n(this, "rules"); | ||
n(this, "validatorError"); | ||
n(this, "validator"); | ||
n(this, "stopOnFirstFailure"); | ||
this.formData = r ?? {}, this.rules = a ?? {}, this.validatorError = new m(), this.stopOnFirstFailure = i ?? !1, this.mergeCustomRules(e); | ||
l(this, "formData"); | ||
l(this, "rules"); | ||
l(this, "validatorError"); | ||
l(this, "validator"); | ||
l(this, "stopOnFirstFailure"); | ||
this.formData = r ?? {}, this.rules = a ?? {}, this.validatorError = new g(), this.stopOnFirstFailure = i ?? !1, this.mergeCustomRules(e); | ||
} | ||
mergeCustomRules(r) { | ||
return this.setValidator({ ...c, ...r }), this; | ||
return this.setValidator({ ...d, ...r }), this; | ||
} | ||
@@ -233,4 +233,89 @@ async validate() { | ||
} | ||
class p { | ||
constructor() { | ||
l(this, "fieldName"); | ||
l(this, "fieldValue"); | ||
l(this, "fieldRules"); | ||
l(this, "validatorError"); | ||
l(this, "validator"); | ||
l(this, "formData"); | ||
this.fieldName = "", this.fieldRules = [], this.fieldValue = null, this.validatorError = new g(), this.validator = d, this.formData = {}; | ||
} | ||
setCustomRules(r) { | ||
this.mergeCustomRules(r); | ||
} | ||
setFieldName(r) { | ||
this.fieldName = r; | ||
} | ||
setFormData(r) { | ||
this.formData = r; | ||
} | ||
getFormData() { | ||
return this.formData; | ||
} | ||
getFieldName() { | ||
return this.fieldName; | ||
} | ||
setFieldRules(r) { | ||
this.fieldRules = r; | ||
} | ||
getFieldRules() { | ||
return this.fieldRules; | ||
} | ||
setFieldValue(r) { | ||
this.fieldValue = r; | ||
} | ||
getFieldValue() { | ||
return this.fieldValue; | ||
} | ||
async validate() { | ||
this.validatorError.clearErrorBag(), (await Promise.all( | ||
this.fieldRules.map( | ||
(e) => typeof this.getValidatorResult(e) == "object" ? this.getValidatorResult(e) : Promise.resolve(this.getValidatorResult(e)) | ||
) | ||
)).forEach((e) => { | ||
e && this.validatorError.add(this.fieldName, e); | ||
}); | ||
} | ||
async getValidatorResult(r) { | ||
let e; | ||
switch (typeof r) { | ||
case "string": { | ||
e = this.handleStringRule(r); | ||
break; | ||
} | ||
case "function": { | ||
e = r(this.fieldValue, this.formData); | ||
break; | ||
} | ||
} | ||
return e; | ||
} | ||
handleStringRule(r) { | ||
const [e, a] = this.parseRule(r); | ||
return this.validator[e](this.fieldValue, ...a); | ||
} | ||
parseRule(r) { | ||
const [e, a] = r.split(":"), i = a ? a.split(",").map((o) => o.trim()) : []; | ||
return [e, i]; | ||
} | ||
pass() { | ||
return !this.validatorError.hasErrors(); | ||
} | ||
fail() { | ||
return !this.pass(); | ||
} | ||
getErrorBag() { | ||
return this.validatorError.getErrorBag()[this.fieldName]; | ||
} | ||
mergeCustomRules(r) { | ||
return this.setValidator({ ...d, ...r }), this; | ||
} | ||
setValidator(r) { | ||
return this.validator = r, this; | ||
} | ||
} | ||
export { | ||
p as Validator | ||
p as FieldValidator, | ||
v as Validator | ||
}; |
{ | ||
"name": "js-formdata-validator", | ||
"private": false, | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "main": "./dist/js-formdata-validator.umd.cjs", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27053
11
542