Skip to content

Commit

Permalink
fix(ui-commons): change error message for string length in validation…
Browse files Browse the repository at this point in the history
…Utils
  • Loading branch information
skamril committed May 16, 2024
1 parent 430d88b commit 61fbed4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
3 changes: 2 additions & 1 deletion webapp/public/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
"form.asyncDefaultValues.error": "Failed to get values",
"form.field.required": "Field required",
"form.field.duplicate": "Value already exists",
"form.field.minLength": "{{0}} character(s) minimum",
"form.field.minLength": "{{length}} character(s) minimum",
"form.field.maxLength": "{{length}} character(s) maximum",
"form.field.minValue": "The minimum value is {{0}}",
"form.field.maxValue": "The maximum value is {{0}}",
"form.field.invalidNumber": "Invalid number",
Expand Down
3 changes: 2 additions & 1 deletion webapp/public/locales/fr/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
"form.asyncDefaultValues.error": "Impossible d'obtenir les valeurs",
"form.field.required": "Champ requis",
"form.field.duplicate": "Cette valeur existe déjà",
"form.field.minLength": "{{0}} caractère(s) minimum",
"form.field.minLength": "{{length}} caractère(s) minimum",
"form.field.maxLength": "{{length}} caractère(s) maximum",
"form.field.minValue": "La valeur minimum est {{0}}",
"form.field.maxValue": "La valeur maximum est {{0}}",
"form.field.invalidNumber": "Nombre invalide",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function AddDialog({
rules={{
validate: (v) =>
validateString(v, {
max: 20,
maxLength: 20,
specialChars: "-",
}),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function Fields({ study, constraintId }: Props) {
rules={{
validate: (v) =>
validateString(v, {
max: 20,
maxLength: 20,
specialChars: "-",
}),
}}
Expand Down
24 changes: 12 additions & 12 deletions webapp/src/utils/validationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ interface NumberValidationOptions {
max?: number;
}

interface ValidationOptions {
interface StringValidationOptions {
existingValues?: string[];
excludedValues?: string[];
isCaseSensitive?: boolean;
allowSpecialChars?: boolean;
specialChars?: string;
allowSpaces?: boolean;
editedValue?: string;
min?: number;
max?: number;
minLength?: number;
maxLength?: number;
}

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -46,7 +46,7 @@ interface ValidationOptions {
*/
export function validateString(
value: string,
options?: ValidationOptions,
options?: StringValidationOptions,
): string | true {
const {
existingValues = [],
Expand All @@ -56,8 +56,8 @@ export function validateString(
allowSpaces = true,
specialChars = "&()_-",
editedValue = "",
min = 0,
max = 255,
minLength = 0,
maxLength = 255,
} = options || {};

const trimmedValue = value.trim();
Expand All @@ -70,12 +70,12 @@ export function validateString(
return t("form.field.spacesNotAllowed");
}

if (trimmedValue.length < min) {
return t("form.field.minValue", { 0: min });
if (trimmedValue.length < minLength) {
return t("form.field.minLength", { length: minLength });
}

if (trimmedValue.length > max) {
return t("form.field.maxValue", { 0: max });
if (trimmedValue.length > maxLength) {
return t("form.field.maxLength", { length: maxLength });
}

// Compiles a regex pattern based on allowed characters and flags.
Expand Down Expand Up @@ -129,11 +129,11 @@ export function validatePassword(password: string): string | true {
}

if (trimmedPassword.length < 8) {
return t("form.field.minValue", { 0: 8 });
return t("form.field.minLength", { length: 8 });
}

if (trimmedPassword.length > 50) {
return t("form.field.maxValue", { 0: 50 });
return t("form.field.maxLength", { length: 50 });
}

if (!/[a-z]/.test(trimmedPassword)) {
Expand Down

0 comments on commit 61fbed4

Please sign in to comment.