Skip to content
BLSQPublic

About

Validate ODK forms, fill and edit submissions as ODK Collect would - a fork of ODK Validate compiled with GraalVM

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

odk-cli

License CI Test report

odk_cli is a command line tool (a native binary, no JVM needed) that runs an ODK XForm (an XLSForm, once converted) the way a form client would - ODK Collect, Enketo, ODK Web Forms - to emulate and verify submissions without a phone or a browser:

  • fill a new submission from a blank form, or edit a saved one, with the answers given as JSON
  • get the form's logic recomputed: preloads, defaults, calculates, relevance, repeat counts
  • get the submission checked as the form clients do: required, constraints (with the form's own messages), types, choices offered (cascades included, with suggestions for a misspelt choice)
  • get back the resulting submission XML, or every problem found as JSON

The engine is JavaRosa, ODK Collect's own, so Collect is the reference where the clients differ. Enketo and ODK Web Forms implement the same spec with their own engines: most forms behave the same, but edge cases (an XPath function, a date format...) may differ.

What it does, case by case (forms, answers, expected XML or errors, on the JVM and on the native binary): the test report (257 cases on the JVM and 247 against the native binary, all passing; main as of 2026-12-04).

It is built for IASO, to create and edit submissions server-side with the same results as in the form clients (see Scope and contributions).

Fast: a native binary, built with GraalVM

odk_cli is compiled ahead of time with GraalVM Native Image into a single self-contained executable (Linux amd64): no JVM to install, and no JVM startup to pay on each call. That matters for a tool called once per submission, from a server or a script:

  • a submission of a small form in about 3 ms, against about 180 ms with java (the same code on a JVM, JDK 17)
  • a submission of a real-life form (17 KB: 20 questions, 34 fields with their calculates and constraints) in about 9 ms, against about 215 ms
  • about 25 MB of memory, against about 75 MB

(averages of 10 runs, odk_cli 1.0.5, Linux; most of the JVM's time is its startup and class loading, not the form)

Getting started

Download the binary (Linux amd64), or build it with the Dockerfile (see Release):

curl -fLo odk_cli https://blsq-io.s3.eu-west-1.amazonaws.com/odk-cli/1.0.5/odk_cli-linux-amd64
chmod +x odk_cli

The examples below use examples/household.xml, a small form; as an XLSForm:

survey

type name label required constraint constraint_message relevant calculation
text head_name Head of household yes
integer adults Adults yes . > 0 At least one adult
integer children Children . >= 0
calculate total ${adults} + coalesce(${children}, 0)
select_one yes_no has_bednet Bednet in the household?
integer bednets How many bednets? . <= ${total} More bednets than people ${has_bednet} = 'yes'

choices

list_name name label
yes_no yes Yes
yes_no no No

Fill a new submission, get the formulas recalculated

./odk_cli new -x examples/household.xml \
  -a '{"head_name": "Philippe Dupont", "adults": "2", "children": "3", "has_bednet": "yes", "bednets": "2"}' \
  > submission.xml

Exit code 0, the submission XML on stdout (indented here): total computed, instanceID generated as the form clients do.

<data id="household_visit" version="1">
  <head_name>Philippe Dupont</head_name>
  <adults>2</adults>
  <children>3</children>
  <total>5</total>
  <has_bednet>yes</has_bednet>
  <bednets>2</bednets>
  <meta><instanceID>uuid:1cfde7ea-f93d-40b5-b4fa-7af23b7f50a3</instanceID></meta>
</data>

Edit it: the calculates and relevance recomputed

./odk_cli edit -x examples/household.xml -i submission.xml -a '{"children": "5", "has_bednet": "no"}'

total is now 7, and bednets is gone: no longer relevant once has_bednet is no. The rest is kept as saved, instanceID included.

<data id="household_visit" version="1">
  <head_name>Philippe Dupont</head_name>
  <adults>2</adults>
  <children>5</children>
  <total>7</total>
  <has_bednet>no</has_bednet>
  <meta><instanceID>uuid:1cfde7ea-f93d-40b5-b4fa-7af23b7f50a3</instanceID></meta>
</data>

See what a form client would refuse

./odk_cli new -x examples/household.xml -a '{"adults": "0", "has_bednet": "Yes", "bednets": "9"}'

Exit code 1, and every problem found, in the form's order, as JSON on stderr: a required question left empty, a constraint with the form's own message, a choice that isn't offered (with what was probably meant), an answer to a question that isn't relevant.

{
  "refused": true,
  "errors": [
    {"path": "head_name", "code": "REQUIRED", "message": "Answer is required"},
    {"path": "adults", "code": "CONSTRAINT", "answer": "0", "message": "At least one adult"},
    {"path": "has_bednet", "code": "UNKNOWN_CHOICE", "answer": "Yes",
     "message": "Unknown choice(s): Yes; did you mean: yes?",
     "suggestions": ["yes"], "choices": ["yes", "no"], "choices_count": 2, "choices_truncated": false},
    {"path": "bednets", "code": "NOT_RELEVANT", "answer": "9", "message": "Not relevant"}
  ]
}

The error codes:

  • REQUIRED: a required question left empty, with the form's own message (jr:requiredMsg) if it has one.
  • CONSTRAINT: an answer that breaks its constraint, with the form's own message (constraint_message) if it has one; constraints between questions included (. <= ${total}).
  • INVALID: not a value of the question's type ("two" for an integer, a date that isn't one...), or one no form client could give (a decimal NaN or Infinity, a geopoint off the globe, a character XML can't hold).
  • UNKNOWN_CHOICE: a choice the select doesn't offer there (its own choices, or its itemset's given the other answers: a cascade), with suggestions (a label given instead of the name, a different case or accents, the closest spellings) and the choices offered (the first 10, choices_count, choices_truncated).
  • NOT_RELEVANT: an answer to a question that isn't relevant, given the other answers.
  • UNKNOWN_QUESTION: no such question in the form (a misspelt name, a repeat instance the form doesn't offer).
  • NOT_A_QUESTION: a calculate or a metadata field (total, meta/instanceID): computed, not answered.
  • READ_ONLY: a read-only question.
  • INVALID_SUBMISSION (edit): the saved submission isn't valid XML, or isn't one of this form (another root or form id); no path.

Each error has a code, a path (as in the answers), a message and the answer given, if any. Any other failure (an unreadable form, a wrong argument) is {"error": "..."}, exit code 1 too.

A constraint between questions is checked too: 4 bednets for a household of 1 gives {"path": "bednets", "code": "CONSTRAINT", "answer": "4", "message": "More bednets than people"}.

The full set of options and rules is below; the test report shows many more cases (repeats, cascades, dates, pulldata(), languages...).

Usage

odk_cli new  -x form.xml [-p PREFILLED] -a ANSWERS
odk_cli edit -x form.xml -i submission.xml [-p PREFILLED] -a ANSWERS [-k]
option
-x, --xform the form (XForm XML, as pyxform writes it from an XLSForm)
-i, --instance edit: the saved submission XML
-a, --answers / -af, --answers-file the answers, as JSON / a JSON file
-p, --prefilled / -pf, --prefilled-file answers set first, by question name, as Collect's intent extras
-c, --csv name=path a CSV attachment, for pulldata() and select_one_from_file (jr://file-csv/name.csv)
-k, --keep-existing-problems edit: problems the submission already had, on questions not answered, are warnings

Answers are keyed by path below the root, as in the submission XML: {"household/population": "7"}.

  • repeat[n] is the n-th repeat instance (1-based), added as Collect's "Add" would: {"child[2]/child_age": "4"}.
  • null or "" clears an answer.
  • Only questions: not the calculates, the metadata or the read-only ones (NOT_A_QUESTION, READ_ONLY).

What happens, as in a form client:

  1. new starts from the blank form: preloads (start, today, instanceID...), defaults, then the prefilled answers. edit loads the saved submission instead: its repeats, metadata and attributes are kept, preloads don't run again.
  2. The answers are set.
  3. The calculates, relevance and repeat counts are recomputed.
  4. The whole form is validated: required, constraints, types, choices.

Output

  • Accepted: exit 0, the submission XML on stdout, without the questions no longer relevant. Values the edit didn't change keep their original text (a dateTime's time zone offset...).
  • Refused: exit 1, {"refused": true, "errors": [...]} on stderr, every problem found, in the form's order (see the error codes).
  • With edit -k: exit 0, and {"warnings": [...]} on stderr for the problems the submission already had.
  • Any other failure: exit 1, {"error": "..."} on stderr.

Without new or edit, odk_cli validates an entity workflow (-y/-f, see EntityWorkflowValidator).

Scope and contributions

odk-cli is built for IASO, and closely tied to it: IASO calls it to create and edit submissions server-side (its forms, its prefilled answers, its CSV attachments for pulldata()), and its release ships to IASO's Docker images. What it supports, and how, follows what IASO needs.

Some features go further and make it more specific to IASO: the entity workflow validation (without new/edit, -y/-f) checks IASO's entity workflows, and a fuller support of entities (registration, follow-up forms, entity lists) would follow IASO's model of them, not a generic one.

Issues and contributions are welcome, as long as they fit IASO's end goals: emulating and verifying submissions as the form clients would, for IASO's forms. A change only another platform needs may not be accepted here.

But feel free to fork it (Apache-2.0): to run your own forms through it, to adapt it to another platform than IASO, or to drop what is IASO-specific.

Development

The rules a change must meet (complexity, parametrised cases, the native binary, test data) are in REVIEW.md; coding agents read AGENTS.md.

Setup

  • JDK 17, for the JVM build and the tests
  • Docker, to build the native binary (GraalVM, in the Dockerfile: nothing to install locally)
  • uv, for the scripts (scripts/*.py)

The code: src/com/bluesquare/iaso (odk_cli: Main, InstanceEditor), src/org/opendatakit/validate (from ODK Validate), the tests in src/test.

Build

./gradlew build                              # on the JVM: compiles, tests, checks
uv run scripts/release.py build dev          # the native binary: dist/dev/odk_cli-linux-amd64 (Docker, a few minutes)

or docker build -t odk_cli .: an image with the binary as /usr/local/bin/odk_cli, for another image to copy (COPY --from=odk_cli /usr/local/bin/odk_cli /usr/local/bin/odk_cli).

Test

Why every case runs twice, on the JVM and against the native binary. The code (Kotlin and Java) is tested on the JVM: fast, debuggable, with coverage and mutation testing. But what ships is the GraalVM native image, and it doesn't behave exactly like the JVM. Native Image compiles ahead of time only what it can see is reachable: anything looked up at runtime by name - reflection, ServiceLoader services, resources - is left out unless it is listed in resources/META-INF/native-image/ (reflect-config.json, resource-config.json...). Several dependencies work that way:

  • JCommander creates the option converters and splitters by reflection: a new option type (as --csv's WholeValue splitter) works on the JVM, and fails in the binary until the class is listed in reflect-config.json.
  • The XML parsers (DocumentBuilderFactory, XmlPullParserFactory), the time zone rules and the logging (SLF4J) are found through META-INF/services files, which must be listed in resource-config.json.
  • A new dependency, or a new version of one (JavaRosa...), can add such lookups.

Such a failure only shows in the binary, and only on the code path that uses it: hence the same cases against the binary, in scripts/test.sh and the CI. When one fails there and not on the JVM, run the case on the JVM with the GraalVM tracing agent to find what is missing, and add it to the config:

java -agentlib:native-image-agent=config-merge-dir=resources/META-INF/native-image -cp ... com.bluesquare.iaso.Main new -x ... -a ...

Running them:

./gradlew test                               # fast: every test on the JVM
./gradlew check                              # + checkstyle and the minimum coverage (JaCoCo)
scripts/test.sh [path/to/odk_cli]            # full: check, then the same cases against the native binary
  • The tests are parametrised cases, one row each: a form of src/test/resources, a saved submission or none, the answers, the expected XML or errors (InstanceEditor*Test), or a command line with its exit code and output (MainTest). With ODK_CLI_BINARY=path/to/odk_cli, the same cases run against the binary.
  • com.bluesquare.iaso.compatibility: what the engine (JavaRosa) does compared to ODK Collect (types, relevance, calculates, XPath functions...); ./gradlew test --tests 'com.bluesquare.iaso.compatibility.*'.
  • A case with bug = "..." is a known bug, skipped; RUN_BUGS=1 ./gradlew test runs them too.
  • ./gradlew pitest: mutation testing (about 30 s).
  • CI (GitHub Actions) runs check and the cases against the binary built by the Dockerfile.

The test report: scripts/test.sh ends by writing build/reports/tests/cases.html (and cases.md), every case readable without the code: its form as XLSForm tables, the answers, the expected XML or errors, its result on the JVM and on the binary, with filters (passed, failed, skipped). To publish it, after a full scripts/test.sh run on main (the binary built from it):

gh gist edit 9930b5471d2f468342d0c77c8dbaa2a6 -f cases.html build/reports/tests/cases.html

The gist keeps its link (the badge and the links of this README); update the counts and the date in them. For a report of your own (a branch), gh gist create build/reports/tests/cases.html, viewed at https://gistpreview.github.io/?<gist id>/cases.html.

Release

uv run scripts/release.py build 1.0.6        # dist/1.0.6/odk_cli-linux-amd64 and its .sha256
uv run scripts/release.py upload 1.0.6       # s3://blsq-io/odk-cli/1.0.6/ (AWS credentials needed)

upload checks the public URL and prints the ODK_CLI_URL / ODK_CLI_SHA256 lines to put in IASO's Dockerfiles. Then update the version in "Getting started".

Origins

odk-cli started as a fork of ODK Validate (Apache-2.0). It reuses Validate's code and, like Validate, relies on JavaRosa, the form engine of ODK Collect. Validate only checks that a form is compliant with the spec; odk-cli goes further and enters data into the form. The Validate code (org.opendatakit.validate.FormValidator) is still there, extended to validate a form with answers.

License

Apache License 2.0, see LICENSE.md. Based on ODK Validate, by the ODK contributors.

About

Validate ODK forms, fill and edit submissions as ODK Collect would - a fork of ODK Validate compiled with GraalVM

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages