diff --git a/packages/evolution-generator/src/common/validations.tsx b/packages/evolution-generator/src/common/validations.tsx index 75924f8f..28295701 100644 --- a/packages/evolution-generator/src/common/validations.tsx +++ b/packages/evolution-generator/src/common/validations.tsx @@ -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) => { @@ -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: { + 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 [ diff --git a/packages/evolution-generator/src/types/inputTypes.ts b/packages/evolution-generator/src/types/inputTypes.ts index 410a3915..7bb643bb 100644 --- a/packages/evolution-generator/src/types/inputTypes.ts +++ b/packages/evolution-generator/src/types/inputTypes.ts @@ -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) }; @@ -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; +type Linestrings = GeoJSON.FeatureCollection; type Polygons = GeoJSON.FeatureCollection; -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 @@ -110,6 +113,7 @@ export type InputString = InputStringBase & { conditional: Conditional; validations?: Validations; textTransform?: 'uppercase' | 'lowercase' | 'capitalize'; + defaultValue?: string; }; /* Text widgetConfig Type */ @@ -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; };