diff --git a/lib/build/recipe/emailpassword/api/utils.js b/lib/build/recipe/emailpassword/api/utils.js index b947c990e..afbca42de 100644 --- a/lib/build/recipe/emailpassword/api/utils.js +++ b/lib/build/recipe/emailpassword/api/utils.js @@ -67,7 +67,9 @@ async function validateFormOrThrowError(inputs, configFormFields, tenantId, user // and the field is not optional. const isValidInput = !!input && - ((typeof input.value === "string" && input.value.length > 0) || + ((typeof input.value === "string" + ? input.value.length > 0 + : input.value !== null && typeof input.value !== "undefined") || (typeof input.value === "object" && Object.values(input.value).length > 0)); if (!formField.optional && !isValidInput) { validationErrors.push({ diff --git a/lib/ts/recipe/emailpassword/api/utils.ts b/lib/ts/recipe/emailpassword/api/utils.ts index b30721035..23023ae1f 100644 --- a/lib/ts/recipe/emailpassword/api/utils.ts +++ b/lib/ts/recipe/emailpassword/api/utils.ts @@ -87,7 +87,7 @@ function newBadRequestError(message: string) { async function validateFormOrThrowError( inputs: { id: string; - value: string | object | undefined; + value: any | undefined; }[], configFormFields: NormalisedFormField[], tenantId: string, @@ -111,7 +111,9 @@ async function validateFormOrThrowError( // and the field is not optional. const isValidInput = !!input && - ((typeof input.value === "string" && input.value.length > 0) || + ((typeof input.value === "string" + ? input.value.length > 0 + : input.value !== null && typeof input.value !== "undefined") || (typeof input.value === "object" && Object.values(input.value).length > 0)); if (!formField.optional && !isValidInput) { validationErrors.push({ diff --git a/test/emailpassword/signupFeature.test.js b/test/emailpassword/signupFeature.test.js index 371c98d30..4f337ef8b 100644 --- a/test/emailpassword/signupFeature.test.js +++ b/test/emailpassword/signupFeature.test.js @@ -830,6 +830,136 @@ describe(`signupFeature: ${printPath("[test/emailpassword/signupFeature.test.js] assert(response.user.emails[0] === "random@gmail.com"); }); + it("test valid boolean formFields with optional", async function () { + const connectionURI = await startST(); + STExpress.init({ + supertokens: { + connectionURI, + }, + appInfo: { + apiDomain: "api.supertokens.io", + appName: "SuperTokens", + websiteDomain: "supertokens.io", + }, + recipeList: [ + EmailPassword.init({ + signUpFeature: { + formFields: [ + { + id: "autoVerify", + optional: false, + }, + ], + }, + }), + Session.init({ getTokenTransferMethod: () => "cookie" }), + ], + }); + const app = express(); + + app.use(middleware()); + + app.use(errorHandler()); + + let response = await new Promise((resolve) => + request(app) + .post("/auth/signup") + .send({ + formFields: [ + { + id: "password", + value: "validpass123", + }, + { + id: "email", + value: "random@gmail.com", + }, + { + id: "autoVerify", + value: false, + }, + ], + }) + .expect(200) + .end((err, res) => { + if (err) { + resolve(undefined); + } else { + resolve(JSON.parse(res.text)); + } + }) + ); + + assert(response.status === "OK"); + assert(response.user.id !== undefined); + assert(response.user.emails[0] === "random@gmail.com"); + }); + + it("test valid int formFields with optional", async function () { + const connectionURI = await startST(); + STExpress.init({ + supertokens: { + connectionURI, + }, + appInfo: { + apiDomain: "api.supertokens.io", + appName: "SuperTokens", + websiteDomain: "supertokens.io", + }, + recipeList: [ + EmailPassword.init({ + signUpFeature: { + formFields: [ + { + id: "intField", + optional: false, + }, + ], + }, + }), + Session.init({ getTokenTransferMethod: () => "cookie" }), + ], + }); + const app = express(); + + app.use(middleware()); + + app.use(errorHandler()); + + let response = await new Promise((resolve) => + request(app) + .post("/auth/signup") + .send({ + formFields: [ + { + id: "password", + value: "validpass123", + }, + { + id: "email", + value: "random@gmail.com", + }, + { + id: "intField", + value: 23, + }, + ], + }) + .expect(200) + .end((err, res) => { + if (err) { + resolve(undefined); + } else { + resolve(JSON.parse(res.text)); + } + }) + ); + + assert(response.status === "OK"); + assert(response.user.id !== undefined); + assert(response.user.emails[0] === "random@gmail.com"); + }); + //- Bad test case without optional (something is missing, and it's not optional) it("test bad case input to signup without optional", async function () { const connectionURI = await startST();