hi-validator
Version:
  
2 lines • 1.31 kB
JavaScript
import {isString,isNumber}from'hi-datatype-operation';var i=class extends Error{constructor(t,e={}){super(t),this.name="ValidationError",this.path=e.path||[],this.value=e.value;}};function o(r,t){let e=(n,a)=>{let s=r(n,a);if(!s&&a){let u=typeof t=="function"?t(n,a):t||`Validation failed at path: ${a.path.join(".")}`;throw a.errors.push(new i(u,{path:[...a.path],value:n})),new i(u,{path:[...a.path],value:n})}return s};return e.withMessage=n=>o(r,n),e.optional=()=>o(n=>n===void 0||r(n),t),e}function T(r){return o((t,e)=>typeof t!="object"||t===null?false:Object.entries(r).every(([n,a])=>{e&&e.path.push(n);let s=a(t[n],e);return e&&e.path.pop(),s}))}var c=o(isString,r=>`Expected string, got ${typeof r}`),w=o(isNumber,r=>`Expected number, got ${typeof r}`),y=o(r=>c(r)&&/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(r),"Invalid email format");var l=class{constructor(t){this.value=t;this.validators=[];}check(t,e){return this.validators.push(n=>{if(!t(n))throw new i(e||"Validation failed");return true}),this}validate(){for(let t of this.validators)t(this.value);return this.value}},E=r=>new l(r);export{l as ValidationChain,i as ValidationError,E as chain,y as checkEmail,w as checkNumber,c as checkString,o as createValidator,T as validateObject};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map