Skip to content

Commit

Permalink
Close #729 - @hidden tag for JSON schema
Browse files Browse the repository at this point in the history
> This is a feature requested by sponsor @windofwind.

When `@hidden` comment tag being used, let JSON schema generator to ignore that property.

Unlike `@internal` tag, it would not affect to other `typia` functions like `typia.assert<T>()`.

```typescript
import typia from "typia";

interface ISomething {
    id: string;

    /**
     * @hidden
     */
    authority: number;
}
const app: typia.IJsonApplication = typia.application<[ISomething], "ajv">();
const object: typia.IJsonComponents.IObject = (app.components.schemas ?? {})
    .ISomething as typia.IJsonComponents.IObject;
console.log(Object.keys(object.properties)); // ["id"]
```
  • Loading branch information
samchon committed Jul 31, 2023
1 parent 7312573 commit c0cf2aa
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 862 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "4.1.11",
"version": "4.1.12",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "4.1.11",
"version": "4.1.12",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.1.11"
"typia": "4.1.12"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
3 changes: 3 additions & 0 deletions src/programmers/internal/application_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ export const application_object =

for (const property of obj.properties) {
if (
// FUNCTIONAL TYPE
property.value.functional === true &&
property.value.nullable === false &&
property.value.isRequired() === true &&
property.value.size() === 0
)
continue;
else if (property.jsDocTags.find((tag) => tag.name === "hidden"))
continue; // THE HIDDEN TAG

const key: string | null = property.key.getSoleLiteral();
const schema: IJsonSchema | null = application_schema(options)(
Expand Down
28 changes: 28 additions & 0 deletions test/features/issues/test_issue_hidden_comment_tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import typia from "typia";

interface ISomething {
/**
* @format uuid
*/
id: string;

/**
* @hidden
*/
authority: number;
}

export const test_issue_hidden_comment_tag = () => {
const app: typia.IJsonApplication = typia.application<
[ISomething],
"ajv"
>();
const object: typia.IJsonComponents.IObject = (app.components.schemas ?? {})
.ISomething as typia.IJsonComponents.IObject;
const keys: string[] = Object.keys(object.properties);

if (keys.length !== 1 || keys[0] !== "id")
throw new Error(
"Bug on typia.application: failed to understand the hidden comment tag.",
);
};
25 changes: 25 additions & 0 deletions test/generated/output/issues/test_issue_hidden_comment_tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import typia from "typia";

interface ISomething {
/**
* @format uuid
*/
id: string;
/**
* @hidden
*/
authority: number;
}
export const test_issue_hidden_comment_tag = () => {
const app: typia.IJsonApplication = typia.application<
[ISomething],
"ajv"
>();
const object: typia.IJsonComponents.IObject = (app.components.schemas ?? {})
.ISomething as typia.IJsonComponents.IObject;
const keys: string[] = Object.keys(object.properties);
if (keys.length !== 1 || keys[0] !== "id")
throw new Error(
"Bug on typia.application: failed to understand the hidden comment tag.",
);
};
14 changes: 14 additions & 0 deletions test/issues/hidden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import typia from "typia";

interface ISomething {
id: string;

/**
* @hidden
*/
authority: number;
}
const app: typia.IJsonApplication = typia.application<[ISomething], "ajv">();
const object: typia.IJsonComponents.IObject = (app.components.schemas ?? {})
.ISomething as typia.IJsonComponents.IObject;
console.log(Object.keys(object.properties)); // ["id"]
Loading

0 comments on commit c0cf2aa

Please sign in to comment.