UNPKG

validlyjs

Version:

A high-performance Laravel-inspired validation library for TypeScript/JavaScript

33 lines (32 loc) 2.95 kB
function e(e,r){const i=e[r] return r in e&&null!=i&&""!==i}function r(e){return null!=e&&("string"==typeof e?e.length>0&&e.trim().length>0:!Array.isArray(e)||e.length>0)}async function i(e,r,i,t){try{const n={field:i,data:t,path:i} return(await a.validateValue(e,r,n)).passed}catch(e){return!1}}function t(e,r,i){switch(r){case"gte":return Array.isArray(e)?e.length>=i:e>=i case"gt":return Array.isArray(e)?e.length>i:e>i case"lte":return Array.isArray(e)?i>=e.length:i>=e case"lt":return Array.isArray(e)?i>e.length:i>e case"eq":return Array.isArray(e)?e.length===i:e===i case"length":return!!Array.isArray(e)&&e.length===i default:return!1}}import{a as n}from"./index-Ci9AjmGp.js" let a=null const s={name:"required_if",validate:(e,i,t,n)=>{const a=i[0] return!a||n[a]!==i[1]||r(e)},message:"The {field} field is required when {0} is {1}.",priority:1},l={name:"required_unless",validate:(e,i,t,n)=>{const a=i[0] return a&&n[a]===i[1]||r(e)},message:"The {field} field is required unless {0} is {1}.",priority:1},o={name:"required_with",validate:(i,t,n,a)=>{const s=t.length if(0===s)return!0 for(let n=0;s>n;n++)if(e(a,t[n]))return r(i) return!0},message:"The {field} field is required when {0} is present.",priority:1},u={name:"required_with_all",validate:(i,t,n,a)=>{const s=t.length if(0===s)return!0 for(let r=0;s>r;r++)if(!e(a,t[r]))return!0 return r(i)},message:"The {field} field is required when {0} are present.",priority:1},d={name:"required_without",validate:(i,t,n,a)=>{const s=t.length if(0===s)return!0 for(let n=0;s>n;n++)if(!e(a,t[n]))return r(i) return!0},message:"The {field} field is required when {0} is not present.",priority:1},f={name:"required_without_all",validate:(i,t,n,a)=>{const s=t.length if(0===s)return!0 for(let r=0;s>r;r++)if(e(a,t[r]))return!0 return r(i)},message:"The {field} field is required when none of {0} are present.",priority:1},c={name:"prohibited",validate:(e,i,t,n)=>!(t in n)||!r(e),message:"The {field} field is prohibited.",priority:1},h={name:"prohibited_if",validate:(e,i,t,n)=>{const a=i[0] return!a||n[a]!==i[1]||!r(e)},message:"The {field} field is prohibited when {0} is {1}.",priority:1},p={name:"prohibited_unless",validate:(e,i,t,n)=>{const a=i[0] return a&&n[a]===i[1]||!r(e)},message:"The {field} field is prohibited unless {0} is {1}.",priority:1},m={name:"when",validate:async(e,r,s,l)=>{const[o,u]=r,d=l[o] a||(a=new n) for(const[r,n]of Object.entries(u))if("then"!==r&&"otherwise"!==r&&"object"==typeof n)for(const[r,a]of Object.entries(n)){const n=t(d,r,a) if(n&&u.then)return await i(e,u.then,s,l) if(!n&&u.otherwise)return await i(e,u.otherwise,s,l)}return!0},message:"The {field} field validation failed based on conditional rules.",async:!0} export{h as prohibitedIfRule,c as prohibitedRule,p as prohibitedUnlessRule,s as requiredIfRule,l as requiredUnlessRule,u as requiredWithAllRule,o as requiredWithRule,f as requiredWithoutAllRule,d as requiredWithoutRule,m as whenRule}