Form Validate With Composable Rules

Validate form fields with composable rule functions that return typed errors and support async dependency checks.

JavascriptForm Validation

Code

snippet
export function composeRules(rules){return async function(value,context){for(const rule of rules){const result=await rule(value,context);if(result!==true)return result;}return true;};}export const required=(msg='required')=>v=>v!==null&&v!==undefined&&String(v).trim()!==''?true:msg;export const minLen=(n,msg)=>v=>String(v).length>=n?true:(msg??'min length '+n);export const matches=(regex,msg)=>v=>regex.test(String(v))?true:(msg??'invalid format');export const asyncUnique=(check,msg)=>async v=>await check(v)?true:(msg??'already taken');