Repository navigation
Fix boolean schema bundling by converting boolean root schemas before adding metadata (#139) - #143
sivasurya05 wants to merge 2 commits into
Conversation
|
@sivasurya05 The logic looks good, IMO I dont think the |
|
Thanks for taking a look, @Kaileshwar16! On the 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
left a comment
There was a problem hiding this comment.
Thanks! The fix is right: an external false now bundles as { $id, not: {} } and still rejects.
The README still says toSchema returns object.
| if (options.contextUri) { | ||
| const relativeUri = toRelativeIri(normalizeIri(options.contextUri), browser.document.baseUri); | ||
| if (relativeUri !== "") { | ||
| schema = convertBooleanSchema(schema, browser.document.dialectId); |
There was a problem hiding this comment.
Rather than converting at all three places that add metadata, decide up front whether $id/$schema will be added and convert once.
|
|
||
| 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) { |
There was a problem hiding this comment.
This guard isn't needed. For a boolean the check is already 0 and the delete is a no-op.
| @@ -0,0 +1,177 @@ | |||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | |||
There was a problem hiding this comment.
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.
| const customDialectId = "https://example.com/custom-not-dialect"; | ||
| const customVocabId = "https://example.com/custom-not-vocab"; | ||
|
|
||
| defineVocabulary(customVocabId, { |
There was a problem hiding this comment.
This vocabulary is never removed.
| 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; |
There was a problem hiding this comment.
Good fix, but it isn't related to #139. Please put it in its own commit.
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>
2dc9d41 to
472cbc8
Compare
Closes #139
Summary
Fixes a bug where bundling a schema that references an external
falseboolean 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 whenschemais a plain boolean:{ ...false }throws away the boolean and leaves just{ [idToken]: "..." }.{}) that accepts any input.Solution
I followed the approach suggested by @jdesrosiers and @Kaileshwar16 in #139.
lib/schema.jsconvertBooleanSchema(schema, dialectId)helper:truebecomes{}.falsebecomes{ [notToken]: {} }. I used{}rather thantrueas the value ofnot.notTokenis resolved forhttps://json-schema.org/keyword/notfrom the dialect viagetKeywordName.notkeyword registered.toSchema(). The helper is only called right before spreading, when$idor$schemaactually needs to be attached.definitionsTokencleanup withtypeof schema === "object".$idnor$schemais added, bare boolean schemas (true/false) are returned unchanged.Types (
lib/experimental.d.ts)toSchemareturn type toSchemaObject | boolean.isPersistent?: booleanparameter toloadDialectso the types match its runtime signature inlib/keywords.js.Tests
lib/to-schema.spec.ts: New unit tests covering:$idand$schemaincludeDialect: "always" | "never" | "auto"contextUri$schema)notnotisn't supportedbundle/boolean-schemas.spec.ts: New regression tests using the parameterizedtestRunner(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 theURIandUUIDnaming strategies, including nested property references.Verification
npx vitest run: passed (30/30 test files, 15,092 passed, 64 skipped)npx eslint .: 0 errors, 0 warningsnpx tsc --noEmit: 0 errors