Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null type error message improvements #1148

Merged
merged 4 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Document.objectFromSchema = async function (object: any, model: Model<Document>,
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") {
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,5 +22,6 @@ export = {
array_flatten,
empty_function,
object,
dynamoose
dynamoose,
type_name
};
16 changes: 16 additions & 0 deletions lib/utils/type_name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 = "";
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;
};
5 changes: 5 additions & 0 deletions test/unit/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
];

Expand Down