-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: marking strategy now validating correctly
- Loading branch information
Showing
4 changed files
with
134 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
api/src/services/import-services/marking/import-markings-service.interface.ts
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
api/src/services/import-services/marking/import-markings-strategy.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { z } from 'zod'; | ||
import { IAsSelectLookup } from '../../critterbase-service'; | ||
|
||
/** | ||
* Get CSV Marking schema. | ||
* | ||
* Note: This getter allows custom values to be injected for validation. | ||
* | ||
* @param {IAsSelectLookup[]} colours - Array of supported Critterbase colours | ||
* @returns {*} Custom Zod schema for CSV Markings | ||
*/ | ||
export const getCsvMarkingSchema = ( | ||
colours: IAsSelectLookup[], | ||
markingTypes: IAsSelectLookup[], | ||
critterBodyLocationsMap: Map<string, IAsSelectLookup[]> | ||
) => { | ||
const colourNames = colours.map((colour) => colour.value.toLowerCase()); | ||
const markingTypeNames = markingTypes.map((markingType) => markingType.value.toLowerCase()); | ||
|
||
const coloursSet = new Set(colourNames); | ||
const markingTypesSet = new Set(markingTypeNames); | ||
|
||
return z | ||
.object({ | ||
critter_id: z.string({ required_error: 'Unable to find matching survey critter with alias' }).uuid(), | ||
capture_id: z.string({ required_error: 'Unable to find matching capture with date and time' }).uuid(), | ||
body_location: z.string(), | ||
marking_type: z | ||
.string() | ||
.refine( | ||
(val) => markingTypesSet.has(val.toLowerCase()), | ||
`Marking type not supported. Allowed values: ${markingTypeNames.join(', ')}` | ||
) | ||
.optional(), | ||
identifier: z.string().optional(), | ||
primary_colour: z | ||
.string() | ||
.refine( | ||
(val) => coloursSet.has(val.toLowerCase()), | ||
`Colour not supported. Allowed values: ${colourNames.join(', ')}` | ||
) | ||
.optional(), | ||
secondary_colour: z | ||
.string() | ||
.refine( | ||
(val) => coloursSet.has(val.toLowerCase()), | ||
`Colour not supported. Allowed values: ${colourNames.join(', ')}` | ||
) | ||
.optional(), | ||
comment: z.string().optional() | ||
}) | ||
.superRefine((schema, ctx) => { | ||
const bodyLocations = critterBodyLocationsMap.get(schema.critter_id); | ||
if (!bodyLocations) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: 'No taxon body locations found for Critter' | ||
}); | ||
} else if (!bodyLocations.filter((location) => location.value === schema.body_location).length) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: `Invalid body location for Critter. Allowed values: ${bodyLocations | ||
.map((bodyLocation) => bodyLocation.value) | ||
.join(', ')}` | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* A validated CSV Marking object | ||
* | ||
*/ | ||
export type CsvMarking = z.infer<ReturnType<typeof getCsvMarkingSchema>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters