Closed heken84 closed 2 weeks ago
Hi there,
is there someone who can help me with this issue?
Best regards, Heiko
Hello there !
I also had this issue. By following the stack trace and inspecting the code I found the source of the problem in this function:
// src/utils/util.ts, function definition starts at line 55
// ...
export function formatI18nErrors<K = Record<string, unknown>>(
errors: I18nValidationError[],
i18n: I18nService<K>,
options?: TranslateOptions,
): I18nValidationError[] {
return errors.map((error) => {
error.children = formatI18nErrors(error.children ?? [], i18n, options);
// the error is in the next line
error.constraints = Object.keys(error.constraints).reduce((result, key) => {
const [translationKey, argsString] = error.constraints[key].split('|');
const args = !!argsString ? JSON.parse(argsString) : {};
const constraints = args.constraints
? args.constraints.reduce((acc: object, cur: any, index: number) => {
acc[index.toString()] = cur;
return acc;
}, {})
: error.constraints;
result[key] = i18n.translate(translationKey as Path<K>, {
...options,
args: {
property: error.property,
value: error.value,
target: error.target,
contexts: error.contexts,
...args,
constraints,
},
});
return result;
}, {});
return error;
});
}
// ...
Object.keys(...)
will throw if error.constraints === undefined
, which happens when the validation errors originate in the children and no validation error occurs in the parent.
The solution I found is to replace Object.keys(error.constraints)
by Object.keys(error.constraints ?? {})
But obviously without a fix in the repo nothing will change when fetching from npm
Hi there,
I always get a TypeError: Cannot convert undefined or null to object\n when using validation method "i18n.validate" from the "I18nService" while validating nested array of objects.
This error does not occur, when the normal "validate" function of the "class-validator" library is used.
Thank you, Heiko
My Dto (Parent)
My Dto (Nested)
My Pipe
Stack Trace
Originally posted by @heken84 in https://github.com/toonvanstrijp/nestjs-i18n/discussions/630