Skip to content

Commit

Permalink
feat(frontend): slugify error-list paths
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Aug 8, 2023
1 parent 01c4542 commit daec422
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions packages/frontend/lib/globals/error-list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _camelToSnakeCase = (string) =>
string.replaceAll(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
import { slugify } from "@indiekit/util";

/**
* Transform errors provided by express-validator into array that can be
Expand All @@ -14,7 +13,7 @@ export const errorList = (errorMap) => {
for (const fieldError of fieldsWithErrors) {
errorList.push({
text: fieldError[1].msg,
href: `#${_camelToSnakeCase(fieldError[1].path)}`,
href: `#${slugify(fieldError[1].path, { decamelize: true })}`,
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/tests/unit/globals/error-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ test("Transforms errors provided by express-validator", (t) => {
me: {
value: "foo",
msg: "Enter a valid URL",
path: "customConfigUrl",
path: "fooBar[0].url",
location: "body",
},
});

t.is(result[0].href, "#custom-config-url");
t.is(result[0].href, "#foo-bar-0-url");
t.is(result[0].text, "Enter a valid URL");
});

0 comments on commit daec422

Please sign in to comment.