Skip to content

Commit

Permalink
Merge pull request #1066 from samchon/features/unique
Browse files Browse the repository at this point in the history
Close #1062: add `UniqueItems` type tag.
  • Loading branch information
samchon committed Jun 3, 2024
2 parents e7fde2b + 76f6ed2 commit 962d332
Show file tree
Hide file tree
Showing 26 changed files with 411 additions and 27 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.0.4.tgz"
"typia": "../typia-6.0.5.tgz"
}
}
11 changes: 11 additions & 0 deletions debug/features/unique.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import typia, { tags } from "typia";

console.log(
JSON.stringify(
typia.json.application<[number[] & tags.UniqueItems]>(),
null,
2,
),
typia.is<number[] & tags.UniqueItems>([1, 2, 3]),
typia.is<number[] & tags.UniqueItems>([1, 2, 2]),
);
5 changes: 1 addition & 4 deletions debug/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const cp = require("child_process");
const fs = require("fs");
const path = require("path");
const runner = require("ts-node");
const supress = require("suppress-warnings");

Expand All @@ -19,9 +18,7 @@ const setup = () => {
cwd: __dirname,
stdio: "ignore",
});
mine.dependencies.typia = path.resolve(
`${__dirname}/../typia-${version}.tgz`,
);
mine.dependencies.typia = `../typia-${version}.tgz`;
fs.writeFileSync(`${__dirname}/package.json`, JSON.stringify(mine, null, 2));
cp.execSync("npm install", { cwd: __dirname, stdio: "ignore" });
};
Expand Down
2 changes: 1 addition & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"typescript": "^5.4.2"
},
"dependencies": {
"typia": "D:\\github\\samchon\\typia\\typia-6.0.3-dev.20240417.tgz"
"typia": "../typia-6.0.4.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-6.0.4.tgz"
"typia": "../typia-6.0.5.tgz"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "6.0.4",
"version": "6.0.5",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -16,7 +16,7 @@
"test": "npm run package:tgz",
"test:template": "npm run test template",
"-------------------------------------------------": "",
"build": "rimraf lib && tsc --declaration false && tsc --emitDeclarationOnly",
"build": "rimraf lib && tsc",
"dev": "rimraf lib && tsc --watch",
"eslint": "eslint ./**/*.ts",
"eslint:fix": "eslint ./**/*.ts --fix",
Expand Down Expand Up @@ -65,7 +65,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^0.1.21",
"@samchon/openapi": "0.1.22",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "6.0.4",
"version": "6.0.5",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -13,7 +13,7 @@
"test": "npm run package:tgz",
"test:template": "npm run test template",
"-------------------------------------------------": "",
"build": "rimraf lib && tsc --declaration false && tsc --emitDeclarationOnly",
"build": "rimraf lib && tsc",
"dev": "rimraf lib && tsc --watch",
"eslint": "eslint ./**/*.ts",
"eslint:fix": "eslint ./**/*.ts --fix",
Expand Down Expand Up @@ -62,7 +62,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "6.0.4"
"typia": "6.0.5"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.5.0"
Expand Down
20 changes: 18 additions & 2 deletions src/factories/MetadataCommentTagFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export namespace MetadataCommentTagFactory {
if (parser === undefined) return {};

const text = (comment.text || [])[0]?.text;
if (text === undefined) return report(`no comment tag value`);
return parser(report)(text);
if (text === undefined && comment.name !== "uniqueItems")
return report(`no comment tag value`);
return parser(report)(text!);
};
}

Expand Down Expand Up @@ -187,6 +188,21 @@ const PARSER: Record<
},
],
}),
uniqueItems: () => () => ({
array: [
{
name: `UniqueItems`,
target: "array",
kind: "uniqueItems",
value: true,
validate: `true === ($input.length === 0 || new Set($input).size === $input.length)`,
exclusive: true,
schema: {
uniqueItems: true,
},
},
],
}),

/* -----------------------------------------------------------
NUMBER
Expand Down
12 changes: 12 additions & 0 deletions src/tags/UniqueItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TagBase } from "./TagBase";

export type UniqueItems<Value extends boolean = true> = TagBase<{
target: "array";
kind: "uniqueItems";
value: Value;
validate: `${Value} === ($input.length === 0 || $input.length === new Set($input).size)`;
exclusive: true;
schema: {
uniqueItems: true;
};
}>;
5 changes: 3 additions & 2 deletions src/tags/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
export * from "./Constant";
export * from "./ContentMediaType";
export * from "./Default";
export * from "./ExclusiveMaximum";
export * from "./ExclusiveMinimum";
export * from "./Format";
export * from "./JsonSchemaPlugin";
export * from "./Maximum";
export * from "./MaxItems";
export * from "./MaxLength";
export * from "./Minimum";
export * from "./MinItems";
export * from "./MinLength";
export * from "./MultipleOf";
export * from "./JsonSchemaPlugin";
export * from "./Pattern";
export * from "./TagBase";
export * from "./Type";
export * from "./ContentMediaType";
export * from "./UniqueItems";
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.0.4.tgz"
"typia": "../typia-6.0.5.tgz"
}
}
10 changes: 9 additions & 1 deletion test/schemas/json/v3_0/CommentTagArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@
},
"minItems": 10,
"maxItems": 10
},
"unique": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"nullable": false,
"required": [
"items",
"minItems",
"both",
"equal"
"equal",
"unique"
]
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/schemas/json/v3_0/TypeTagArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@
},
"minItems": 10,
"maxItems": 10
},
"unique": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"nullable": false,
"required": [
"items",
"minItems",
"both",
"equal"
"equal",
"unique"
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/schemas/json/v3_0/UltimateUnion.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@
"items": {
"$ref": "#/components/schemas/OpenApi.IJsonSchema"
},
"uniqueItems": {
"type": "boolean"
},
"minItems": {
"type": "integer"
},
Expand Down Expand Up @@ -369,6 +372,9 @@
}
]
},
"uniqueItems": {
"type": "boolean"
},
"minItems": {
"type": "integer"
},
Expand Down
10 changes: 9 additions & 1 deletion test/schemas/json/v3_1/CommentTagArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@
},
"minItems": 10,
"maxItems": 10
},
"unique": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"required": [
"items",
"minItems",
"both",
"equal"
"equal",
"unique"
]
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/schemas/json/v3_1/TypeTagArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,21 @@
},
"minItems": 10,
"maxItems": 10
},
"unique": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
},
"required": [
"items",
"minItems",
"both",
"equal"
"equal",
"unique"
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/schemas/json/v3_1/UltimateUnion.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
"items": {
"$ref": "#/components/schemas/OpenApi.IJsonSchema"
},
"uniqueItems": {
"type": "boolean"
},
"minItems": {
"type": "integer"
},
Expand Down Expand Up @@ -342,6 +345,9 @@
}
]
},
"uniqueItems": {
"type": "boolean"
},
"minItems": {
"type": "integer"
},
Expand Down
1 change: 1 addition & 0 deletions test/schemas/protobuf/CommentTagArray.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ message CommentTagArray {
repeated double minItems = 2;
repeated string both = 3;
repeated double equal = 4;
repeated string unique = 5;
}
}
1 change: 1 addition & 0 deletions test/schemas/protobuf/TypeTagArray.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ message TypeTagArray {
repeated double minItems = 2;
repeated string both = 3;
repeated double equal = 4;
repeated string unique = 5;
}
}
Loading

0 comments on commit 962d332

Please sign in to comment.