Create a TypeScript library that evaluates XForm constraint expressions locally, without network access, so field workers catch data entry errors immediately. ```typescript 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 count - `regex(expr, pattern)` — returns boolean - `number(expr)` — casts to number - `if(condition, then, else)` — ternary - `coalesce(a, b)` — returns first non-empty value - `selected(field, value)` — true if multi-select field contains value - `count-selected(field)` — count of selected values Comparison 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.