Skip to content

Commit

Permalink
1670: disallow multi spaces. move replace multi spaces to utils, fix …
Browse files Browse the repository at this point in the history
…small issues
  • Loading branch information
f1sh1918 committed Nov 13, 2024
1 parent 57ee118 commit cd7d8db
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ClearInputButton from '../../cards/extensions/components/ClearInputButton
import useWindowDimensions from '../../hooks/useWindowDimensions'
import BasicDialog from '../../mui-modules/application/BasicDialog'
import { ProjectConfigContext } from '../../project-configs/ProjectConfigContext'
import { removeMultipleSpaces } from '../../util/helper'
import { useAppToaster } from '../AppToaster'
import ExtensionForms from '../cards/ExtensionForms'
import { DataPrivacyAcceptingStatus } from './CardSelfServiceView'
Expand Down Expand Up @@ -82,7 +83,7 @@ const CardSelfServiceForm = ({
}
intent={isFullNameValid(card) ? undefined : Intent.DANGER}
value={card.fullName}
onChange={event => updateCard({ fullName: event.target.value })}
onChange={event => updateCard({ fullName: removeMultipleSpaces(event.target.value) })}
/>
<FormErrorMessage errorMessage={getFullNameValidationErrorMessage(card.fullName)} />
</FormGroup>
Expand Down
16 changes: 4 additions & 12 deletions administration/src/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { CardExtensions, CardInfo } from '../generated/card_pb'
import { Region } from '../generated/graphql'
import { CardConfig } from '../project-configs/getProjectConfig'
import PlainDate from '../util/PlainDate'
import { removeMultipleSpaces } from '../util/helper'
import { REGION_EXTENSION_NAME } from './extensions/RegionExtension'
import Extensions, { Extension, ExtensionKey, ExtensionState, InferExtensionStateType } from './extensions/extensions'

// Due to limited space on the cards
export const MAX_NAME_LENGTH = 40
export const MAX_NAME_LENGTH = 30
// Due to limited space on the qr code
const MAX_ENCODED_NAME_LENGTH = 50

Expand Down Expand Up @@ -75,19 +76,13 @@ const hasValidNameLength = (fullName: string): boolean => {
return fullName.length > 0 && encodedName.length <= MAX_ENCODED_NAME_LENGTH && fullName.length <= MAX_NAME_LENGTH
}

const multipleSpacePattern = /\s\s+/g
const containsMultipleSpaces = (fullName: string): boolean => multipleSpacePattern.test(fullName)

const hasNameAndForename = (fullName: string): boolean => {
const names = fullName.replace(multipleSpacePattern, ' ').split(' ')
const names = removeMultipleSpaces(fullName).split(' ')
return names.length > 1 && names.every(name => name.length > 1)
}

export const isFullNameValid = ({ fullName }: Card): boolean =>
hasValidNameLength(fullName) &&
hasNameAndForename(fullName) &&
!containsNameSpecialCharacters(fullName) &&
!containsMultipleSpaces(fullName)
hasValidNameLength(fullName) && hasNameAndForename(fullName) && !containsNameSpecialCharacters(fullName)

export const isExpirationDateValid = (card: Card, { nullable } = { nullable: false }): boolean => {
const today = PlainDate.fromLocalDate(new Date())
Expand Down Expand Up @@ -205,9 +200,6 @@ export const getFullNameValidationErrorMessage = (name: string): string | null =
if (containsNameSpecialCharacters(name)) {
errors.push('Der Name darf keine Sonderzeichen oder Zahlen enthalten.')
}
if (containsMultipleSpaces(name)) {
errors.push('Der Name darf nicht mehrere aufeinanderfolge Leerzeichen enthalten.')
}
if (!hasNameAndForename(name)) {
errors.push('Bitte geben Sie Ihren vollständigen Namen ein.')
}
Expand Down
3 changes: 3 additions & 0 deletions administration/src/util/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ export const updateArrayItem = <T>(array: T[], updatedItem: T, index: number): T

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never

const multipleSpacePattern = /\s\s+/g
export const removeMultipleSpaces = (value: string): string => value.replace(multipleSpacePattern, ' ')

0 comments on commit cd7d8db

Please sign in to comment.