Skip to content

Commit

Permalink
feat: add nonOptionalErrorMsg props (#757)
Browse files Browse the repository at this point in the history
* Add nonOptionalErr types

* Now supports nonOptionalErrorMsg attribute

* Add tests and fix signin types to include nonOptionalMsg

* Enforce no api request are made on blank forms

* Clean up signup

* Throw error if invalid nonOptionalErrorMsg, add tests for the same

* Better error message

* Handle incorrect optional flag

* fixes redundant normalisation

---------

Co-authored-by: rishabhpoddar <[email protected]>
  • Loading branch information
amitbadala and rishabhpoddar authored Nov 11, 2023
1 parent 7b9896e commit 52f8d93
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 33 deletions.
22 changes: 22 additions & 0 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,19 @@ const incorrectFormFields = [
return "Please check Terms and conditions";
},
},
{
id: "city",
label: "Your city",
optional: false,
nonOptionalErrorMsg: "", // empty string should throw error
},
];

const customFields = [
{
id: "select-dropdown",
label: "Select Dropdown",
nonOptionalErrorMsg: "Select dropdown is not an optional",
inputComponent: ({ value, name, onChange }) => (
<select value={value} name={name} onChange={(e) => onChange(e.target.value)}>
<option value="" disabled hidden>
Expand All @@ -301,6 +308,7 @@ const customFields = [
id: "terms",
label: "",
optional: false,
nonOptionalErrorMsg: "You must accept the terms and conditions",
inputComponent: ({ name, onChange }) => (
<div
style={{
Expand Down Expand Up @@ -711,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 All @@ -723,6 +735,7 @@ function getFormFields() {

function getSignInFormFields() {
let showDefaultFields = localStorage.getItem("SHOW_SIGNIN_DEFAULT_FIELDS");
let showFieldsWithNonOptionalErrMsg = localStorage.getItem("SHOW_SIGNIN_WITH_NON_OPTIONAL_ERROR_MESSAGE");
if (showDefaultFields === "YES") {
return {
formFields: [
Expand All @@ -736,6 +749,15 @@ function getSignInFormFields() {
},
],
};
} else if (showFieldsWithNonOptionalErrMsg === "YES") {
return {
formFields: [
{
id: "email",
nonOptionalErrorMsg: "Please add email",
},
],
};
}
return {};
}
Expand Down
7 changes: 7 additions & 0 deletions lib/build/emailpassword-shared4.js

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

47 changes: 37 additions & 10 deletions lib/build/emailpassword-shared7.js

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

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

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

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

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

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

2 changes: 2 additions & 0 deletions lib/build/types.d.ts

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

16 changes: 12 additions & 4 deletions lib/ts/recipe/emailpassword/components/library/formBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,18 @@ export const FormBase: React.FC<FormBaseProps<any>> = (props) => {
// If field error.
if (result.status === "FIELD_ERROR") {
const errorFields = result.formFields;

setFieldStates((os) =>
os.map((fs) => ({ ...fs, error: errorFields.find((ef: any) => ef.id === fs.id)?.error }))
);
const getErrorMessage = (fs: FieldState) => {
const errorMessage = errorFields.find((ef: any) => ef.id === fs.id)?.error;
if (errorMessage === "Field is not optional") {
const fieldConfigData = props.formFields.find((f) => f.id === fs.id);
// replace non-optional server error message from nonOptionalErrorMsg
if (fieldConfigData?.nonOptionalErrorMsg !== undefined) {
return fieldConfigData?.nonOptionalErrorMsg;
}
}
return errorMessage;
};
setFieldStates((os) => os.map((fs) => ({ ...fs, error: getErrorMessage(fs) })));
}
}
} catch (e) {
Expand Down
7 changes: 7 additions & 0 deletions lib/ts/recipe/emailpassword/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,18 @@ 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 === "") {
throw new Error(`nonOptionalErrorMsg for field ${field.id} cannot be an empty string`);
}
return {
...field,
validate: async (value: any): Promise<string | undefined> => {
// Absent or not optional empty field
if (value === "" && field.optional === false) {
if (field.nonOptionalErrorMsg !== undefined) {
return field.nonOptionalErrorMsg;
}
return "ERROR_NON_OPTIONAL";
}

Expand Down
10 changes: 10 additions & 0 deletions lib/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ export type FormFieldBaseConfig = {
*Ability to provide default value to input field.
*/
getDefaultValue?: () => string;

/*
* Error message for non optional field.
*/
nonOptionalErrorMsg?: string;
};

export type FormField = FormFieldBaseConfig & {
Expand Down Expand Up @@ -279,6 +284,11 @@ export type NormalisedFormField = {
*/
optional: boolean;

/*
* Error message for non optional field.
*/
nonOptionalErrorMsg?: string;

/*
* Autocomplete input.
*/
Expand Down
44 changes: 44 additions & 0 deletions test/end-to-end/signin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,50 @@ describe("SuperTokens SignIn", function () {
}
});
});

describe("Check if nonOptionalErrorMsg works as expected", function () {
it("Check on blank form submit nonOptionalErrorMsg gets displayed as expected", async function () {
await page.evaluate(() => localStorage.removeItem("SHOW_SIGNIN_DEFAULT_FIELDS"));

// set cookie and reload which loads the form with custom field
await page.evaluate(() =>
window.localStorage.setItem("SHOW_SIGNIN_WITH_NON_OPTIONAL_ERROR_MESSAGE", "YES")
);
await page.reload({
waitUntil: "domcontentloaded",
});

let apiCallMade = false;

const requestHandler = (request) => {
const url = request.url();
if (url === SIGN_IN_API) {
apiCallMade = true;
request.continue();
} else {
request.continue();
}
};

await page.setRequestInterception(true);
page.on("request", requestHandler);

try {
await submitForm(page);
let formFieldErrors = await getFieldErrors(page);

// Also standard non-optional-error is displayed if nonOptionalErrorMsg is not provided
assert.deepStrictEqual(formFieldErrors, ["Please add email", "Field is not optional"]);
} finally {
page.off("request", requestHandler);
await page.setRequestInterception(false);
}

if (apiCallMade) {
throw new Error("Empty form making API request to signin");
}
});
});
});

describe("SuperTokens SignIn => Server Error", function () {
Expand Down
Loading

0 comments on commit 52f8d93

Please sign in to comment.