Skip to content
Permalink
Browse files

feat(validation): truncate long validation message

  • Loading branch information...
kirillgroshkov committed Apr 16, 2019
1 parent 868556e commit 6e70bb1f066efa226d8b47eda264822638f2623a
@@ -72,6 +72,19 @@ exports[`getValidationResult should still convert 1`] = `
[1] "a2" length must be at least 2 characters long]
`;

exports[`long message string 1`] = `
"[
{
\\"a\\" [1, 10, 100, 1000, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 13, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 14, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 15, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 16, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 17, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 18, 180, 181, 182, 183, 184, 185, 186,
... 37 KB message truncated
\\"a\\" must be a string @ .0.a
\\"a\\" must be a string @ .1.a
\\"a\\" must be a string @ .2.a
\\"a\\" must be a string @ .3.a
\\"a\\" must be a string @ .4.a
... 1000 errors"
`;

exports[`should fail on invalid values 1`] = `"\\"value\\" is required"`;

exports[`should fail on invalid values 2`] = `
@@ -1,6 +1,16 @@
import { AppError, ErrorData } from '@naturalcycles/js-lib'
import { ValidationErrorItem } from 'joi'

/**
* Example of ValidationErrorItem:
*
* {
* message: '"temperature" must be larger than or equal to 33',
* path: [ 'entries', 10, 'temperature' ],
* type: 'number.min',
* context: { limit: 33, value: 30, key: 'temperature', label: 'temperature' }
* }
*/
export interface JoiValidationErrorData extends ErrorData {
joiValidationErrorItems: ValidationErrorItem[]
}
@@ -153,3 +153,17 @@ test('array items with invalid props', async () => {

// optional
// default values

test('long message string', () => {
const objSchema = arraySchema.items(
objectSchema({
a: stringSchema,
}),
)

const longObject = Array(1000).fill({ a: 5 })

const { error } = getValidationResult(longObject, objSchema)
// console.log(error!.message, error!.message.length)
expect(error!.message).toMatchSnapshot()
})
@@ -88,11 +88,27 @@ export function getValidationResult<T> (

export function validationErrorToString (err: ValidationError, objectName?: string): string {
if (!err) return undefined as any
let msg = ''
if (objectName) msg += objectName + '\n'
const tokens: string[] = []
if (objectName) tokens.push(objectName)

// Strip colors in production (for e.g Sentry reporting)
const stripColors = process.env.NODE_ENV === 'production'
msg += (err.annotate as any)(stripColors) // typings are not up-to-date, hence "as any"
return msg
const annotation: string = (err.annotate as any)(stripColors) // typings are not up-to-date, hence "as any"

if (annotation.length > 5000) {
// Annotation message is too big and will be replaced by stringified `error.details` instead
tokens.push(
annotation.substr(0, 500),
`... ${Math.ceil(annotation.length / 1024)} KB message truncated`,
)

// Up to 5 `details`
tokens.push(...err.details.slice(0, 5).map(i => `${i.message} @ .${i.path.join('.')}`))

if (err.details.length > 5) tokens.push(`... ${err.details.length} errors`)
} else {
tokens.push(annotation)
}

return tokens.join('\n')
}

0 comments on commit 6e70bb1

Please sign in to comment.
You can’t perform that action at this time.