Skip to content

Commit

Permalink
Remove redundant getErrorMessagesFromField + remove lodash from depen…
Browse files Browse the repository at this point in the history
…dencies
  • Loading branch information
Bartmr committed Sep 3, 2021
1 parent 491cff3 commit a6b3d9e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 43 deletions.
11 changes: 3 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "not-me",
"version": "4.1.0",
"version": "4.2.0",
"description": "Easy and type-safe validation",
"main": "lib/index.js",
"types": "lib/types.d.ts",
Expand Down Expand Up @@ -35,12 +35,9 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"lodash": "^4.17.21"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/lodash": "^4.14.171",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
Expand Down
30 changes: 0 additions & 30 deletions src/error-messages/error-messages-tree.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
import {
getTypesafeObjectFieldPath,
TypesafeObjectFieldPathPointer,
} from "../utils/get-typesafe-object-field-path";
import lodashGet from "lodash/get";

export type AnyErrorMessagesTree = Array<
string | { [key: string]: AnyErrorMessagesTree }
>;

export function getErrorMessagesFromField<Output>(
messagesTree: AnyErrorMessagesTree,
pathGetter: (pointer: TypesafeObjectFieldPathPointer<Output>) => string
): string[] | undefined {
const path = pathGetter({
...getTypesafeObjectFieldPath(messagesTree),
end: () => "",
});

let node: AnyErrorMessagesTree;

if (path === "") {
node = messagesTree;
} else {
node = lodashGet(messagesTree, path) as AnyErrorMessagesTree;
}

if (node instanceof Array) {
return node.filter((n): n is string => typeof n === "string");
} else {
return undefined;
}
}

0 comments on commit a6b3d9e

Please sign in to comment.