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

fixes #24

Merged
merged 2 commits into from
Oct 7, 2024
Merged
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
133 changes: 75 additions & 58 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,70 @@ import {
SpeciesTypes,
} from './constants';

import { phoneNumberRegexPattern } from '@aplinkosministerija/design-system';
import { isEqual } from 'lodash';
import { availableMimeTypes } from '../components/fields/DragAndDropUploadField';
import { availablePhotoMimeTypes } from '../components/fields/PhotoUploadField';
import { Species } from '../types';
import { phoneNumberRegexPattern } from '@aplinkosministerija/design-system';

export const validateForm = Yup.object().shape(
{
species: Yup.object().required(validationTexts.requireSelect).nullable(),
source: Yup.object().required(validationTexts.requireSelect).nullable(),
transect: Yup.object().when(['species', 'transect'], {
is: (species: Species, transect) => {
const hasTransect = [
FormTypes.ENDANGERED_ANIMAL,
FormTypes.ENDANGERED_MUSHROOM,
FormTypes.ENDANGERED_PLANT,
].includes(species?.formType);

return hasTransect && transect;
},
then: Yup.object()
.shape({
height: Yup.string()
.required(validationTexts.requireText)
.matches(/^(?=.*[1-9])\d*(?:\.\d+)?$/, 'Turi būti teigiama reikšmė'),
width: Yup.string()
.required(validationTexts.requireText)
.matches(/^(?=.*[1-9])\d*(?:\.\d+)?$/, 'Turi būti teigiama reikšmė'),
})
.nullable(),
}),
transect: Yup.object()
.when(['species', 'transect'], {
is: (species: Species, transect) => {
const hasTransect = [
FormTypes.ENDANGERED_ANIMAL,
FormTypes.ENDANGERED_MUSHROOM,
FormTypes.ENDANGERED_PLANT,
].includes(species?.formType);

return hasTransect && transect;
},
then: Yup.object()
.shape({
height: Yup.string()
.required(validationTexts.requireText)
.matches(/^(?=.*[1-9])\d*(?:\.\d+)?$/, 'Turi būti teigiama reikšmė'),
width: Yup.string()
.required(validationTexts.requireText)
.matches(/^(?=.*[1-9])\d*(?:\.\d+)?$/, 'Turi būti teigiama reikšmė'),
})
.nullable(),
})
.nullable(),

description: Yup.string().required(validationTexts.requireText).nullable(),
geom: Yup.object().required(validationTexts.requireMap).nullable(),
observedBy: Yup.string().required(validationTexts.requireText).nullable(),
observedAt: Yup.date().required(validationTexts.requireSelect).nullable(),
activity: Yup.string().when(['species'], {
is: (species: Species) => {
return isEqual(species?.formType, FormTypes.ENDANGERED_ANIMAL);
},
then: Yup.string().required(validationTexts.requireSelect).nullable(),
}),
activity: Yup.string()
.when(['species'], {
is: (species: Species) => {
return isEqual(species?.formType, FormTypes.ENDANGERED_ANIMAL);
},
then: Yup.string().required(validationTexts.requireSelect).nullable(),
})
.nullable(),

evolution: Yup.string().when(['species', 'activity'], {
is: (species: Species, activity: AnimalActivity) =>
[FormTypes.ENDANGERED_MUSHROOM, FormTypes.ENDANGERED_PLANT].includes(species?.formType) ||
[AnimalActivity.HABITATION, AnimalActivity.OBSERVED_ALIVE, AnimalActivity.OTHER].includes(
activity,
),
then: Yup.string().required(validationTexts.requireSelect).nullable(),
}),
evolution: Yup.string()
.when(['species', 'activity'], {
is: (species: Species, activity: AnimalActivity) => {
return (
[FormTypes.ENDANGERED_MUSHROOM, FormTypes.ENDANGERED_PLANT].includes(
species?.formType,
) ||
[
AnimalActivity.HABITATION,
AnimalActivity.OBSERVED_ALIVE,
AnimalActivity.OTHER,
].includes(activity)
);
},
then: Yup.string().required(validationTexts.requireSelect).nullable(),
})
.nullable(),
quantity: Yup.string().when(['species'], {
is: (species: Species) => {
const isInvasivePlant = isEqual(FormTypes.INVASIVE_PLANT, species?.formType);
Expand All @@ -70,32 +83,36 @@ export const validateForm = Yup.object().shape(
.required(validationTexts.requireText)
.matches(/^[0-9][0-9]*$/, 'Turi būti teigiama reikšmė'),
}),
method: Yup.string().when(['species'], {
is: (species: Species) => {
const formType = species?.formType;
method: Yup.string()
.when(['species'], {
is: (species: Species) => {
const formType = species?.formType;

const hasMethod = [
FormTypes.INVASIVE_CRUSTACEAN,
FormTypes.INVASIVE_FISH,
FormTypes.INVASIVE_MOLLUSK,
FormTypes.INVASIVE_MAMMAL,
FormTypes.INVASIVE_PLANT,
].includes(formType);
const hasMethod = [
FormTypes.INVASIVE_CRUSTACEAN,
FormTypes.INVASIVE_FISH,
FormTypes.INVASIVE_MOLLUSK,
FormTypes.INVASIVE_MAMMAL,
FormTypes.INVASIVE_PLANT,
].includes(formType);

return hasMethod;
},
then: Yup.string().required(validationTexts.requireSelect).nullable(),
}),
methodValue: Yup.string().when(['species', 'method'], {
is: (species: Species, method: string) => {
const isInvasiveMammal = isEqual(FormTypes.INVASIVE_MAMMAL, species?.formType);
return hasMethod;
},
then: Yup.string().required(validationTexts.requireSelect).nullable(),
})
.nullable(),
methodValue: Yup.string()
.when(['species', 'method'], {
is: (species: Species, method: string) => {
const isInvasiveMammal = isEqual(FormTypes.INVASIVE_MAMMAL, species?.formType);

const isObservationValue = isEqual(MammalMethodType.OBSERVATION, method);
const isObservationValue = isEqual(MammalMethodType.OBSERVATION, method);

return isInvasiveMammal && !isObservationValue;
},
then: Yup.string().required(validationTexts.requireText).nullable(),
}),
return isInvasiveMammal && !isObservationValue;
},
then: Yup.string().required(validationTexts.requireText).nullable(),
})
.nullable(),
photos: Yup.array().when('species', {
is: (species) => species?.speciesType.includes(SpeciesTypes.INVASIVE),
then: Yup.array()
Expand Down
Loading