From 5516497f1f5f388ba929b8cef22433de342534a4 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 13 Mar 2021 17:41:54 -0700 Subject: [PATCH 1/4] Refactoring type error message --- lib/Document.ts | 2 +- lib/utils/index.ts | 4 +++- lib/utils/type_name.ts | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 lib/utils/type_name.ts diff --git a/lib/Document.ts b/lib/Document.ts index a9f8ae003..4311ca022 100644 --- a/lib/Document.ts +++ b/lib/Document.ts @@ -335,7 +335,7 @@ Document.objectFromSchema = async function (object: any, model: Model, if (existsInSchema) { const {isValidType, matchedTypeDetails, typeDetailsArray} = utils.dynamoose.getValueTypeCheckResult(schema, value, genericKey, settings, {"standardKey": true, typeIndexOptionMap}); if (!isValidType) { - throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${typeof value}${typeDetailsArray.some((val) => val.name === "Constant") ? ` (${value})` : ""}.`); + throw new Error.TypeMismatch(`Expected ${key} to be of type ${typeDetailsArray.map((detail) => detail.dynamicName ? detail.dynamicName() : detail.name.toLowerCase()).join(", ")}, instead found type ${utils.type_name(value, typeDetailsArray)}.`); } else if (matchedTypeDetails.isSet || matchedTypeDetails.name.toLowerCase() === "model") { validParents.push({key, "infinite": true}); } else if (/*typeDetails.dynamodbType === "M" || */matchedTypeDetails.dynamodbType === "L") { diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 7ccb1895c..21ac91439 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -9,6 +9,7 @@ import empty_function = require("./empty_function"); import object = require("./object"); import dynamoose = require("./dynamoose"); import all_elements_match from "./all_elements_match"; +import type_name from "./type_name"; export = { combine_objects, @@ -21,5 +22,6 @@ export = { array_flatten, empty_function, object, - dynamoose + dynamoose, + type_name }; diff --git a/lib/utils/type_name.ts b/lib/utils/type_name.ts new file mode 100644 index 000000000..50d0dba71 --- /dev/null +++ b/lib/utils/type_name.ts @@ -0,0 +1,6 @@ +import { DynamoDBSetTypeResult, DynamoDBTypeResult } from "../Schema"; + +// This function takes in a value and returns a user string for the type of that value. This function is mostly used to display type errors to users. +export default (value: any, typeDetailsArray: (DynamoDBTypeResult | DynamoDBSetTypeResult)[]): string => { + return `${typeof value}${typeDetailsArray.some((val) => val.name === "Constant") ? ` (${value})` : ""}`; +}; From 9cf6eaed2ab4466b409c23d2ff587931eb1188f3 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 13 Mar 2021 17:43:46 -0700 Subject: [PATCH 2/4] Using null name for type error instead of object --- lib/utils/type_name.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/utils/type_name.ts b/lib/utils/type_name.ts index 50d0dba71..fd1b69f40 100644 --- a/lib/utils/type_name.ts +++ b/lib/utils/type_name.ts @@ -2,5 +2,15 @@ import { DynamoDBSetTypeResult, DynamoDBTypeResult } from "../Schema"; // This function takes in a value and returns a user string for the type of that value. This function is mostly used to display type errors to users. export default (value: any, typeDetailsArray: (DynamoDBTypeResult | DynamoDBSetTypeResult)[]): string => { - return `${typeof value}${typeDetailsArray.some((val) => val.name === "Constant") ? ` (${value})` : ""}`; + let str: string = ""; + if (value === null) { + str += "null"; + } else { + str += typeof value; + } + + // Add constant value to type name + str += typeDetailsArray.some((val) => val.name === "Constant") ? ` (${value})` : ""; + + return str; }; From 7049d3964adbcf81eaa6d6b2b5fe2cee94a652aa Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 13 Mar 2021 17:43:53 -0700 Subject: [PATCH 3/4] Adding test for using null name for type error instead of object --- test/unit/Document.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/unit/Document.js b/test/unit/Document.js index 10e4cb09a..bf34b9593 100644 --- a/test/unit/Document.js +++ b/test/unit/Document.js @@ -2750,6 +2750,11 @@ describe("Document", () => { return {"id": Number, "data": [{"type": Set, "schema": [Item]}, String]}; }, "error": new Error.ValidationError("Expected data to be of type Item Set, string, instead found type boolean.") + }, + { + "input": [{"id": 1, "data": null}, {"type": "toDynamo"}], + "schema": {"id": Number, "data": String}, + "error": new Error.ValidationError("Expected data to be of type string, instead found type null.") } ]; From 156bd4090c3280973170f06fab4725bf1858c291 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 13 Mar 2021 17:45:26 -0700 Subject: [PATCH 4/4] Lint fixes --- lib/utils/type_name.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/type_name.ts b/lib/utils/type_name.ts index fd1b69f40..f9c247bfc 100644 --- a/lib/utils/type_name.ts +++ b/lib/utils/type_name.ts @@ -1,8 +1,8 @@ -import { DynamoDBSetTypeResult, DynamoDBTypeResult } from "../Schema"; +import {DynamoDBSetTypeResult, DynamoDBTypeResult} from "../Schema"; // This function takes in a value and returns a user string for the type of that value. This function is mostly used to display type errors to users. export default (value: any, typeDetailsArray: (DynamoDBTypeResult | DynamoDBSetTypeResult)[]): string => { - let str: string = ""; + let str = ""; if (value === null) { str += "null"; } else {