mirror of
https://github.com/twentyhq/twenty.git
synced 2024-12-25 21:13:01 +03:00
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:
parent
9f59ddc059
commit
b119dd8e9c
@ -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, '')
|
||||
: '';
|
||||
};
|
@ -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),
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user