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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nonOptionalErrorMsg props #757

Merged
merged 9 commits into from
Nov 11, 2023
10 changes: 10 additions & 0 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ const incorrectFormFields = [
return "Please check Terms and conditions";
},
},
{
id: "city",
label: "Your city",
optional: false,
nonOptionalErrorMsg: "", // empty string should throw error
},
];

const customFields = [
Expand Down Expand Up @@ -713,6 +719,10 @@ function getFormFields() {
// since page-error blocks all the other errors
// use this filter to test specific error
return incorrectFormFields.filter(({ id }) => id === "terms");
} else if (localStorage.getItem("INCORRECT_NON_OPTIONAL_ERROR_MSG") === "YES") {
return incorrectFormFields.filter(({ id }) => id === "city");
} else if (localStorage.getItem("INCORRECT_GETDEFAULT") === "YES") {
return incorrectFormFields.filter(({ id }) => id === "country");
}
return incorrectFormFields;
} else if (localStorage.getItem("SHOW_CUSTOM_FIELDS_WITH_DEFAULT_VALUES") === "YES") {
Expand Down
6 changes: 5 additions & 1 deletion lib/build/emailpassword-shared4.js

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

6 changes: 5 additions & 1 deletion lib/ts/recipe/emailpassword/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,16 @@ export function mergeFormFields(
}

export function getFormattedFormField(field: NormalisedFormField): NormalisedFormField {
// Fields with the 'nonOptionalErrorMsg' property must have a valid message defined
if (field.optional === false && field.nonOptionalErrorMsg === "") {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`nonOptionalErrorMsg for field ${field.id} cannot be empty`);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}
return {
...field,
validate: async (value: any): Promise<string | undefined> => {
// Absent or not optional empty field
if (value === "" && field.optional === false) {
if (field.nonOptionalErrorMsg !== undefined && field.nonOptionalErrorMsg !== "") {
if (field.nonOptionalErrorMsg !== undefined) {
return field.nonOptionalErrorMsg;
}
return "ERROR_NON_OPTIONAL";
Expand Down
26 changes: 26 additions & 0 deletions test/end-to-end/signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ describe("SuperTokens SignUp", function () {
});

it("Check if incorrect getDefaultValue throws error", async function () {
await page.evaluate(() => window.localStorage.setItem("INCORRECT_GETDEFAULT", "YES"));
let pageErrorMessage = "";
page.on("pageerror", (err) => {
pageErrorMessage = err.message;
Expand All @@ -724,6 +725,7 @@ describe("SuperTokens SignUp", function () {
});

it("Check if non-string params to onChange throws error", async function () {
await page.evaluate(() => window.localStorage.removeItem("INCORRECT_GETDEFAULT"));
await page.evaluate(() => window.localStorage.setItem("INCORRECT_ONCHANGE", "YES"));
await page.reload({
waitUntil: "domcontentloaded",
Expand All @@ -745,6 +747,30 @@ describe("SuperTokens SignUp", function () {
`Expected "${expectedErrorMessage}" to be included in page-error`
);
});

it("Check if empty string for nonOptionalErrorMsg throws error", async function () {
const expectedErrorMessage = "nonOptionalErrorMsg for field city cannot be empty";
let pageErrorMessage = "";
page.on("pageerror", (err) => {
pageErrorMessage = err.message;
});

await page.evaluate(() => window.localStorage.removeItem("INCORRECT_GETDEFAULT"));
await page.evaluate(() => window.localStorage.removeItem("INCORRECT_ONCHANGE"));
await page.evaluate(() => window.localStorage.setItem("INCORRECT_NON_OPTIONAL_ERROR_MSG", "YES"));
await page.reload({
waitUntil: "domcontentloaded",
});

if (pageErrorMessage !== "") {
assert(
pageErrorMessage.includes(expectedErrorMessage),
`Expected "${expectedErrorMessage}" to be included in page-error`
);
} else {
throw "Empty nonOptionalErrorMsg should throw error";
}
});
});
});

Expand Down
Loading