29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import type { Person } from './api.ts'
|
|
|
|
/** The e-mail addresses linux-manager-user accepts: one @, no spaces, and a dot in the domain. */
|
|
const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
|
|
|
|
/** The longest first or last name linux-manager-user accepts. */
|
|
const PERSON_NAME_MAX = 64
|
|
|
|
/** The problem of each person field, or '' while it is empty or good. */
|
|
export function personProblems(person: Person): Record<keyof Person, string> {
|
|
const name = (value: string) => (value.trim().length > PERSON_NAME_MAX ? `At most ${PERSON_NAME_MAX} characters.` : '')
|
|
return {
|
|
first_name: name(person.first_name),
|
|
last_name: name(person.last_name),
|
|
email: person.email.trim() !== '' && !EMAIL_PATTERN.test(person.email.trim()) ? 'Not an e-mail address.' : '',
|
|
}
|
|
}
|
|
|
|
/** Whether every person field is filled in and has no problem. */
|
|
export function personComplete(person: Person) {
|
|
const problems = personProblems(person)
|
|
return (Object.keys(person) as (keyof Person)[]).every((key) => person[key].trim() !== '' && problems[key] === '')
|
|
}
|
|
|
|
/** The person fields with the surrounding spaces removed, as they are sent. */
|
|
export function trimmed(person: Person): Person {
|
|
return { first_name: person.first_name.trim(), last_name: person.last_name.trim(), email: person.email.trim() }
|
|
}
|