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
14 changes: 14 additions & 0 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,20 @@ function processMapHelper(
const innerItems = resolveArrayItems(effectiveItemSchema, ctx.root);
if (innerItems) {
effectiveItemSchema = innerItems;
// Emit an informational warning so consumers know the flatten happened
addDiagnostic(
ctx,
"MAP_IMPLICIT_FLATTEN",
"warning",
`The "${helperName}" helper will automatically flatten the input array one level before mapping. ` +
`The item type "${Array.isArray(itemType) ? itemType.join(" | ") : itemType}" was unwrapped to its inner items schema.`,
stmt,
{
helperName,
expected: "object",
actual: schemaTypeLabel(effectiveItemSchema),
},
);
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/helpers/map-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ import { HelperFactory } from "./helper-factory.ts";
/**
* Extracts a property from each element of an array.
*
* @param array - The array of objects
* **Note:** The input array is automatically flattened one level (`flat(1)`)
* before mapping. This is designed to support multi-node outputs and chained
* map calls which produce arrays of arrays. If your data is already flat,
* this has no effect.
*
* @example
* // Input: [[{name:"a"},{name:"b"}],[{name:"c"}]]
* // After flat(1): [{name:"a"},{name:"b"},{name:"c"}]
* // Result: ["a","b","c"]
*
* @param array - The array of objects (will be flattened one level)
* @param property - The property name to extract from each element
* @returns A new array containing the extracted property values
*/
Expand All @@ -46,7 +56,12 @@ function mapProperty(array: unknown, property: unknown): unknown[] {
// where the inner map returns an array of arrays.
const flattened = array.flat(1);
return flattened.map((item: unknown) => {
if (item !== null && item !== undefined && typeof item === "object") {
if (
item !== null &&
item !== undefined &&
typeof item === "object" &&
!Array.isArray(item)
) {
return (item as Record<string, unknown>)[prop];
}
return undefined;
Expand Down
87 changes: 79 additions & 8 deletions src/schema-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ export function resolveArrayItems(
* allocations).
*/
export function simplifySchema(schema: JSONSchema7): JSONSchema7 {
// oneOf / anyOf with a single element → unwrap
// ── Phase 1 : Unwrap single-branch combinators ──────────────────────

for (const key of ["oneOf", "anyOf"] as const) {
const arr = schema[key];
if (arr && arr.length === 1) {
Expand All @@ -512,34 +513,104 @@ export function simplifySchema(schema: JSONSchema7): JSONSchema7 {
}
}

// allOf with a single element → unwrap
if (schema.allOf && schema.allOf.length === 1) {
const first = schema.allOf[0];
if (first !== undefined && typeof first !== "boolean")
return simplifySchema(first);
}

// Deduplicate identical entries in oneOf/anyOf
// ── Phase 2 : Deduplicate multi-branch oneOf/anyOf ──────────────────

let result: JSONSchema7 = schema;

for (const key of ["oneOf", "anyOf"] as const) {
const arr = schema[key];
const arr = result[key];
if (arr && arr.length > 1) {
const unique: JSONSchema7[] = [];
for (const entry of arr) {
if (typeof entry === "boolean") continue;
// Use deepEqual instead of JSON.stringify for structural
// comparison — more robust (key order independent) and
// more performant (no string allocations).
const simplified = simplifySchema(entry);
const isDuplicate = unique.some((existing) =>
deepEqual(existing, entry),
deepEqual(existing, simplified),
);
if (!isDuplicate) {
unique.push(simplifySchema(entry));
unique.push(simplified);
}
}
if (unique.length === 1) return unique[0] as JSONSchema7;
return { ...schema, [key]: unique };
result = { ...result, [key]: unique };
}
}

// ── Phase 3 : Recurse into allOf branches (multi-branch) ────────────

if (result.allOf && result.allOf.length > 1) {
const simplifiedBranches = result.allOf.map((branch) => {
if (typeof branch === "boolean" || branch === undefined) return branch;
return simplifySchema(branch);
});
result = { ...result, allOf: simplifiedBranches };
}

// ── Phase 4 : Recurse into properties ───────────────────────────────

if (result.properties) {
const simplifiedProps: Record<string, JSONSchema7> = {};
let changed = false;
for (const [key, prop] of Object.entries(result.properties)) {
if (prop && typeof prop !== "boolean") {
const simplified = simplifySchema(prop);
simplifiedProps[key] = simplified;
if (simplified !== prop) changed = true;
} else {
simplifiedProps[key] = prop as unknown as JSONSchema7;
}
}
if (changed) {
result = { ...result, properties: simplifiedProps };
}
}

// ── Phase 5 : Recurse into items ────────────────────────────────────

if (result.items) {
if (Array.isArray(result.items)) {
// Tuple items
let itemsChanged = false;
const simplifiedItems = result.items.map((item) => {
if (item && typeof item !== "boolean") {
const simplified = simplifySchema(item);
if (simplified !== item) itemsChanged = true;
return simplified;
}
return item;
});
if (itemsChanged) {
result = { ...result, items: simplifiedItems };
}
} else if (typeof result.items !== "boolean") {
// Single items schema
const simplified = simplifySchema(result.items);
if (simplified !== result.items) {
result = { ...result, items: simplified };
}
}
}

// ── Phase 6 : Recurse into additionalProperties ─────────────────────

if (
result.additionalProperties &&
typeof result.additionalProperties === "object"
) {
const simplified = simplifySchema(result.additionalProperties);
if (simplified !== result.additionalProperties) {
result = { ...result, additionalProperties: simplified };
}
}

return schema;
return result;
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ export type DiagnosticCode =
/** The $root token is used with path traversal (e.g. $root.name) */
| "ROOT_PATH_TRAVERSAL"
/** Unsupported JSON Schema feature (e.g. if/then/else conditional schemas) */
| "UNSUPPORTED_SCHEMA";
| "UNSUPPORTED_SCHEMA"
/** The map helper implicitly flattens the input array one level before mapping */
| "MAP_IMPLICIT_FLATTEN";

// ─── Diagnostic Details ──────────────────────────────────────────────────────
// Supplementary information to understand the exact cause of the error.
Expand Down
125 changes: 122 additions & 3 deletions tests/map-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ describe("map helper", () => {
nestedData,
);
expect(analysis.valid).toBe(true);
expect(analysis.diagnostics).toHaveLength(0);
// The outer map emits a MAP_IMPLICIT_FLATTEN warning because the
// inner map returns an array-of-arrays that gets flattened.
const errors = analysis.diagnostics.filter((d) => d.severity === "error");
expect(errors).toHaveLength(0);
expect(value).toEqual(["p1", "p2", "p3"]);
});

Expand All @@ -780,7 +783,9 @@ describe("map helper", () => {
nestedSchema,
);
expect(result.valid).toBe(true);
expect(result.diagnostics).toHaveLength(0);
// The outer map emits a MAP_IMPLICIT_FLATTEN warning (not an error)
const errors = result.diagnostics.filter((d) => d.severity === "error");
expect(errors).toHaveLength(0);
expect(result.outputSchema).toEqual({
type: "array",
items: { type: "string" },
Expand All @@ -794,7 +799,9 @@ describe("map helper", () => {
nestedSchema,
);
expect(result.valid).toBe(true);
expect(result.diagnostics).toHaveLength(0);
// The outer map emits a MAP_IMPLICIT_FLATTEN warning (not an error)
const errors = result.diagnostics.filter((d) => d.severity === "error");
expect(errors).toHaveLength(0);
expect(result.outputSchema).toEqual({
type: "array",
items: { type: "number" },
Expand Down Expand Up @@ -856,4 +863,116 @@ describe("map helper", () => {
expect(error).toBeDefined();
});
});

// ─── flat(1) behavior documentation ──────────────────────────────────────

describe("map helper — flat(1) behavior", () => {
test("should map properties from flat array of objects", () => {
const tp = new Typebars();
const result = tp.execute('{{map users "name"}}', {
users: [{ name: "Alice" }, { name: "Bob" }],
});
expect(result).toEqual(["Alice", "Bob"]);
});

test("should flatten one level before mapping (current behavior)", () => {
const tp = new Typebars();
const result = tp.execute('{{map groups "name"}}', {
groups: [[{ name: "Alice" }, { name: "Bob" }], [{ name: "Charlie" }]],
});
// flat(1) produces [{name:"Alice"},{name:"Bob"},{name:"Charlie"}]
expect(result).toEqual(["Alice", "Bob", "Charlie"]);
});

test("should NOT flatten more than one level", () => {
const tp = new Typebars();
const result = tp.execute('{{map deepNested "name"}}', {
deepNested: [[[{ name: "Alice" }]]],
});
// flat(1) produces [[{name:"Alice"}]]
// The inner array is not a plain object → undefined
expect(result).toEqual([undefined]);
});

test("should return undefined for non-object items after flatten", () => {
const tp = new Typebars();
const result = tp.execute('{{map mixed "name"}}', {
mixed: [{ name: "Alice" }, "not-an-object", null, { name: "Bob" }],
});
expect(result).toEqual(["Alice", undefined, undefined, "Bob"]);
});

test("should return undefined for array items (not treated as objects)", () => {
const tp = new Typebars();
const result = tp.execute('{{map items "name"}}', {
items: [{ name: "Alice" }, [1, 2, 3], { name: "Bob" }],
});
// [1,2,3] is an array, not a plain object → after flat(1) it becomes 1, 2, 3
// 1, 2, 3 are not objects → undefined
expect(result).toEqual(["Alice", undefined, undefined, undefined, "Bob"]);
});
});

// ─── MAP_IMPLICIT_FLATTEN diagnostic ─────────────────────────────────────

describe("map helper — MAP_IMPLICIT_FLATTEN diagnostic", () => {
test("should emit MAP_IMPLICIT_FLATTEN warning when items are arrays", () => {
const tp = new Typebars();
const schema: JSONSchema7 = {
type: "object",
properties: {
groups: {
type: "array",
items: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
},
},
},
},
required: ["groups"],
};
const result = tp.analyze('{{map groups "name"}}', schema);
// Analysis should still succeed (the flatten is valid)
expect(result.valid).toBe(true);
// But a MAP_IMPLICIT_FLATTEN warning should be emitted
const warning = result.diagnostics.find(
(d) => d.code === "MAP_IMPLICIT_FLATTEN",
);
expect(warning).toBeDefined();
expect(warning?.severity).toBe("warning");
expect(warning?.message).toContain("automatically flatten");
});

test("should NOT emit MAP_IMPLICIT_FLATTEN for flat array of objects", () => {
const tp = new Typebars();
const schema: JSONSchema7 = {
type: "object",
properties: {
users: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
},
},
},
required: ["users"],
};
const result = tp.analyze('{{map users "name"}}', schema);
expect(result.valid).toBe(true);
const warning = result.diagnostics.find(
(d) => d.code === "MAP_IMPLICIT_FLATTEN",
);
expect(warning).toBeUndefined();
});
});
});
Loading
Loading