diff --git a/src/jobs/ega/egaClient.ts b/src/jobs/ega/egaClient.ts index 18b68d2..56fbbee 100644 --- a/src/jobs/ega/egaClient.ts +++ b/src/jobs/ega/egaClient.ts @@ -51,10 +51,9 @@ import { GetUserFailure, Result, RevokePermissionsFailure, - safeParseArray, success, - ZodResultAccumulator, } from './types/results'; +import { safeParseArray, ZodResultAccumulator } from './types/zodSafeParseArray'; import { ApprovedUser, getErrorMessage } from './utils'; const { DACS, DATASETS, PERMISSIONS, REQUESTS, USERS } = EGA_API; diff --git a/src/jobs/ega/types/results.ts b/src/jobs/ega/types/results.ts index 4c98b04..9ba2b55 100644 --- a/src/jobs/ega/types/results.ts +++ b/src/jobs/ega/types/results.ts @@ -17,8 +17,6 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import { z, ZodError, ZodTypeAny } from 'zod'; - /* ******************* * Success and Failure types * ******************* */ @@ -76,39 +74,6 @@ export const failure = ( data: undefined, }); -export type ZodResultAccumulator = { success: T[]; failure: ZodError[] }; -/** - * Parses an array of Zod SafeParseReturnType results into success (successful parse) and failure (parsing error) - * @param acc ZodResultAccumulator - * @param item z.SafeParseReturnType - * @returns ZodResultAccumulator - */ -const resultReducer = (acc: ZodResultAccumulator, item: z.SafeParseReturnType) => { - if (item.success) { - acc.success.push(item.data); - } else { - acc.failure.push(item.error); - } - return acc; -}; - -/** - * Run Zod safeParse for Schema T on an array of items, and split results by SafeParseReturnType 'success' or 'error'. - * @params schema - * @params data unknown[] - * @returns { success: [], failure: [] } - */ -export const safeParseArray = ( - schema: T, - data: Array, -): ZodResultAccumulator> => - data - .map((i) => schema.safeParse(i)) - .reduce>>((acc, item) => resultReducer(acc, item), { - success: [], - failure: [], - }); - /* ******************* * Failure types * ******************* */ diff --git a/src/jobs/ega/types/zodSafeParseArray.ts b/src/jobs/ega/types/zodSafeParseArray.ts new file mode 100644 index 0000000..8151d05 --- /dev/null +++ b/src/jobs/ega/types/zodSafeParseArray.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 The Ontario Institute for Cancer Research. All rights reserved + * + * This program and the accompanying materials are made available under the terms of + * the GNU Affero General Public License v3.0. You should have received a copy of the + * GNU Affero General Public License along with this program. + * If not, see . + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import { z, ZodError, ZodTypeAny } from 'zod'; + +export type ZodResultAccumulator = { success: T[]; failure: ZodError[] }; +/** + * Parses an array of Zod SafeParseReturnType results into success (successful parse) and failure (parsing error) + * @param acc ZodResultAccumulator + * @param item z.SafeParseReturnType + * @returns ZodResultAccumulator + */ +const resultReducer = (acc: ZodResultAccumulator, item: z.SafeParseReturnType) => { + if (item.success) { + acc.success.push(item.data); + } else { + acc.failure.push(item.error); + } + return acc; +}; + +/** + * Run Zod safeParse for Schema T on an array of items, and split results by SafeParseReturnType 'success' or 'error'. + * @params schema + * @params data unknown[] + * @returns { success: [], failure: [] } + */ +export const safeParseArray = ( + schema: T, + data: Array, +): ZodResultAccumulator> => + data + .map((i) => schema.safeParse(i)) + .reduce>>((acc, item) => resultReducer(acc, item), { + success: [], + failure: [], + });