Skip to content

Commit

Permalink
feat: support @required tag infer
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 14, 2022
1 parent 139b65d commit d9d5084
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ async function _resolveSchema(
}
schema.properties = schema.properties || {};
if (!schema.properties[key]) {
schema.properties[key] = await _resolveSchema(
const child = (schema.properties[key] = await _resolveSchema(
node[key],
joinPath(id, key),
ctx
);
));

// Infer @required
if (Array.isArray(child.tags) && child.tags.includes("@required")) {
schema.required = schema.required || [];
if (!schema.required.includes(key)) {
schema.required.push(key);
}
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,17 @@ describe("resolveSchema", () => {
},
});
});

it("Handle @required tag", async () => {
const schema = await resolveSchema({
some: {
prop: {
$schema: {
tags: ["@required"],
},
},
},
});
expect((schema as any).properties.some.required).toMatchObject(["prop"]);
});
});

0 comments on commit d9d5084

Please sign in to comment.