feat(overlaybioentity): add util to get validated overlay bio entities
Add util to get validated overlay bio entities in order to provide only valid data to parseOverlayBioEntityToOlRenderingFormat
Merge request reports
Activity
assigned to @mateusz-winiarczyk
66 68 67 69 return overlaysOrder; 68 70 }; 71 72 const isValidOverlayBioEntity = (overlayBioEntity: OverlayBioEntity): boolean => { 73 return overlayBioEntitySchema.safeParse(overlayBioEntity).success; 74 }; 75 76 type OverlayBioEntities = OverlayBioEntity[]; 77 78 export const getValidOverlayBioEntities = ( 79 unvalidatedOverlayBioEntities: OverlayBioEntities, 80 ): OverlayBioEntities => { 81 const filteredValidOverlayBioEntities = z changed this line in version 2 of the diff
74 }; 75 76 type OverlayBioEntities = OverlayBioEntity[]; 77 78 export const getValidOverlayBioEntities = ( 79 unvalidatedOverlayBioEntities: OverlayBioEntities, 80 ): OverlayBioEntities => { 81 const filteredValidOverlayBioEntities = z 82 .array(z.any()) 83 .transform(overlayBioEntities => overlayBioEntities.filter(isValidOverlayBioEntity)); 84 85 const parsedOverlayBioEntities = filteredValidOverlayBioEntities.safeParse( 86 unvalidatedOverlayBioEntities, 87 ); 88 89 return parsedOverlayBioEntities.success ? parsedOverlayBioEntities.data : []; changed this line in version 2 of the diff
It's zod type safety thing. We are not able to get
parsedOverlayBioEntities.data
ifparsedOverlayBioEntities.success
is not true and I don't think using assertion is better solution. Changed false value toundefined
, I think with undefined it makes more sense with code below.const validOverlayBioEntities = getValidOverlayBioEntities(response.data); if (validOverlayBioEntities) { return parseOverlayBioEntityToOlRenderingFormat(validOverlayBioEntities, overlayId); }
68 70 }; 71 72 const isValidOverlayBioEntity = (overlayBioEntity: OverlayBioEntity): boolean => { 73 return overlayBioEntitySchema.safeParse(overlayBioEntity).success; 74 }; 75 76 type OverlayBioEntities = OverlayBioEntity[]; 77 78 export const getValidOverlayBioEntities = ( 79 unvalidatedOverlayBioEntities: OverlayBioEntities, 80 ): OverlayBioEntities => { 81 const filteredValidOverlayBioEntities = z 82 .array(z.any()) 83 .transform(overlayBioEntities => overlayBioEntities.filter(isValidOverlayBioEntity)); 84 85 const parsedOverlayBioEntities = filteredValidOverlayBioEntities.safeParse( It's confusing solution for me to understand. If I'm not mistaken
- You create schema
filteredValidOverlayBioEntities
that removes (.transform()
) entities that does not match the schema. - You use schema to parse the data and for every entity use
isValidOverlayBioEntity
to parse. - Then you return data in line 89 if it succeeded
- In
src/redux/overlayBioEntity/overlayBioEntity.thunk.ts
line 35 the data you already parsed is parsed again (but it's always valid at this moment)
How about making it simpler by making it one liner solution?
data.map(validate)
- You create schema
changed this line in version 2 of the diff
Removed unnecessary reuse of safeParse w
overlayBioEntity.thunk.ts
. We are unable to get the data directly without calling safeParse after filteredValidOverlayBioEntitiesSchema. We need to check whether safeParse is successful and if so, we have access to the data. I checked the zod methods and we havetransform
andpreprocess
and both have the same condition for reaching the data Now all the logic for filtering and parsing is done in util.@mateusz-winiarczyk ok I got it. Thank you for clarification. So it lgtm
27 30 apiPath.getOverlayBioEntity({ overlayId, modelId }), 28 31 ); 29 32 30 const isDataValid = validateDataUsingZodSchema(response.data, z.array(overlayBioEntitySchema)); 33 const validOverlayBioEntities = getValidOverlayBioEntities(response.data); 34 35 const isDataValid = validateDataUsingZodSchema( changed this line in version 2 of the diff
added 1 commit
- 9b75abcd - refactor(overlaybioentity): refactor getValidOverlayBioEntities
mentioned in commit f9b87323