Hi all! I am using `middy-validator` to validate r...
# sst
i
Hi all! I am using
middy-validator
to validate requests in
SST
is there a way to add custom message? Also...is it related to middy or
JSON schema validation
? Tried to google it but found nothing
a
I guess you need to add a middleware that takes that error message and parses it to whatever format you like
e
IIRC out of the box you can’t really customize middy-validator. I wanted to get it to return the validation error as a response and I think the dev said that not being able to do that was a (security) feature.
a
Haven't worked with middy but that sounds like bullsh* to me 😅
e
It’s only one or two lines to validate an incoming body using AVJ directly, anyway. I suppose it does have a bit of value when combined with
@middy/http-json-body-parser
and
@middy/http-error-handler
but my experience was that the setup code to get the types right was probably more than I would have saved by simply adding a few lines the parse the body and run it through a validator myself.
r
In the older version of middy-validator (^1.0.0), it actually bundles in ajv-errors automatically and you can add custom error messages to your schemas as
errorMessage
"properties": {
"token": {
"type": "string",
"minLength": 50,
"errorMessage": "The API Token is required and must be a string greater than 50 characters"
},
In the newest version they don’t include ajv-errors by default and have to import it separately and pass it in. The docs on how to do this were not working for me tbh, and just had to pin the old version and use that and move on