Skip to content

Validate object-typed fields structurally in schema validation - #2458

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2440-object-field-type-validation
Sep 8, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2440-object-field-type-validation

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

validate_node_with_fields special-cased every declared field_type except enum — a field declared object (or an array field declared itemType: "object") accepted any JSON value and stored it as-is, with no error and no warning.

Scope decision

Per the issue's "scope to determine" question, this closes the object-shape gap specifically, not every declared field_type:

  • A field declared object must hold a JSON object (or explicit null, mirroring the existing enum-field null allowance).
  • A field declared array with itemType: "object" must hold an array whose every element is a JSON object.

Widening to every declared type (string/number/boolean/date) is out of scope here — that requires its own writer survey, and the issue itself warns this could repeat the ai-chat.status incident (PR #2436), where enabling enum validation broke 16 daemon tests because the schema and its writers had already drifted apart. This fix is deliberately narrower and lower-risk.

Deliberately not recursive: a nested object field declared via fields/item_fields (e.g. ai-chat.messages[].args, left without declared sub-fields on purpose since tool-call arguments are freeform) is not walked into — validate_node_with_fields only ever sees a type's top-level fields list.

Survey

Every declared object/array<object> field in core_schemas.rs — ai-chat.messages (item type object), query.filters, query.sorting — was checked against its writers. No writer contradicts its declared shape: daemon writes to messages always send well-formed arrays of message objects, and query writes always send arrays of filter/sort objects. So enabling this check needed no accompanying writer fix.

Changes

  • packages/core/src/services/node_service/crud.rs: validate_node_with_fields now rejects a wrongly-shaped object/array<object> value with a message naming the field, the declared type, and what was received.
  • packages/core/src/schema/mod.rs: json_type_name made pub(crate) so both call sites share one type-name formatter instead of duplicating it.
  • packages/core/tests/object_field_type_validation_test.rs: new coverage — rejects a string/number where object is declared, accepts a real object, accepts explicit null, rejects a non-array and a non-object array item where array<object> is declared, accepts a correct array of objects, and confirms the update path enforces the same rule as create.

Test plan

  • cargo test -p nodespace-core --test object_field_type_validation_test — 8/8 passing
  • Every new test individually mutation-tested (code path disabled/inverted, confirmed the specific test fails, restored, confirmed it passes again)
  • cargo test -p nodespace-core — full crate suite green (1259 passed)
  • bun run test:all — green
  • bun run quality:fix — clean (no lint/format changes needed)

Closes #2440

🤖 Generated with Claude Code

mstomar125 and others added 2 commits September 8, 2026 10:32
validate_node_with_fields checked every declared field_type except
enum, so a field declared "object" (or an array field declared
itemType: "object") accepted any JSON shape with no error.

Scoped to the object gap specifically, not every declared field_type:
a field declared object must now hold a JSON object (or explicit
null), and an array field with itemType "object" must hold an array
whose every element is a JSON object. Widening to every declared type
was considered and rejected for this change — that requires its own
writer survey, and enabling it blind risks repeating the ai-chat.status
incident where schema and writers had already drifted apart.

A survey of every writer of a declared object/array<object> field in
core_schemas.rs (ai-chat.messages, query.filters, query.sorting) found
no contradicting writer, so no writer-side fix accompanies this.

Co-Authored-By: Claude <[email protected]>
Adds test coverage for the gaps flagged in review of PR #2458:
- required + explicit null (documents that the pre-existing required
  check only tests key-absence, so null on a required object field is
  currently accepted — not fixed here, pinned as-is)
- required + entirely missing (pins the pre-existing required check
  still fires correctly alongside the new type check)
- an array item that is itself an array, not just a scalar
- an empty array (vacuous pass)
- a wrongly-typed schema default now bricking creation for every node
  of that type that omits the field, once the bad default is applied
  and then rejected by the new check (documented, not fixed — no
  shipped core schema hits this)

Also strengthens two under-asserted tests to check the declared-type
and received-type text like their siblings do, and corrects a doc
comment that overstated validate_node_against_schema as a separate
implementation rather than a thin wrapper around the same check.

Co-Authored-By: Claude <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Addressed the adversarial review's findings in a follow-up commit:

  • Added 5 tests: required+null (documents the loophole — not fixed, matches the pre-existing enum precedent), required+missing (pins the pre-existing required check), a nested-array item, an empty array, and a wrongly-typed schema default now bricking creation for the type (documented, not fixed — no shipped core schema hits it).
  • Strengthened two under-asserted tests to check declared/received type text like their siblings.
  • Corrected a doc comment that overstated validate_node_against_schema as a separate implementation rather than a thin wrapper around the same check.

All 13 tests in the file individually mutation-tested (code path disabled/inverted, confirmed the specific test fails, restored). Full cargo test -p nodespace-core (1259+ passing) and bun run test:all green. No changes to the reviewed implementation itself — review found no critical issues in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

field_type is declared but never validated except for enum, so an object field accepts any value

1 participant