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