validlyjs
Version:
A high-performance Laravel-inspired validation library for TypeScript/JavaScript
28 lines (27 loc) • 2.33 kB
JavaScript
function e(e){return e}async function t(e,t,r,n,o){try{const i=(await e.validateValue(t,r,n)).passed
return 1e3>a.size&&a.set(o,i),i}catch(e){return 1e3>a.size&&a.set(o,!1),!1}}import{a as r}from"./index-Ci9AjmGp.js"
let n=null
const a=new Map,o=e=>"object"==typeof e&&null!==e&&!Array.isArray(e),i={name:"object.shape",validate:async(i,s,c,u)=>{if(!o(i))return!1
const[y]=s
if(!(e=>e&&"object"==typeof e&&!Array.isArray(e))(y))return!0
n||(n=new r)
const l=Object.entries(y)
if(0===l.length)return!0
const f=[]
for(let r=0;l.length>r;r++){const[o,s]=l[r],y=`${c}.${o}:${JSON.stringify(s)}:${JSON.stringify(i[o])}`
if(a.has(y)){if(!a.get(y))return!1
continue}const p=i[o],m={field:`${c}.${o}`,data:u,parameters:[]},b=e(s)
f.push(t(n,p,b,m,y))}return(await Promise.all(f)).every(e=>e)},message:"The {field} does not match the required shape.",async:!0},s={name:"object.has",validate:(e,t)=>{if(!o(e))return!1
const[r]=t
return("string"==typeof r||"number"==typeof r||"symbol"==typeof r)&&r in e},message:"The {field} must have the property {0}."},c={name:"object.keys",validate:(e,t)=>{if("object"!=typeof e||null===e||Array.isArray(e))return!1
const r=t
return Object.keys(e).every(e=>r.includes(e))},message:"The {field} contains invalid keys.",priority:2},u={name:"object.requiredKeys",validate:(e,t)=>{if("object"!=typeof e||null===e||Array.isArray(e))return!1
const r=t,n=Object.keys(e)
return r.every(e=>n.includes(e))},message:"The {field} is missing required keys.",priority:2},y={name:"object.keyCount",validate:(e,t)=>{if("object"!=typeof e||null===e||Array.isArray(e))return!1
const[r]=t,n=parseInt(r,10)
return Object.keys(e).length===n},message:"The {field} must have exactly {0} keys.",priority:2},l={name:"object.strict",validate:(e,t)=>{if("object"!=typeof e||null===e||Array.isArray(e))return!1
const[r]=t
if(!Array.isArray(r))return!0
const n=Object.keys(e)
return n.every(e=>r.includes(e))&&r.length>=n.length},message:"The {field} contains unexpected properties.",priority:2},f={name:"object.notEmpty",validate:e=>"object"==typeof e&&null!==e&&!Array.isArray(e)&&Object.keys(e).length>0,message:"The {field} must not be empty.",priority:2}
export{s as objectHasRule,y as objectKeyCountRule,c as objectKeysRule,f as objectNotEmptyRule,u as objectRequiredKeysRule,i as objectShapeRule,l as objectStrictRule}