Create a TypeScript library that evaluates XForm constraint expressions locally, without network access, so field workers catch data entry errors immediately.
validateConstraint(
expression: string,
value: unknown,
formContext: Record<string, unknown> // other field values in scope
): { valid: boolean, message?: string }
Implement evaluation for these XPath functions commonly used in ODK forms:
string-length(expr) — returns character countregex(expr, pattern) — returns booleannumber(expr) — casts to numberif(condition, then, else) — ternarycoalesce(a, b) — returns first non-empty valueselected(field, value) — true if multi-select field contains valuecount-selected(field) — count of selected valuesComparison operators to support: =, !=, <, <=, >, >=, and, or, not().
Parse expressions with a simple recursive descent parser. No external parser libraries.
Write a test suite with 20 XForm constraint examples: phone number regex, date range check, required-if-other-selected, numeric bounds, string length limits.
No contributions yet.