Skip to content

Commit

Permalink
created utils folder inside compensations folder and created validati…
Browse files Browse the repository at this point in the history
…on file to align with PR 535
  • Loading branch information
anemne committed Sep 9, 2024
1 parent a6ae1a8 commit c6e1570
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
6 changes: 2 additions & 4 deletions studio/schemas/objects/compensations/benefitsByLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { defineField } from "sanity";
import { location, locationID } from "../locations";
import { companyLocationNameID } from "studio/schemas/documents/companyLocation";
import { benefitId } from "studio/schemas/documents/benefit";
import {
checkForDuplicateLocations,
DocumentWithLocation,
} from "studio/utils/checkForDuplicateLocations";
import { checkForDuplicateLocations, DocumentWithLocation } from "./utils/validation";


export const benefitsByLocation = defineField({
name: "benefitsByLocation",
Expand Down
27 changes: 27 additions & 0 deletions studio/schemas/objects/compensations/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface LocationReference {
_ref: string;
_type: string;
title?: string;
}

export interface DocumentWithLocation {
location: LocationReference;
}

/**
* Checks for duplicate location references in the documents array.
* Ensures each location has a unique document entry.
*
* @param {DocumentWithLocation[] | undefined} documents - The array of documents, each with one or more locations.
* @returns {string | true} - Returns an error message if duplicate locations are found, or true if all are unique.
*/
export const checkForDuplicateLocations = (
documents: DocumentWithLocation[] | undefined,
): boolean => {
if (!documents) return true;
const locationRefs = documents
.map((entry) => entry.location?._ref)
.filter(Boolean);
const uniqueRefs = new Set(locationRefs);
return uniqueRefs.size === locationRefs.length;
};
20 changes: 0 additions & 20 deletions studio/utils/checkForDuplicateLocations.ts

This file was deleted.

0 comments on commit c6e1570

Please sign in to comment.