Repository navigation
Conversation
…ey and value types
Map properties were emitted as { schema: {...} }, which validators ignore, so map values were never validated. Map keys and values now go through the same schema helper as fields: scalars contribute their base type and validators (regex.source, uuid and date-time formats), and class values are referenced by their own fully qualified name under refRoot. Fixes accordproject#292
Signed-off-by: Ujjwal Verma <ujjwalverma010305@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #292
For a property whose type is a map,
JSONSchemaVisitorreturned the map's schema wrapped in aschemakey.schemaisn't a JSON Schema keyword, so validators ignored it and map properties accepted any value, including a plain string. Strict Ajv rejected the schema outright (unknown keyword: "schema").Removing the wrapper means map schemas are evaluated for the first time, which showed three more bugs in the same branch that the wrapper had been hiding:
map … { o String o SSN }){ "type": "SSN" }: the scalar's name as a JSON type, an invalid schema{ "type": "string", "pattern": "…" }, the scalar's base type and validators"pattern": "/(\\d{3}-\\d{2}-\\d{4})+/": the delimiters are part of the pattern, so valid SSNs would be rejected"pattern": "(\\d{3}-\\d{2}-\\d{4})+"(regex.source, as for fields)$refbuilt from the field's namespace, e.g.org.acme.hr@1.0.0.TShirtSizeTypeandorg.acme.hr@1.0.0.Concept, which don't existorg.acme.hr.base@1.0.0.TShirtSizeType,concerto@1.0.0.Concept)refRootset (asOpenApiVisitordoes)#/definitions/…<refRoot>/…, like other referencesWithout these fixes, removing the wrapper would make
hr_integration's schema fail to compile and make valid data fail validation, so they belong in the same change.Changes
lib/codegen/fromcto/jsonschema/jsonschemavisitor.js: the map branch ofvisitFieldreturns the map schema itself. Its key and value schemas come from a new privategetMapEntrySchema(), which uses the same helper as fields for primitives and scalars (base type,regex.source, length and range validators,uuidanddate-timeformats), and references class and enum values by their fully qualified name underrefRoot.test/codegen/fromcto/jsonschema/jsonschemavisitor.js):.schema;uuid,date-time), checked by validating instances;#/definitionsand under a customrefRoot;getFieldOrScalarDeclarationValidatorsForSchemafor an unrecognised type name. Only the scalar-name bug used to reach it, so it would otherwise lose coverage.main.test/codegen/__snapshots__/codegen.js.snap: regenerated. The diff is the 18 removed wrappers, 6 patterns without delimiters,"SSN"→"string", and the corrected$refs.test/verification/cases.js: two OpenAPI skip reasons updated to what still fails (see Flags).Flags
propertyNames, which map key schemas use. So thecollectionsizeOpenAPI case stays skipped, with an updated reason. (My issue comment said this fix would unblock it; that was wrong, sorry.) Forhr_integration, the invalid-typeerrors go from 5 to 0 and unresolved$refs from 3 to 1;$decoratorsandpropertyNamesremain. Both are OpenAPI-specific and out of scope here.npm testreports branch coverage at 95.90%, below the 96% threshold, butmainis at 95.91% locally for the same reason: without the Go/Java/Rust toolchains the verification tests are skipped, and fix(jsonschema): support non-URL $id values #285 left a branch infromJsonSchema/cto/jsonSchemaVisitor.js(line 217) untested. CI has the toolchains. I can add a test for that in a separate PR.Screenshots or Video
N/A
Related Issues
Author Checklist
--signoffoption of git commit.mainfromfork:fix/jsonschema-map-schemaTesting (Node 24, Windows 11):
npm test671 passing, 80 pending (toolchains not installed), 0 failing; lint, licchk and jsdoc pass;npm run test:esm7/7. Thehr_integrationJSON Schema verification, which compiles the HR model with Ajv, passes with the fix and fails if only the wrapper is removed.