-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13975 from mozilla/FXA-5793-remove-unique-values-…
…from-validation-error-messages task(content): Remove 'with value $val' from regex validation error m…
- Loading branch information
Showing
6 changed files
with
82 additions
and
4 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
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,40 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { AnySchema } from 'joi'; | ||
|
||
/** | ||
* A set of default message overrides. These result in better error resolution in sentry. | ||
*/ | ||
export const defaultMessageOverrides = { | ||
// Override some of the message defaults. Here we remove the 'with value {:[.]}' | ||
// portion of the message, because it causes too much fragmentation in our sentry | ||
// errors. These should be applied to any .regex or .pattern joi validator. | ||
// Form more context concerning overriding messages see: | ||
// - https://joi.dev/api/?v=17.6.0#anymessagesmessages | ||
// - https://github.com/hapijs/joi/blob/7aa36666863c1dde7e4eb02a8058e00555a99d54/lib/types/string.js#L718 | ||
'string.pattern.base': | ||
'{{#label}} fails to match the required pattern: {{#regex}}', | ||
'string.pattern.name': '{{#label}} fails to match the {{#name}} pattern', | ||
'string.pattern.invert.base': | ||
'{{#label}} matches the inverted pattern: {{#regex}}', | ||
'string.pattern.invert.name': | ||
'{{#label}} matches the inverted {{#name}} pattern', | ||
}; | ||
|
||
/** | ||
* Applies a set of message overrides to the default joi message formats. | ||
* @param data - Set of joi validators to apply message overrides to to. Note, data is mutated. | ||
* @param overrides - Set of optional overrides, if none are provide the defaultMessageOverrides are used. | ||
* @returns data | ||
*/ | ||
export function overrideJoiMessages( | ||
data: Record<string, AnySchema>, | ||
overrides?: Record<string, string> | ||
) { | ||
Object.keys(data).forEach( | ||
(x) => (data[x] = data[x].messages(overrides || defaultMessageOverrides)) | ||
); | ||
return data; | ||
} |
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,26 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { assert } from 'chai'; | ||
import joi, { valid } from 'joi'; | ||
import { overrideJoiMessages } from '../../sentry/joi-message-overrides'; | ||
|
||
describe('joi-message-overrides', () => { | ||
it('overrides default message for regex', () => { | ||
const validators = { | ||
test: joi.string().regex(/test/), | ||
}; | ||
const result1 = validators.test.validate('foobar').error?.message; | ||
|
||
const validators2 = overrideJoiMessages(validators); | ||
const result2 = validators2.test.validate('foobar').error?.message; | ||
|
||
assert.exists(validators2); | ||
assert.exists(result1); | ||
assert.exists(result2); | ||
assert.notEqual(result1, result2); | ||
assert.include(result1, 'with value'); | ||
assert.notInclude(result2, 'with value'); | ||
}); | ||
}); |