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
Merged
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
14 changes: 12 additions & 2 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.

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") {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
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
9 changes: 8 additions & 1 deletion lib/ts/recipe/emailpassword/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ export function mergeFormFields(
// If new field, push to mergeFormFields.
if (isNewField) {
mergedFormFields.push({
optional: false,
placeholder: userField.label,
validate: defaultValidate,
...userField,
optional: Boolean(userField.optional),
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Expand All @@ -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 === "") {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
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
Loading