-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response validation: Automatically coerce date data type to ISO string #288
Comments
@xeoneux is this occurring with response validation or request validation? |
This is with response validation, didn't check the request part. |
@xeoneux I did some investigation here. This is a bit tricky to solve. I'm not sure a satisfactory solution exists within Ajv. I tried to create a custom Ajv keyword. A keyword will enable me to manipulate the
AFTER is the issue. Ultimately, validation will fail despite modifying the data because an I certainly agree that using The following is the keyword I was experimenting with. I added this keyword to the response validator.
|
keeping this open, perhaps folks have alternative ideas |
I have the same problem and found this, which is related: #246 Still a workaround, and I still don't know if it works, but hey. |
You can work around the addKeyword issue with https://github.com/pageboard/server/blob/876236cf0603e4a31b15d793605195008c350648/packages/api/index.js#L70-L100 |
Do you accept bounties on issues? I could put 50$US for a fix on this one. This is the only blocking issue for us. We have to |
Feedback given. Thank you all for the effort on this issue. |
this is solved in |
…ize` request parameters or fields. It could resolve : cdimascio#353 cdimascio#465 cdimascio#288 cdimascio#246 Unit tests validate Date and MongoDb ObjectID. Developers have choice to : - only serialize response contents - also deserialize request strings to custom objects Frequent SerDes are defined in base.serdes.ts (date and date-time). Documentation updated with this setting
* `serDes` setting allows to `serialize` response objects and `deserialize` request parameters or fields. It could resolve : #353 #465 #288 #246 Unit tests validate Date and MongoDb ObjectID. Developers have choice to : - only serialize response contents - also deserialize request strings to custom objects Frequent SerDes are defined in base.serdes.ts (date and date-time). Documentation updated with this setting * I don't know why there was a cloneDeep but it seems to be necessary in all cases. * Fix validation problems with oneOf and force default SerDes when no SerDes are defined (force DateTime and Date serialization). * Delete old code comments * New test : If I answer with an object which serialize fails (here because I answer with an ObjectID instead of Date and no toISOString function exists), a 500 error is thrown. * Add Date and date-time serialization by default in addition to other serDes settings. Custom settings can also override date and date-time formats in order to also deserialize date and/or date-time on requests * Add Date and date-time serialization by default in addition to other serDes settings. Custom settings can also override date and date-time formats in order to also deserialize date and/or date-time on requests * `serDes` option adaptation to be more user friendly Test OK Documentation is modified I also changed my https://github.com/pilerou/mongo-serdes-js with a 0.0.3 version which is compliant to the design : ```javascript serDes: [ OpenApiValidator.baseSerDes.date.serializer, OpenApiValidator.baseSerDes.dateTime, MongoSerDes.objectid, // this configuration if we want to deserialize objectid in request and serialize it in response MongoSerDes.uuid.serializer, // this configuration if we only want to serialize on response ], ``` * When we add custom formats in serDes, they are automatically added to unknownFormats * Rename OpenApiValidator.baseSerDes to OpenApiValidator.serdes
Is your feature request related to a problem? Please describe.
When validating responses, if the data type is a js
Date
object, the validation of type string with format date-time fails.Describe the solution you'd like
The lib should try to coerce the type to ISO string and then try validation on it.
Describe alternatives you've considered
I have to programmatically search through all the object to find a date data type and call the
toISOString
method on it to pass the output validation.Additional context
None
The text was updated successfully, but these errors were encountered: