-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 updates throws on validation errors
- Loading branch information
Showing
5 changed files
with
75 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
import { randomUUID } from 'crypto' | ||
import * as EmailValidator from 'email-validator' | ||
import { Email, PersonUpdater } from '../types' | ||
import { throwValidationErrorForField } from './validation-error' | ||
|
||
const normalizeStringInput = (s?: string): string|null => (typeof s === 'string') | ||
? s.trim() | ||
: null | ||
|
||
export const createEmailUpdater = (): PersonUpdater => ({ | ||
updatePerson: async (person, update) => ({ | ||
...person, | ||
email: patchEmail(person?.email, update?.email?.trim()), | ||
email: patchEmail(person?.email || null, getValidatedEmail(normalizeStringInput(update?.email))), | ||
}), | ||
}) | ||
|
||
const patchEmail = (email?: Email, update?: string): Email | null => | ||
update === '' | ||
? null | ||
: update && EmailValidator.validate(update) && (update !== email?.address) | ||
? ({ | ||
address: update, | ||
isVerified: false, | ||
verifiedDate: null, | ||
verificationCode: randomUUID(), | ||
}) | ||
const getValidatedEmail = (email: string): string|null|false => email && EmailValidator.validate(email) && email | ||
|
||
const patchEmail = (email: Email, update: string|null|false): Email | null => | ||
update === null | ||
: update === '' | ||
? null | ||
: !update | ||
? (throwValidationErrorForField('email'), email) | ||
: update !== email?.address | ||
? ({ | ||
address: update, | ||
isVerified: false, | ||
verifiedDate: null, | ||
verificationCode: randomUUID(), | ||
}) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { GraphQLError } from 'graphql' | ||
|
||
export const throwValidationErrorForField = (fieldName: string): void => { | ||
throw new GraphQLError('Validation error', { | ||
extensions: { | ||
'validation-failed-for-field': fieldName, | ||
}, | ||
}) | ||
} |