Skip to content

Commit

Permalink
Merge pull request #1148 from dynamoose/nullTypeErrorMessage
Browse files Browse the repository at this point in the history
Null type error message improvements
  • Loading branch information
fishcharlie committed Mar 14, 2021
2 parents 809c716 + 156bd40 commit 8a2d056
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
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

1 comment on commit 8a2d056

@vercel
Copy link

@vercel vercel bot commented on 8a2d056 Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.