Select with dataSrc "custom": no options for the agent, and any value is accepted

Open
#46 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
mongodb, node.js, typescript
Domain
api, backend

Research direction

Start by locating the get_field_info and submit_completed_form tool handlers and the existing custom validation evaluation that uses isolated-vm. Trace how dataSrc: "values" exposes and validates options, then compare the custom select behavior described in the reproduction. Done means custom options are available to the agent and submissions with values outside those options are rejected, including when options depend on collected form data.

Written by the indexing model from the issue text.

Description

Environment: formio/uag:1.13.2 and formio/uag:2.0.0-rc.3 (same result), formio/formio Open Source server, MongoDB 7. The UAG runs with no custom module.

Summary

For a select with dataSrc: "custom" the agent gets no options, and the UAG accepts any value for the field.

Steps

Form reproSelect (tag uag) has a required select color with data.custom:

values = [{label: 'Red', value: 'red'}, {label: 'Blue', value: 'blue'}]
  1. get_field_info { form_name: "reproSelect", field_paths: ["color"] } lists the options as (No options available).
  2. submit_completed_form { form_name: "reproSelect", form_data: { color: "green" } } succeeds, and the submission is saved with color: "green", which is not one of the options.

Expected

  • The agent receives the options the form would show: Red (red) and Blue (blue).
  • A value outside the options is rejected, as with dataSrc: "values".

Proposal

  • Evaluate synchronous data.custom code on the server, with the form data collected so far. The UAG already evaluates custom validation code in isolated-vm.
  • The options often depend on other fields (for example the districts of the selected country), so they could be returned by a tool that takes form_data, or by get_field_info when form_data is passed.
  • Code that loads options asynchronously (for example a fetch followed by instance.setItems()) cannot run this way. The agent could be told that the options are not available, instead of receiving an empty list.

In our proof of concept we implemented this as a UAG module that runs the form with formio.js headless and adds a resolve_select_options tool. We can share the approach, or contribute it, if it fits your plans.

Reproduction: docker-compose.yml and repro.mjs (Node 18+, no dependencies)
services:
  mongo:
    image: mongo:7
  formio:
    image: formio/formio
    depends_on: [mongo]
    ports: ["3001:3001"]
    environment:
      NODE_CONFIG: '{"mongo": "mongodb://mongo:27017/uag-repro", "jwt": {"secret": "repro-jwt-secret"}, "mongoSecret": "repro-mongo-secret"}'
      ROOT_EMAIL: [email protected]
      ROOT_PASSWORD: repro-password
      ADMIN_KEY: repro-admin-key
  uag:
    image: formio/uag:${UAG_VERSION:-1.13.2}
    depends_on: [formio]
    restart: on-failure
    ports: ["3200:3200"]
    environment:
      PORT: 3200
      BASE_URL: http://localhost:3200
      PROJECT: http://formio:3001
      ADMIN_KEY: repro-admin-key
      JWT_SECRET: repro-jwt-secret
// Node 18+, no dependencies.
//   node repro.mjs setup   -> creates the forms on the Form.io server (run before starting the UAG)
//   node repro.mjs         -> calls the UAG MCP tools and prints the results
const FORMIO = 'http://localhost:3001', UAG = 'http://localhost:3200', KEY = 'repro-admin-key';
const H = { 'content-type': 'application/json', 'x-admin-key': KEY };

async function upsert(form) {
  const [existing] = await (await fetch(`${FORMIO}/form?name=${form.name}`, { headers: H })).json();
  const res = await fetch(existing ? `${FORMIO}/form/${existing._id}` : `${FORMIO}/form`,
    { method: existing ? 'PUT' : 'POST', headers: H, body: JSON.stringify({ ...existing, ...form }) });
  const body = await res.json();
  if (!res.ok) throw new Error(JSON.stringify(body));
  return body._id;
}

async function setup() {
  const required = (key, label) => ({ type: 'textfield', key, label, input: true, validate: { required: true } });
  const childId = await upsert({ title: 'Repro child', name: 'reproChild', path: 'reprochild', type: 'form', display: 'form',
    components: [required('firstName', 'First name')] });
  await upsert({ title: 'Repro nested', name: 'reproNested', path: 'repronested', type: 'form', display: 'form', tags: ['uag'],
    components: [required('subject', 'Subject'), { type: 'form', key: 'person', label: 'Person', input: true, form: childId },
      { type: 'button', key: 'submit', action: 'submit', input: true }] });
  await upsert({ title: 'Repro custom select', name: 'reproSelect', path: 'reproselect', type: 'form', display: 'form', tags: ['uag'],
    components: [{ type: 'select', key: 'color', label: 'Color', input: true, dataSrc: 'custom', validate: { required: true },
      data: { custom: "values = [{label: 'Red', value: 'red'}, {label: 'Blue', value: 'blue'}]" } },
      { type: 'button', key: 'submit', action: 'submit', input: true }] });
  // A custom validation that can only run in the browser (it calls a method of the component instance).
  await upsert({ title: 'Repro custom validation', name: 'reproValidation', path: 'reprovalidation', type: 'form', display: 'form', tags: ['uag'],
    components: [{ type: 'textfield', key: 'code', label: 'Code', input: true,
      validate: { required: true, custom: "valid = instance.checkCode() ? true : 'Invalid code'" } },
      { type: 'button', key: 'submit', action: 'submit', input: true }] });
  console.log(`Forms created. Nested form id: ${childId}`);
}

let token;
async function call(name, args) {
  token ??= (await (await fetch(`${UAG}/auth/token`, { method: 'POST', headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ grant_type: 'client_credentials', client_id: 'formio-oss-x-admin-key', client_secret: KEY }) })).json()).access_token;
  const res = await fetch(`${UAG}/mcp`, { method: 'POST',
    headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', authorization: `Bearer ${token}` },
    body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } }) });
  const raw = await res.text();
  const msg = JSON.parse(raw.startsWith('{') ? raw : raw.split('\n').find((l) => l.startsWith('data:')).slice(5));
  const text = msg.result ? msg.result.content.map((c) => c.text).join('\n') : JSON.stringify(msg.error);
  console.log(`\n--- ${name} ${JSON.stringify(args)}${msg.result?.isError ? '  [isError]' : ''}\n${text.split('\n').slice(0, 14).join('\n')}`);
}

async function repro() {
  console.log('==== Nested form ====');
  await call('get_form_fields', { form_name: 'reproNested' });
  await call('get_form_fields', { form_name: 'reproNested', parent_path: 'person' });
  await call('get_field_info', { form_name: 'reproNested', field_paths: ['person.data.firstName'] });
  await call('collect_field_data', { form_name: 'reproNested', form_data: { subject: 'Hello' }, parent_path: 'person',
    updates: [{ data_path: 'person.data.firstName', new_value: 'Mario' }] });
  await call('submit_completed_form', { form_name: 'reproNested', form_data: { subject: 'Hello', person: { data: { firstName: 'Mario' } } } });
  // The same payload posted straight to the Form.io server is accepted:
  const [form] = await (await fetch(`${FORMIO}/form?name=reproNested`, { headers: H })).json();
  const direct = await fetch(`${FORMIO}/form/${form._id}/submission`, { method: 'POST', headers: H,
    body: JSON.stringify({ data: { subject: 'Hello', person: { data: { firstName: 'Mario' } } } }) });
  console.log(`\n--- Direct POST to the Form.io server: HTTP ${direct.status}`);

  console.log('\n==== Custom select ====');
  await call('get_field_info', { form_name: 'reproSelect', field_paths: ['color'] });
  await call('submit_completed_form', { form_name: 'reproSelect', form_data: { color: 'green' } });

  console.log('\n==== Custom validation that throws ====');
  await call('submit_completed_form', { form_name: 'reproValidation', form_data: { code: 'anything' } });
}

await (process.argv[2] === 'setup' ? setup() : repro());
docker compose up -d mongo formio   # wait until http://localhost:3001 answers
node repro.mjs setup                # creates the forms; the UAG loads them at startup
docker compose up -d uag            # or: UAG_VERSION=2.0.0-rc.3 docker compose up -d uag
node repro.mjs
Dominant language
TypeScript
Stars
12
Forks
5
Avg merge
22h 47m
Merged PRs (30d)
15

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from formio/uag

All issues in formio/uag

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.