Skip to content

Commit

Permalink
feat(normalize-hash): Add utf8 check
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn de Voogd committed Jun 25, 2024
1 parent 532b591 commit bc0e0e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ You can also combine the normalize options:
/// @encryption:hash(email)?normalize=lowercase&normalize=trim&normalize=trim&normalize=diacritics
```

> Be aware: You should only use the normalize hash feature in combination with a `utf8` input encodnig. It would not make sense to normalize a `hex` or `base64` string.
> Be aware: Using the normalize hash feature in combination with `unique` could cause conflicts. Example: Users with the name `François` and `francois` result in the same hash which could result in a database conflict.
## Migrations
Expand Down
12 changes: 11 additions & 1 deletion src/dmmf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,19 @@ export function parseHashAnnotation(
process.env.NODE_ENV === 'development' &&
model &&
field
) {
console.warn(warnings.unsupportedNormalize(model, field, normalize))
}

if (
normalize.length > 0 &&
inputEncoding !== 'utf8' &&
process.env.NODE_ENV === 'development' &&
model &&
field
) {
console.warn(
warnings.unsupportedNormalize(model, field, normalize, 'output')
warnings.unsupportedNormalizeEncoding(model, field, inputEncoding)
)
}

Expand Down
12 changes: 9 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,15 @@ export const warnings = {
unsupportedNormalize: (
model: string,
field: string,
normalize: string,
io: string
) => `${warning}: unsupported ${io} normalize \`${normalize}\` for hash field ${model}.${field}
normalize: string
) => `${warning}: unsupported normalize \`${normalize}\` for hash field ${model}.${field}
-> Valid values are ${Object.values(HashFieldNormalizeOptions)}
`,
unsupportedNormalizeEncoding: (
model: string,
field: string,
inputEncoding: string
) => `${warning}: unsupported normalize flag on field with encoding \`${inputEncoding}\` for hash field ${model}.${field}
-> Valid inputEncoding values for normalize are [utf8]
`
}

0 comments on commit bc0e0e5

Please sign in to comment.