Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/typeschema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ export const mkTypeSchemaIndex = (
if (nonConstraintSchema?.fields) {
for (const [name, baseField] of Object.entries(nonConstraintSchema.fields)) {
if (!baseField.required) continue;
// Choice declarations (e.g. `value[x]`) are required via the
// declaration, not a real property — validateRequired() would
// check a key that never exists in FHIR JSON. The correct
// validateChoiceRequired() handling is deferred to the per-type
// validator redesign (#169); until then, skip rather than emit
// a check that can only misfire.
if (isChoiceDeclarationField(baseField)) continue;
const flat = flatFields[name] as { required?: boolean; min?: number } | undefined;
// Profile explicitly relaxed the field via differential min:0 →
// skip (regular validate emission also skips it because flatField
Expand Down
40 changes: 40 additions & 0 deletions test/unit/typeschema/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,5 +740,45 @@ describe("TypeSchema Index", () => {

expect(snap.inheritedRequiredFields).toEqual(["target", "recorded", "agent"]);
});

it("skips a base-required choice declaration the profile does not re-state", () => {
// A required choice (`value[x]`, 1..1) is satisfied via one of its
// variants, not a property literally named "value[x]". Listing it in
// inheritedRequiredFields would emit validateRequired("value[x]") —
// a check against a key that never exists in FHIR JSON. It must be
// skipped (proper validateChoiceRequired handling is tracked in #169).
const base: SpecializationTypeSchema = {
identifier: {
name: "Observationish" as Name,
package: "test",
kind: "resource",
version: "1.0.0",
url: "http://example.org/StructureDefinition/Observationish" as CanonicalUrl,
},
fields: {
recorded: { type: stringType, required: true, array: false },
"value[x]": { choices: ["valueString", "valueQuantity"], required: true },
},
};
const profile: ProfileTypeSchema = {
identifier: {
name: "ObservationishProfile" as Name,
package: "test",
kind: "profile",
version: "1.0.0",
url: "http://example.org/StructureDefinition/ObservationishProfile" as CanonicalUrl,
},
base: base.identifier,
fields: {},
};

const index = mkTypeSchemaIndex([base, profile], {});
const snap = index.collectSnapshotProfiles().find((s) => s.identifier.name === "ObservationishProfile");
if (!snap) throw new Error("snapshot not built");

// recorded is inherited; value[x] is a choice declaration → skipped.
expect(snap.inheritedRequiredFields).toEqual(["recorded"]);
expect(snap.inheritedRequiredFields).not.toContain("value[x]");
});
});
});
Loading