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

Add car and householdSize validations #408

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions packages/evolution-generator/src/common/validations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { _isBlank } from 'chaire-lib-common/lib/utils/LodashExtensions';
import { Validations } from '../types/inputTypes';
import surveyHelper from 'evolution-legacy/lib/helpers/survey/survey';

// Make sure the question is answered
export const requiredValidation: Validations = (value) => {
Expand Down Expand Up @@ -43,6 +44,86 @@ export const inputRangeValidation: Validations = (value) => {
];
};

// Verify if the value is a valid household size
export const householdSizeValidation: Validations = (value) => {
return [
{
validation: isNaN(Number(value)) || !Number.isInteger(Number(value)),
errorMessage: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il faudrait à terme s'orienter vers des messages seulement via des fichiers de locales. Pas nécessairement maintenant, mais tranquillement.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui, en effet @tahini.

Est-ce qu'il y a un avantage d'utiliser les fichiers de locales comparativement à cette méthode ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est-ce qu'il y a un avantage d'utiliser les fichiers de locales comparativement à cette méthode ?

Plusieurs:

  • Les messages sont tous rassemblés au même endroit, ce qui facilite leur édition plutôt que perdu un peu partout dans le code
  • C'est assez simple et trivial d'ajouter des langues en traduisant uniquement ces fichiers
  • Question responsabilité, un fichier de code ne devrait pas aussi contenir des traductions

fr: 'La taille du ménage est invalide.',
en: 'Household size is invalid.'
}
},
{
validation: _isBlank(value),
errorMessage: {
fr: 'La taille du ménage est requise.',
en: 'Household size is required.'
}
},
{
validation: Number(value) > 18,
errorMessage: {
fr: 'La taille du ménage doit être au maximum 18.',
en: 'Household size must be less than or equal to 18.'
}
},
{
validation: Number(value) <= 0,
errorMessage: {
fr: 'La taille du ménage doit être au moins de 1 (vous devez vous inclure).',
en: 'Household size must be at least 1 (you need to include yourself).'
}
}
];
};

// Verify if the value is a valid number of cars
export const carNumberValidation: Validations = (value, _customValue, interview, _path, _customPath) => {
const householdSize = surveyHelper.get(interview, 'household.size', null);

return [
{
validation: isNaN(Number(value)) || !Number.isInteger(Number(value)),
errorMessage: {
fr: 'Le nombre de véhicules est invalide.',
en: 'The number of vehicles is invalid.'
}
},
{
validation: surveyHelper.isBlank(value),
errorMessage: {
fr: 'Le nombre de véhicules est requis.',
en: 'The number of vehicles is required.'
}
},
{
validation: Number(value) > 13,
errorMessage: {
fr: 'Le nombre de véhicules doit être au maximum 13.',
en: 'The number of vehicles must be less than or equal to 13.'
}
},
{
validation: Number(value) < 0,
errorMessage: {
fr: 'Le nombre de véhicules doit être au moins de 0.',
en: 'The number of vehicles must be at least 0.'
}
},
{
validation:
!surveyHelper.isBlank(householdSize) &&
!isNaN(Number(householdSize)) &&
Number(value) / householdSize > 3,
errorMessage: {
fr: 'Le nombre de véhicules est trop élevé pour le nombre de personnes dans le ménage. Ne pas inclure les véhicules de collection ou les véhicules qui ne sont pas utilisés régulièrement.',
en: 'The number of vehicles is too high for the number of people in the household. Do not include collection vehicles or vehicles that are not used on a regular basis.'
}
}
];
};

// Verify if the value is a valid age
export const ageValidation: Validations = (value) => {
return [
Expand Down
15 changes: 10 additions & 5 deletions packages/evolution-generator/src/types/inputTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Columns = 1 | 2;
type Path = string;
type Placeholder = string;
type Align = 'left' | 'right' | 'center';
type Title = { fr: string; en: string };
type Title = { fr: string | ((interview?, path?) => string); en: string | ((interview?, path?) => string) };
export type InputFilter = (value) => string | number;
type LabelFunction = (t: TFunction, interview?, path?) => string;
type LabelNotFunction = { en: string | ((interview?, path?) => string); fr: string | ((interview?, path?) => string) };
Expand Down Expand Up @@ -58,16 +58,19 @@ export type HelpPopup = {
containsHtml?: ContainsHtml;
title: Title;
content: { fr: string; en: string };
// TODO: This is the correct type, but it doesn't work with the current implementation
// title: (t: TFunction) => string;
// content: (t: TFunction) => string;
};
type DefaultCenter = {
lat: number;
lon: number;
};
type Points = GeoJSON.FeatureCollection<GeoJSON.Point>;
type Linestrings = GeoJSON.FeatureCollection<GeoJSON.LineString | GeoJSON.MultiLineString>;
type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon | GeoJSON.MultiPolygon>;
type Geojsons = (interview?: any, path?: Path, activeUuid?: any) => { polygons: Polygons };
type Geojsons = (
interview?: any,
path?: Path,
activeUuid?: any
) => { points?: Points; linestrings?: Linestrings; polygons?: Polygons };

// TODO: Add some missing types for the different input types

Expand Down Expand Up @@ -110,6 +113,7 @@ export type InputString = InputStringBase & {
conditional: Conditional;
validations?: Validations;
textTransform?: 'uppercase' | 'lowercase' | 'capitalize';
defaultValue?: string;
};

/* Text widgetConfig Type */
Expand Down Expand Up @@ -243,6 +247,7 @@ export type Group = {
shortname: string;
groupName: { fr: string; en: string };
name: { fr: NameFunction; en: NameFunction };
filter?: (interview, groupedObjects) => any;
conditional?: Conditional;
};

Expand Down
Loading