Hello!
I need to localize validation errors. Error contains the validated field, but not the validator name (and arguments?)
Is it possible to add the validation type to the error extensions?
e.g.:
...
],
"extensions": {
"field": "password",
"validation": "required", // < add this?
"specifiedBy": "http://spec.graphql.org/June2018/#sec-Input-Object-Required-Fields"
}
}
I tried to intercept the error and patch it, by looking for the "required" word in the message, or something, but error filter
does not work for validations.
Error helper and resources seem to be static, and internal.
Am I missing something?
You can allso check @benmccallum solution for input validation... https://github.com/benmccallum/fairybread
Made a version of middleware for data annotations using this: ValidateInputMiddleware.
It works for all validators except [Required] that happens before the middleware.
I think that maybe ErrorHelper can be made a singleton or injected, or at least, a validation type can be added as an extension:
public static IError FieldIsRequiredButNull(
this IDocumentValidatorContext context,
ISyntaxNode node,
string fieldName)
{
return ErrorBuilder.New()
.SetMessage(Resources.ErrorHelper_FieldIsRequiredButNull, fieldName)
.AddLocation(node)
.SetPath(context.CreateErrorPath())
.SetExtension("field", fieldName)
.SetExtension("validator", "required") // inform what validation is being performed, for localization purposes!
.SpecifiedBy("sec-Input-Object-Required-Fields")
.Build();
}
To localize these error messages we will add error codes but have to wait for this one to be official:
https://github.com/graphql/graphql-spec/issues/708
@PascalSenn we already use error codes ... our error codes are located in the extensions section.
The plan is to introduce more error hot chocolate error codes of the next few releases ....
Most helpful comment
You can allso check @benmccallum solution for input validation... https://github.com/benmccallum/fairybread