Skip to content

Commit

Permalink
Add car and householdSize validations (#408)
Browse files Browse the repository at this point in the history
Add car and householdSize validations
Add defaultValue type
Get better types in inputTypes.ts
  • Loading branch information
samuel-duhaime authored Mar 25, 2024
1 parent ff89a07 commit a456341
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
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: {
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

0 comments on commit a456341

Please sign in to comment.