Validate no motion sens set if no stick sens set

This commit is contained in:
Kalle
2022-10-02 12:29:54 +03:00
parent 7b038d87d9
commit 73e31da398
2 changed files with 75 additions and 62 deletions

View File

@@ -29,6 +29,7 @@ import styles from "~/styles/u-edit.css";
import { translatedCountry } from "~/utils/i18n.server";
import { safeParseRequestFormData } from "~/utils/remix";
import { errorIsSqliteUniqueConstraintFailure } from "~/utils/sql";
import { rawSensToString } from "~/utils/strings";
import { isCustomUrl, userPage } from "~/utils/urls";
import {
actualNumber,
@@ -42,63 +43,76 @@ export const links: LinksFunction = () => {
return [{ rel: "stylesheet", href: styles }];
};
const userEditActionSchema = z.object({
country: z.preprocess(
falsyToNull,
z
.string()
.refine(
(val) => !val || Object.keys(countries).some((code) => val === code)
)
.nullable()
),
bio: z.preprocess(
falsyToNull,
z.string().max(USER.BIO_MAX_LENGTH).nullable()
),
customUrl: z.preprocess(
falsyToNull,
z
.string()
.max(USER.CUSTOM_URL_MAX_LENGTH)
.refine((val) => val === null || isCustomUrl(val), {
message: "forms.errors.invalidCustomUrl.numbers",
})
.refine(
// validate val only contains numbers and letters
(val) => val === null || /^[a-zA-Z0-9-_]+$/.test(val),
{ message: "forms.errors.invalidCustomUrl.strangeCharacter" }
)
.transform((val) => val?.toLowerCase())
.nullable()
),
stickSens: z.preprocess(
processMany(actualNumber, undefinedToNull),
z
.number()
.min(-50)
.max(50)
.refine((val) => val % 5 === 0)
.nullable()
),
motionSens: z.preprocess(
processMany(actualNumber, undefinedToNull),
z
.number()
.min(-50)
.max(50)
.refine((val) => val % 5 === 0)
.nullable()
),
inGameNameText: z.preprocess(
falsyToNull,
z.string().max(USER.IN_GAME_NAME_TEXT_MAX_LENGTH).nullable()
),
inGameNameDiscriminator: z.preprocess(
falsyToNull,
z.string().length(USER.IN_GAME_NAME_DISCRIMINATOR_LENGTH).nullable()
),
});
const userEditActionSchema = z
.object({
country: z.preprocess(
falsyToNull,
z
.string()
.refine(
(val) => !val || Object.keys(countries).some((code) => val === code)
)
.nullable()
),
bio: z.preprocess(
falsyToNull,
z.string().max(USER.BIO_MAX_LENGTH).nullable()
),
customUrl: z.preprocess(
falsyToNull,
z
.string()
.max(USER.CUSTOM_URL_MAX_LENGTH)
.refine((val) => val === null || isCustomUrl(val), {
message: "forms.errors.invalidCustomUrl.numbers",
})
.refine(
// validate val only contains numbers and letters
(val) => val === null || /^[a-zA-Z0-9-_]+$/.test(val),
{ message: "forms.errors.invalidCustomUrl.strangeCharacter" }
)
.transform((val) => val?.toLowerCase())
.nullable()
),
stickSens: z.preprocess(
processMany(actualNumber, undefinedToNull),
z
.number()
.min(-50)
.max(50)
.refine((val) => val % 5 === 0)
.nullable()
),
motionSens: z.preprocess(
processMany(actualNumber, undefinedToNull),
z
.number()
.min(-50)
.max(50)
.refine((val) => val % 5 === 0)
.nullable()
),
inGameNameText: z.preprocess(
falsyToNull,
z.string().max(USER.IN_GAME_NAME_TEXT_MAX_LENGTH).nullable()
),
inGameNameDiscriminator: z.preprocess(
falsyToNull,
z.string().length(USER.IN_GAME_NAME_DISCRIMINATOR_LENGTH).nullable()
),
})
.refine(
(val) => {
if (val.motionSens !== null && val.stickSens === null) {
return false;
}
return true;
},
{
message: "forms.errors.invalidSens",
}
);
export const action: ActionFunction = async ({ request }) => {
const parsedInput = await safeParseRequestFormData({
@@ -260,8 +274,7 @@ function SensSelects({
<option value="">{"-"}</option>
{SENS_OPTIONS.map((sens) => (
<option key={sens} value={sens}>
{sens > 0 ? "+" : ""}
{Number(sens) / 10}
{rawSensToString(Number(sens))}
</option>
))}
</select>
@@ -277,8 +290,7 @@ function SensSelects({
<option value="">{"-"}</option>
{SENS_OPTIONS.map((sens) => (
<option key={sens} value={sens}>
{sens > 0 ? "+" : ""}
{Number(sens) / 10}
{rawSensToString(Number(sens))}
</option>
))}
</select>

View File

@@ -18,5 +18,6 @@
"forms.errors.invalidCustomUrl.numbers": "Custom URL can't only contain numbers",
"forms.errors.invalidCustomUrl.strangeCharacter": "Custom URL can't contain special characters",
"forms.errors.invalidCustomUrl.duplicate": "Someone is already using this custom URL"
"forms.errors.invalidCustomUrl.duplicate": "Someone is already using this custom URL",
"forms.errors.invalidSens": "Motion sens can't be set if R-stick sens isn't"
}