Skip to content

Commit

Permalink
Close #823 - ban Map types on JSON functions.
Browse files Browse the repository at this point in the history
When `JSON.stringify()` function be called about `Map<Key, T>` type, it always returns the empty bracket value `{}`. Until now, I'd developed `typia` to return the empty bracket value `{}`, following the standard `JSON.stringify()` function.

However, as most TypeScript/JavaScript developers do not know about that and there's not any problem when using the `Map` type on `typia.json.stringify<T>()` function ini the compile time, it was possible to misunderstand that `typia`'s stringify function has a bug about the `Map` time.

In such reason, I've decided to throw a compile error when such non-supported type being used in the `typia.json` functions. Also, such strategy would be helpful for `typia` users to avoid taking a mistake using such non-supported type on the JSON functions.
  • Loading branch information
samchon committed Oct 12, 2023
1 parent 105a4ae commit 96f9e65
Show file tree
Hide file tree
Showing 365 changed files with 569 additions and 42,884 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Check out the document in the [website](https://typia.io/docs/):
- [`decode()` functions](https://typia.io/docs/protobuf/decode/)
- [`encode()` functions](https://typia.io/docs/protobuf/encode/)
- [Random Generator](https://typia.io/docs/random/)
- [Miscellaneous](https://typia.io/docs/miscellaneous/)
- [Miscellaneous](https://typia.io/docs/misc/)

### 🔗 Appendix
- Utillization Cases
Expand Down
35 changes: 35 additions & 0 deletions errors/src/json/error_json_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import typia from "typia";

interface IPointer<T> {
value: T;
}

// JSON SCHEMA
typia.json.application<[Map<any, any>]>();
typia.json.application<[[Map<any, any>, Map<any, any>]]>();
typia.json.application<[Map<any, any>[]]>();
typia.json.application<[Record<string, Map<any, any>>]>();
typia.json.application<[IPointer<Map<any, any>>]>();
typia.json.application<[IPointer<[Map<any, any>, Map<any, any>]>]>();
typia.json.application<[IPointer<Map<any, any>[]>]>();
typia.json.application<[IPointer<Record<string, Map<any, any>>>]>();

// ENCODE
typia.json.createStringify<Map<any, any>>();
typia.json.createStringify<[Map<any, any>, Map<any, any>]>();
typia.json.createStringify<Map<any, any>[]>();
typia.json.createStringify<Record<string, Map<any, any>>>();
typia.json.createStringify<IPointer<Map<any, any>>>();
typia.json.createStringify<IPointer<[Map<any, any>, Map<any, any>]>>();
typia.json.createStringify<IPointer<Map<any, any>[]>>();
typia.json.createStringify<IPointer<Record<string, Map<any, any>>>>();

// DECODE
typia.json.createAssertParse<Map<any, any>>();
typia.json.createAssertParse<[Map<any, any>, Map<any, any>]>();
typia.json.createAssertParse<Map<any, any>[]>();
typia.json.createAssertParse<Record<string, Map<any, any>>>();
typia.json.createAssertParse<IPointer<Map<any, any>>>();
typia.json.createAssertParse<IPointer<[Map<any, any>, Map<any, any>]>>();
typia.json.createAssertParse<IPointer<Map<any, any>[]>>();
typia.json.createAssertParse<IPointer<Record<string, Map<any, any>>>>();
471 changes: 471 additions & 0 deletions errors/src/json/error_json_native.ts

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions errors/src/json/error_json_set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import typia from "typia";

interface IPointer<T> {
value: T;
}

// JSON SCHEMA
typia.json.application<[Set<any>]>();
typia.json.application<[[Set<any>, Set<any>]]>();
typia.json.application<[Set<any>[]]>();
typia.json.application<[Record<string, Set<any>>]>();
typia.json.application<[IPointer<Set<any>>]>();
typia.json.application<[IPointer<[Set<any>, Set<any>]>]>();
typia.json.application<[IPointer<Set<any>[]>]>();
typia.json.application<[IPointer<Record<string, Set<any>>>]>();

// ENCODE
typia.json.createStringify<Set<any>>();
typia.json.createStringify<[Set<any>, Set<any>]>();
typia.json.createStringify<Set<any>[]>();
typia.json.createStringify<Record<string, Set<any>>>();
typia.json.createStringify<IPointer<Set<any>>>();
typia.json.createStringify<IPointer<[Set<any>, Set<any>]>>();
typia.json.createStringify<IPointer<Set<any>[]>>();
typia.json.createStringify<IPointer<Record<string, Set<any>>>>();

// DECODE
typia.json.createAssertParse<Set<any>>();
typia.json.createAssertParse<[Set<any>, Set<any>]>();
typia.json.createAssertParse<Set<any>[]>();
typia.json.createAssertParse<Record<string, Set<any>>>();
typia.json.createAssertParse<IPointer<Set<any>>>();
typia.json.createAssertParse<IPointer<[Set<any>, Set<any>]>>();
typia.json.createAssertParse<IPointer<Set<any>[]>>();
typia.json.createAssertParse<IPointer<Record<string, Set<any>>>>();
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": "5.2.2-dev.20231012",
"version": "5.2.2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Check out the document in the [website](https://typia.io/docs/):
- [`decode()` functions](https://typia.io/docs/protobuf/decode/)
- [`encode()` functions](https://typia.io/docs/protobuf/encode/)
- [Random Generator](https://typia.io/docs/random/)
- [Miscellaneous](https://typia.io/docs/miscellaneous/)
- [Miscellaneous](https://typia.io/docs/misc/)

### 🔗 Appendix
- Utillization Cases
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": "5.2.2-dev.20231012",
"version": "5.2.2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "5.2.2-dev.20231012"
"typia": "5.2.2"
},
"peerDependencies": {
"typescript": ">= 4.8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type PrimitiveMain<Instance> = Instance extends [never]
: ValueOf<Instance> extends object
? Instance extends object
? Instance extends NativeClass
? {}
? never
: Instance extends IJsonable<infer Raw>
? ValueOf<Raw> extends object
? Raw extends object
Expand Down
7 changes: 7 additions & 0 deletions src/factories/JsonMetadataFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import ts from "typescript";

import { Metadata } from "../schemas/metadata/Metadata";

import { AtomicPredicator } from "../programmers/helpers/AtomicPredicator";

import { TransformerError } from "../transformers/TransformerError";

import { MetadataCollection } from "./MetadataCollection";
Expand Down Expand Up @@ -38,6 +40,11 @@ export namespace JsonMetadataFactory {
meta.arrays.some((a) => a.type.value.isRequired() === false)
)
output.push("JSON does not support undefined type in array.");
if (meta.maps.length) output.push("JSON does not support Map type.");
if (meta.sets.length) output.push("JSON does not support Set type.");
for (const native of meta.natives)
if (AtomicPredicator.native(native) === false && native !== "Date")
output.push(`JSON does not support ${native} type.`);
return output;
};
}
2 changes: 1 addition & 1 deletion src/programmers/helpers/AtomicPredicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export namespace AtomicPredicator {
!ArrayUtil.has(meta.atomics, (a) => a.type === "string");
}

const LIKE = new Set(["boolean", "number", "string"]);
const LIKE = new Set(["boolean", "bigint", "number", "string"]);

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 96f9e65

Please sign in to comment.