I use middy as validator package which is based on ajv, I set jsonschema
like follows
JavaScript
x
5
1
serviceDate: {
2
type: 'string',
3
format: 'date-time'
4
},
5
my request
is ilke this,this is basically aligned with date-time
JavaScript
1
4
1
{
2
"serviceDate":"2022-03-06T00:00:00"
3
}
4
But it returned following error response. I totally confused what is the wrong point of that. If someone has opinion ,please let me know. Thanks
JavaScript
1
16
16
1
{
2
"errorMessage": "Request body validation failed: does not conform to the "date-time" format, is not of a type(s) number, is not of a type(s) number, is not of a type(s) number",
3
"errorType": "Error",
4
"offlineInfo": "If you believe this is an issue with serverless-offline please submit it, thanks. https://github.com/dherault/serverless-offline/issues",
5
"stackTrace": [
6
"Error: Request body validation failed: does not conform to the "date-time" format, is not of a type(s) number, is not of a type(s) number, is not of a type(s) number",
7
"at payloadSchemaValidator (file:///Users/h.miyashita/post-pricing/packages/presentation/rest-api/shop-api/node_modules/serverless-offline/src/events/http/payloadSchemaValidator.js:7:11)",
8
"at hapiHandler (file:///Users/h.miyashita/post-pricing/packages/presentation/rest-api/shop-api/node_modules/serverless-offline/src/events/http/HttpServer.js:638:11)",
9
"at exports.Manager.execute (/Users/h.miyashita/post-pricing/node_modules/@hapi/hapi/lib/toolkit.js:57:29)",
10
"at Object.internals.handler (/Users/h.miyashita/post-pricing/node_modules/@hapi/hapi/lib/handler.js:46:48)",
11
"at exports.execute (/Users/h.miyashita/post-pricing/node_modules/@hapi/hapi/lib/handler.js:31:36)",
12
"at Request._lifecycle (/Users/h.miyashita/post-pricing/node_modules/@hapi/hapi/lib/request.js:371:68)",
13
"at processTicksAndRejections (node:internal/process/task_queues:95:5)"
14
]
15
}
16
I found following list which come from validation packages.I still have not found root cause of this
JavaScript
1
5
1
'date-time': /^d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])[tT ](2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])(.d+)?([zZ]|[+-]([0-5][0-9]):(60|[0-5][0-9]))$/,
2
'date': /^d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])$/,
3
'time': /^(2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])$/,
4
'duration': /P(Td+(H(d+M(d+S)?)?|M(d+S)?|S)|d+(D|M(d+D)?|Y(d+M(d+D)?)?)(Td+(H(d+M(d+S)?)?|M(d+S)?|S))?|d+W)/i,
5
Advertisement
Answer
Your input "2022-03-06T00:00:00"
doesn’t match the date-time
regex
JavaScript
1
2
1
^d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])[tT ](2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])(.d+)?([zZ]|[+-]([0-5][0-9]):(60|[0-5][0-9]))$
2
It almost does. It matches this part of regex:
JavaScript
1
2
1
^d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])[tT ](2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])(.d+)?
2
You are missing the other part:
JavaScript
1
2
1
([zZ]|[+-]([0-5][0-9]):(60|[0-5][0-9]))$
2
Which enforces you to supply a time zone. A few examples:
JavaScript
1
4
1
2022-03-06T00:00:00z
2
2022-03-06T00:00:00Z
3
2022-03-06T00:00:00+03:00
4