Skip to content

Fix boolean schema bundling by converting boolean root schemas before adding metadata (#139) - #143

Open
sivasurya05 wants to merge 2 commits into
hyperjump-io:mainfrom
sivasurya05:fix/139-boolean-schema-bundling
Open

sivasurya05 wants to merge 2 commits into
hyperjump-io:mainfrom
sivasurya05:fix/139-boolean-schema-bundling

Conversation

@sivasurya05

Copy link
Copy Markdown

Closes #139

Summary

Fixes a bug where bundling a schema that references an external false boolean schema flipped its validation behavior from rejecting everything to accepting everything.

Problem

toSchema() attaches identification ($id / id) and dialect ($schema) metadata using object spread (...schema). That breaks when schema is a plain boolean:

  • { ...false } throws away the boolean and leaves just { [idToken]: "..." }.
  • The rejection constraint disappears, so an always-failing schema quietly becomes an empty schema ({}) that accepts any input.

Solution

I followed the approach suggested by @jdesrosiers and @Kaileshwar16 in #139.

lib/schema.js

  • Added a convertBooleanSchema(schema, dialectId) helper:
    • true becomes {}.
    • false becomes { [notToken]: {} }. I used {} rather than true as the value of not. notToken is resolved for https://json-schema.org/keyword/not from the dialect via getKeywordName.
    • It throws a descriptive error if the dialect doesn't have the not keyword registered.
  • Kept the original control flow in toSchema(). The helper is only called right before spreading, when $id or $schema actually needs to be attached.
  • Guarded the definitionsToken cleanup with typeof schema === "object".
  • If neither $id nor $schema is added, bare boolean schemas (true / false) are returned unchanged.

Types (lib/experimental.d.ts)

  • Changed the toSchema return type to SchemaObject | boolean.
  • Added the optional isPersistent?: boolean parameter to loadDialect so the types match its runtime signature in lib/keywords.js.

Tests

  • lib/to-schema.spec.ts: New unit tests covering:
    • boolean conversion with $id and $schema
    • includeDialect: "always" | "never" | "auto"
    • contextUri
    • Draft-07 (legacy hash-suffixed $schema)
    • custom keywords aliasing not
    • bare booleans being preserved when no metadata is added
    • error handling when not isn't supported
  • bundle/boolean-schemas.spec.ts: New regression tests using the parameterized testRunner(dialect) pattern across all dialects that support boolean schemas (v1, draft-2020-12, draft-2019-09, draft-07, draft-06). They unregister the original schemas, register only the bundled schema, and confirm validation behavior is unchanged for both the URI and UUID naming strategies, including nested property references.

Verification

  • npx vitest run: passed (30/30 test files, 15,092 passed, 64 skipped)
  • npx eslint .: 0 errors, 0 warnings
  • npx tsc --noEmit: 0 errors

@Kaileshwar16

Kaileshwar16 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

@sivasurya05 The logic looks good, IMO I dont think the typeof schema === "object" check is needed here, since accessing or deleting properties on booleans is safe in JavaScript but it could be better for readbility. The tests could also be simplified a bit, but I will leave that to the maintainers to decide.

@sivasurya05

Copy link
Copy Markdown
Author

Thanks for taking a look, @Kaileshwar16!

On the typeof schema === "object" check, I added it so boolean schemas bail out early instead of creating a throwaway {} just to pass to Reflect.ownKeys(). It also keeps the types clean now that toSchema can return SchemaObject | boolean. Happy to drop it if @jdesrosiers would rather keep the diff minimal.

And yeah, the tests got a bit out of hand. I was checking every dialect and option combo while verifying the fix and kept adding cases. I can trim them down to the core ones if that's preferred.

@jdesrosiers jdesrosiers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The fix is right: an external false now bundles as { $id, not: {} } and still rejects.

The README still says toSchema returns object.

Comment thread lib/schema.js Outdated
if (options.contextUri) {
const relativeUri = toRelativeIri(normalizeIri(options.contextUri), browser.document.baseUri);
if (relativeUri !== "") {
schema = convertBooleanSchema(schema, browser.document.dialectId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than converting at all three places that add metadata, decide up front whether $id/$schema will be added and convert once.

Comment thread lib/schema.js Outdated

const definitionsToken = getKeywordName(browser.document.dialectId, "https://json-schema.org/keyword/definitions");
if (Reflect.ownKeys(schema[definitionsToken] ?? {}).length === 0) {
if (typeof schema === "object" && Reflect.ownKeys(schema[definitionsToken] ?? {}).length === 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard isn't needed. For a boolean the check is already 0 and the delete is a no-op.

Comment thread bundle/boolean-schemas.spec.ts Outdated
@@ -0,0 +1,177 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a case in bundle/tests/*.json instead? An entry with false/true in externalSchemas already runs every dialect with both naming strategies.

Comment thread lib/to-schema.spec.ts Outdated
const customDialectId = "https://example.com/custom-not-dialect";
const customVocabId = "https://example.com/custom-not-vocab";

defineVocabulary(customVocabId, {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This vocabulary is never removed.

Comment thread lib/experimental.d.ts
export const removeFormatHandler: (keywordUri: string, formatName: string) => void;
export const defineVocabulary: (id: string, keywords: Record<string, string>) => void;
export const loadDialect: (dialectId: string, dialect: Record<string, boolean>, allowUnknownKeywords?: boolean) => void;
export const loadDialect: (dialectId: string, dialect: Record<string, boolean>, allowUnknownKeywords?: boolean, isPersistent?: boolean) => void;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix, but it isn't related to #139. Please put it in its own commit.

sivasurya05 and others added 2 commits November 15, 2026 00:37
Match the runtime signature in lib/keywords.js.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… adding metadata (hyperjump-io#139)

toSchema() attached $id/$schema with object spread, which discarded a
boolean schema. An external `false` schema became `{ $id }` and started
accepting everything. Convert a boolean schema to an equivalent object
(`true` -> `{}`, `false` -> `{ not: {} }`) once, up front, whenever $id or
$schema will be added. Bare booleans are returned unchanged otherwise.

Update toSchema's return type and README, and cover the fix with a bundle
test-suite case plus toSchema unit tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sivasurya05
sivasurya05 force-pushed the fix/139-boolean-schema-bundling branch from 2dc9d41 to 472cbc8 Compare November 14, 2026 19:16
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.

Bundling an external false schema changes validation from rejecting all instances to accepting all instances

3 participants