Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 403x 403x 403x 403x 403x 403x 403x 403x 403x 403x 10x 10x 10x 10x 10x 20x 20x 11x 20x 9x 9x 9x 9x 9x 9x 9x 9x 9x 20x 10x 10x 10x 403x 403x 403x 403x 403x 403x 403x 8x 8x 8x 8x 8x 8x 8x 403x 403x 403x 403x 403x 403x 403x 403x 403x 18x 18x 18x 18x 18x 18x 24x 24x 24x 24x 24x 24x 8x 8x 8x 8x 8x 8x 8x 8x 24x 24x 5x 1x 5x 4x 4x 4x 5x 5x 5x 24x 24x 4x 4x 4x 4x 4x 3x 3x 4x 4x 4x 4x 24x 24x 3x 3x 3x 3x 3x 2x 2x 3x 3x 3x 3x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 24x 24x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 2x 2x 2x 2x 2x 2x 2x 24x 24x 24x 24x 24x 12x 12x 12x 24x 18x 18x 18x 403x 403x 403x 403x 403x 403x 403x 403x 10x 10x 10x 3x 3x 3x 3x 3x 3x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 12x 12x 7x 7x 7x 12x 10x 10x | /**
* Declarative Form Validation Engine for Avenx
*/
/**
* Parses a data-ax-validate rule string (e.g. "required|email|min:8").
* @param {string} ruleString - Raw directive value.
* @returns {Array<{name: string, arg: string|null, customMsg: string|null}>}
*/
export function parseValidationRules(ruleString) {
if (!ruleString || typeof ruleString !== 'string') return [];
const rules = [];
const parts = ruleString.split('|').map((r) => r.trim()).filter(Boolean);
for (const part of parts) {
const colonIdx = part.indexOf(':');
if (colonIdx === -1) {
rules.push({ name: part.toLowerCase(), arg: null, customMsg: null });
} else {
const name = part.slice(0, colonIdx).trim().toLowerCase();
const rest = part.slice(colonIdx + 1).trim();
const secondColonIdx = rest.indexOf(':');
if (secondColonIdx !== -1 && name !== 'pattern' && name !== 'regex') {
const arg = rest.slice(0, secondColonIdx).trim();
const customMsg = rest.slice(secondColonIdx + 1).trim();
rules.push({ name, arg, customMsg });
} else {
rules.push({ name, arg: rest, customMsg: null });
}
}
}
return rules;
}
/**
* Extracts field name from an HTML element.
* @param {Element} el
* @returns {string}
*/
export function getFieldName(el) {
if (!el || typeof el.getAttribute !== 'function') return 'field';
return (
el.getAttribute('name') ||
el.getAttribute('data-ax-bind') ||
el.getAttribute('id') ||
'field'
);
}
/**
* Evaluates a field value against parsed rules.
* @param {any} value - The input value.
* @param {Array<{name: string, arg: string|null, customMsg: string|null}>} rules - Parsed rules.
* @param {object} [context] - Additional scope/context (state, customMessages).
* @returns {string[]} Array of validation error messages.
*/
export function validateValue(value, rules, context = {}) {
const errors = [];
const customMessages = context.customMessages || {};
const strVal = value === undefined || value === null ? '' : String(value);
for (const rule of rules) {
const { name, arg, customMsg } = rule;
let isValid = true;
let defaultMsg = '';
switch (name) {
case 'required': {
if (typeof value === 'boolean') {
isValid = value === true;
} else if (Array.isArray(value)) {
isValid = value.length > 0;
} else {
isValid = strVal.trim().length > 0;
}
defaultMsg = 'Field is required';
break;
}
case 'email': {
if (strVal.trim() === '') {
isValid = true;
} else {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
isValid = emailRegex.test(strVal);
}
defaultMsg = 'Invalid email address';
break;
}
case 'min': {
const minVal = parseFloat(arg);
if (!isNaN(minVal)) {
if (typeof value === 'number') {
isValid = value >= minVal;
} else if (Array.isArray(value)) {
isValid = value.length >= minVal;
} else if (strVal !== '') {
isValid = strVal.length >= minVal;
}
}
defaultMsg = `Minimum length/value is ${arg}`;
break;
}
case 'max': {
const maxVal = parseFloat(arg);
if (!isNaN(maxVal)) {
if (typeof value === 'number') {
isValid = value <= maxVal;
} else if (Array.isArray(value)) {
isValid = value.length <= maxVal;
} else if (strVal !== '') {
isValid = strVal.length <= maxVal;
}
}
defaultMsg = `Maximum length/value is ${arg}`;
break;
}
case 'pattern':
case 'regex': {
if (strVal.trim() === '' || !arg) {
isValid = true;
} else {
try {
const re = new RegExp(arg);
isValid = re.test(strVal);
} catch {
isValid = false;
}
}
defaultMsg = 'Field format is invalid';
break;
}
case 'numeric':
case 'number': {
if (strVal.trim() === '') {
isValid = true;
} else {
isValid = /^-?\d+(\.\d+)?$/.test(strVal.trim());
}
defaultMsg = 'Must be a number';
break;
}
case 'alpha': {
if (strVal.trim() === '') {
isValid = true;
} else {
isValid = /^[a-zA-Z]+$/.test(strVal);
}
defaultMsg = 'Must contain only letters';
break;
}
case 'alphanumeric': {
if (strVal.trim() === '') {
isValid = true;
} else {
isValid = /^[a-zA-Z0-9]+$/.test(strVal);
}
defaultMsg = 'Must contain only letters and numbers';
break;
}
case 'url': {
if (strVal.trim() === '') {
isValid = true;
} else {
try {
const url = new URL(strVal);
isValid = !!url;
} catch {
isValid = false;
}
}
defaultMsg = 'Invalid URL format';
break;
}
case 'same': {
if (arg && context.state) {
const targetValue = context.state[arg];
isValid = value === targetValue;
}
defaultMsg = `Must match ${arg}`;
break;
}
default:
break;
}
if (!isValid) {
const msg = customMsg || customMessages[name] || defaultMsg;
errors.push(msg);
}
}
return errors;
}
/**
* Initializes or updates component state.$validation structure.
* @param {object} state - Reactive component state.
* @param {string} fieldName - Target field name.
* @param {string[]} errors - List of validation errors for field.
*/
export function updateValidationState(state, fieldName, errors) {
if (!state) return;
if (!state.$validation) {
state.$validation = {
isValid: true,
errors: {},
fields: {},
};
}
if (!state.$validation.errors) {
state.$validation.errors = {};
}
if (!state.$validation.fields) {
state.$validation.fields = {};
}
const isFieldValid = errors.length === 0;
state.$validation.errors[fieldName] = errors;
state.$validation.fields[fieldName] = {
isValid: isFieldValid,
errors,
};
let allValid = true;
for (const fieldKey of Object.keys(state.$validation.fields)) {
const f = state.$validation.fields[fieldKey];
if (f && f.isValid === false) {
allValid = false;
break;
}
}
state.$validation.isValid = allValid;
}
|