Added sanitize funtion to normalize the link input (#3543)

* Added sanitize funtion to sanitize the link input of the companies record

* Enabled Eslint

* FIXED: Sanitize www. and query params

Added logic to sanitize both www  and query params in the link input.

* fix: fix useSpreadsheetPersonImport tests

* Refactored sanitizeLink function at packages/twenty-front/src/modules/object-record/utils/sanitizeLinkRecordInput.ts

Co-authored-by: Thaïs <guigon.thais@gmail.com>

---------

Co-authored-by: Thaïs <guigon.thais@gmail.com>
This commit is contained in:
Sujith Thirumalaisamy 2024-02-07 14:39:25 +05:30 committed by GitHub
parent 9f59ddc059
commit b119dd8e9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -0,0 +1,13 @@
import { z } from 'zod';
export const sanitizeLink = (url: string) =>
getUrlHostName(url) || getUrlHostName(`https://${url}`);
const getUrlHostName = (url: string) => {
const urlSchema = z.string().url();
const validation = urlSchema.safeParse(url);
return validation.success
? new URL(validation.data).hostname.replace(/^www\./i, '')
: '';
};

View File

@ -1,5 +1,9 @@
import { isString } from '@sniptt/guards';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { isFieldRelationValue } from '@/object-record/record-field/types/guards/isFieldRelationValue';
import { sanitizeLink } from '@/object-record/utils/sanitizeLinkRecordInput';
import { FieldMetadataType } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
@ -10,7 +14,7 @@ export const sanitizeRecordInput = ({
objectMetadataItem: ObjectMetadataItem;
recordInput: Record<string, unknown>;
}) => {
return Object.fromEntries(
const filteredResultRecord = Object.fromEntries(
Object.entries(recordInput)
.map<[string, unknown] | undefined>(([fieldName, fieldValue]) => {
const fieldMetadataItem = objectMetadataItem.fields.find(
@ -37,4 +41,14 @@ export const sanitizeRecordInput = ({
})
.filter(isDefined),
);
if (
objectMetadataItem.nameSingular !== CoreObjectNameSingular.Company ||
!isString(filteredResultRecord.domainName)
)
return filteredResultRecord;
return {
...filteredResultRecord,
domainName: sanitizeLink(filteredResultRecord.domainName),
};
};