Skip to content

Commit

Permalink
Fix: Add support for any type in formField values (#924)
Browse files Browse the repository at this point in the history
* Add support for accepting any type in form fields

* Add test case for int in formField

---------

Co-authored-by: Rishabh Poddar <[email protected]>
  • Loading branch information
deepjyoti30Alt and rishabhpoddar authored Sep 14, 2024
1 parent 2488e0e commit f6b577f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/build/recipe/emailpassword/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 4 additions & 2 deletions lib/ts/recipe/emailpassword/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand Down
130 changes: 130 additions & 0 deletions test/emailpassword/signupFeature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,136 @@ describe(`signupFeature: ${printPath("[test/emailpassword/signupFeature.test.js]
assert(response.user.emails[0] === "[email protected]");
});

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: "[email protected]",
},
{
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] === "[email protected]");
});

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: "[email protected]",
},
{
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] === "[email protected]");
});

//- 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();
Expand Down

0 comments on commit f6b577f

Please sign in to comment.